Icicle\Concurrent\Sync\SharedMemoryParcel::wrap PHP Method

wrap() protected method

If the value requires more memory to store than currently allocated, a new shared memory segment will be allocated with a larger size to store the value in. The previous memory segment will be cleaned up and marked for deletion. Other processes and threads will be notified of the new memory segment on the next read attempt. Once all running processes and threads disconnect from the old segment, it will be freed by the OS.
protected wrap ( $value )
    protected function wrap($value)
    {
        if ($this->isFreed()) {
            throw new SharedMemoryException('The object has already been freed.');
        }
        $serialized = serialize($value);
        $size = strlen($serialized);
        $header = $this->getHeader();
        /* If we run out of space, we need to allocate a new shared memory
              segment that is larger than the current one. To coordinate with other
              processes, we will leave a message in the old segment that the segment
              has moved and along with the new key. The old segment will be discarded
              automatically after all other processes notice the change and close
              the old handle.
           */
        if (shmop_size($this->handle) < $size + self::MEM_DATA_OFFSET) {
            $this->key = $this->key < 0xffffffff ? $this->key + 1 : mt_rand(0x10, 0xfffffffe);
            $this->setHeader(self::STATE_MOVED, $this->key, 0);
            $this->memDelete();
            shmop_close($this->handle);
            $this->memOpen($this->key, 'n', $header['permissions'], $size * 2);
        }
        // Rewrite the header and the serialized value to memory.
        $this->setHeader(self::STATE_ALLOCATED, $size, $header['permissions']);
        $this->memSet(self::MEM_DATA_OFFSET, $serialized);
    }