shmopIf

neon 04.10.03 13:08

Basic shmopIf with dynamic allocation. Consult www.php.net and you system man pages for more information. See also the ftok() function for index creation.

 Tekstiversio  Arvo: 1 (1 ääntä)  Äänestä: +  -
<?php
/**
* READ http://www.php.net/manual/en/ref.shmop.php
*/

class shmopIf
{
    /**
     * Data in current block
     * @access private
     * @var string
     */

    var $block;
    /**
     * User given input data
     * @access private
     * @var string
     */

    var $data;
    /**
     * Current block resource
     * @access private
     * @var resource
     */

    var $id;
    /**
     * Open block size
     * @access private
     * @var int
     */

    var $blockSize;
    /**
     * User input data lenght
     * @access private
     * @var int
     */

    var $len;
    /**
     * Block index, a way to create one:
     * see PHP's ftok() function
     * @access private
     * @var string
     */

    var $index;
    /**
     * Max size for shared memory block
     * @access public
     * @var int
     */

    var $maxSize = 4096;
    /**
     * Shared memory block permissions.
     * Permissions need to be passed in octal form ex. 0644.
     * See unix man page for more inrormation. For example "man chmod" will give you quite much information.
     * @access private
     * @var long
     */

    var $flags = '0644';
    /**
     * Open existing shared memory block. Notice that index MUST have been created beforehand.
     * Only one block can be open each time.
     * To create a new block use allocate() method instead.
     * @access public
     * @param string shared memory block index
     * @return string index
     */

    function open($index,$flags = '')
    {
        if($flags)
            $this->flags = $flags;
        // Issue an error if an other block is open.
        if($this->id) {
            $this->close();
            trigger_error("Memory segment open",256);
        }
        $this->index = $index;
        // open
        $this->id = shmop_open($this->index,"w",$this->flags,0);
        // get size
        $this->blockSize = shmop_size($this->id);
        // gte data
        $this->block = shmop_read($this->id,0,$this->blockSize);
        return $this->index;
    }
    /**
     * Read $len bytes
     * @access public
     * @param int how many bytes to read
     * @return string string
     */

    function read($len)
    {
        if($len == 0)
            $len = $this->blockSize;
        // use "cache"
        return substr($this->block,0,$len);
    }
    /**
     * Write data to open shared memory block
     * @access public
     * @param string
     * @return int lenght
     */

    function write($data)
    {
        $this->data = $data;
        $this->len = strlen($this->data);
        // In case given data string is bigger than allocated block
        if($this->len > $this->blockSize) {
            // delete existing block
            $this->delete();
            // allocate a new one
            $this->_allocateNew();
        }
        $bytesWritten = shmop_write($this->id,$data);
        // not everything ...
        if($bytesWritten != $this->len) {
            $this->delete();
            trigger_error("Error writing to shared memory",256);
        }
        $this->block = $this->data;
        return $this->len;
    }
    /**
     * Allocate a new shared memory block
     * @access private
     */

    function _allocateNew()
    {
        if($this->id) {
            $this->close();
            trigger_error("Memory segment open",256);
        }
        // too big, issue an error
        if($this->len > $this->maxSize) {
             trigger_error("Exceeded the $this->maxSize bytes limit",256);
        }
        // create
        $this->id = shmop_open($this->index,"c",$this->flags,$this->len);
    }
    /**
     * Delete a block
     * @access public
     */

    function delete()
    {
        // mark for deleting
        $res = shmop_delete($this->id);
        // MUST be closed properly after delete
        $this->close();
        // something went wrong
        if($res == 0)
            trigger_error("Unable to delete shared memory block!",256);
               
    }
    /**
     * Allocate a new shared memory block, user interface
     * @access public
     * @var string index
     * @return string index
     */

    function allocate($index,$len,$flags='')
    {
        if($flags)
            $this->flags = $flags;
        if($this->id)
            trigger_error("Memory segment open",256);
        $this->index = $index;
        $this->len = $len;
        $this->_allocateNew();
        return $this->index;
    }
    /**
     * Standard close
     * @access public
     */

    function close()
    {
        shmop_close($this->id);
        return true;
    }
}
?>

ane 20:29 4.10.03 
Veikeä. Mutta en saanut C:llä yhteyttä tuohon muistiin, varmaan johtuu PHP:n tavasta tallentaa dataa :/