<?php /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */ /** * * Contains the System_SharedMemory_Common base class * * PHP versions 4 and 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category System * @package System_Sharedmemory * @author Evgeny Stepanischev <bolk@lixil.ru> * @copyright 2005 Evgeny Stepanischev * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id:$ * @link http://pear.php.net/package/System_SharedMemory */ /** * * System_SharedMemory_Common is the base class from which each database driver class extends * * @category System * @package System_Sharedmemory * @package System_Sharedmemory * @author Evgeny Stepanischev <bolk@lixil.ru> * @copyright 2005 Evgeny Stepanischev * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id:$ * @link http://pear.php.net/package/System_SharedMemory */ // {{{ class System_SharedMemory_Common class System_SharedMemory_Common { // {{{ isConnected() /** * returns true if plugin was * successfully connected to backend * * @return bool true if connected * @access public */ function isConnected() { return true; } // }}} // {{{ engineName() /** * returns name of current engine * * @return string name of engine * @access public */ function engineName() { return strtolower(substr(basename(__FILE__), 0, -4)); } // }}} // {{{ _default() /** * fill non-set properties by def values * * @param array options array * @param array hash of pairs keys and default values * * @return array filled array * @access public */ function _default($options, $def) { foreach ($def as $key=>$val) { if (!isset($options[$key])) { $options[$key] = $val; } } return $options; } // }}} } // }}} ?>
<?php /** * * The System V driver for SharedMemory * * PHP versions 4 and 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category System * @package System_Sharedmemory * @author Evgeny Stepanischev <bolk@lixil.ru> * @copyright 2005 Evgeny Stepanischev * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id:$ * @link http://pear.php.net/package/System_SharedMemory */ /** * * The methods PEAR SharedMemory uses to interact with PHP's System V extension * for interacting with System V shared memory * * These methods overload the ones declared System_SharedMemory_Common * * @category System * @package System_Sharedmemory * @package System_Sharedmemory * @author Evgeny Stepanischev <bolk@lixil.ru> * @copyright 2005 Evgeny Stepanischev * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id:$ * @link http://pear.php.net/package/System_SharedMemory */ require_once 'Common.php'; require_once "PEAR.php"; // {{{ class System_SharedMemory_Systemv class System_SharedMemory_Systemv extends System_SharedMemory_Common { // {{{ properties /** * true if plugin was connected to backend * * @var bool * * @access private */ var $_connected; /** * handler for shmop_* functions * * @var string * * @access private */ var $_h; // }}} // {{{ constructor /** * Constructor. Init all variables. * * @param array $options * * @access public */ function System_SharedMemory_Systemv($options) { extract($this->_default($options, array ( 'size' => false, 'project' => 's', ))); if ($size === false) { $this->_h = shm_attach($this->_ftok($project)); } else { if ($size < SHMMIN || $size > SHMMAX) { return $this->_connection = false; } $this->_h = shm_attach($this->_ftok($project), $size); } $this->_connection = true; } // }}} // {{{ isConnected() /** * returns true if plugin was * successfully connected to backend * * @return bool true if connected * @access public */ function isConnected() { return $this->_connected; } // }}} // {{{ get() /** * returns value of variable in shared mem * * @param int $name name of variable * * @return mixed value of the variable * @access public */ function get($name) { return shm_get_var($this->_h, $this->_s2i($name)); } // }}} // {{{ set() /** * set value of variable in shared mem * * @param string $name name of the variable * @param string $value value of the variable * * @return bool true on success, false on fail * @access public */ function set($name, $value) { return shm_put_var($this->_h, $this->_s2i($name), $value); } // }}} // {{{ rm() /** * remove variable from memory * * @param string $name name of the variable * * @return bool true on success, false on fail * @access public */ function rm($name) { return shm_remove_var($this->_h, $this->_s2i($name)); } // }}} // {{{ _ftok() /** * ftok emulation for Windows * * @param string $project project ID * * @access private */ function _ftok($project) { if (function_exists('ftok')) { return ftok(__FILE__, $project); } $s = stat(__FILE__); return sprintf("%u", (($s['ino'] & 0xffff) | (($s['dev'] & 0xff) << 16) | (($project & 0xff) << 24))); } // }}} // {{{ _s2i() /** * convert string to int * * @param string $name string to conversion * * @access private */ function _s2i($name) { return unpack('N', str_pad($name, 4, "/0", STR_PAD_LEFT)); } // }}} } // }}} ?>
<?php /** * * The Shmop driver for SharedMemory * * PHP versions 4 and 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category System * @package System_Sharedmemory * @author Evgeny Stepanischev <bolk@lixil.ru> * @copyright 2005 Evgeny Stepanischev * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id:$ * @link http://pear.php.net/package/System_SharedMemory */ /** * * The methods PEAR SharedMemory uses to interact with PHP's Shmop extension * for interacting with Shmop shared memory * * These methods overload the ones declared System_SharedMemory_Common * * @category System * @package System_Sharedmemory * @package System_Sharedmemory * @author Evgeny Stepanischev <bolk@lixil.ru> * @copyright 2005 Evgeny Stepanischev * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id:$ * @link http://pear.php.net/package/System_SharedMemory */ require_once 'Common.php'; require_once "PEAR.php"; // {{{ class System_SharedMemory_Shmop class System_SharedMemory_Shmop extends System_SharedMemory_Common { // {{{ properties /** * handler for shmop_* functions * * @var string * * @access private */ var $_h; /** * Contains internal options * * @var string * * @access private */ var $_options; // }}} // {{{ constructor /** * Constructor. Init all variables. * * @param array $options * * @access public */ function System_SharedMemory_Shmop($options) { $this->_options = $this->_default($options, array ( 'size' => 1048576, 'tmp' => '/tmp', 'project' => 's' )); $this->_h = $this->_ftok($this->_options['project']); } // }}} // {{{ get() /** * returns value of variable in shared mem * * @param mixed $name name of variable or false if all variables needs * * @return mixed PEAR_error or value of the variable * @access public */ function get($name = false) { $id = shmop_open($this->_h, 'c', 0600, $this->_options['size']); if ($id !== false) { $ret = unserialize(shmop_read($id, 0, shmop_size($id))); shmop_close($id); if ($name === false) { return $ret; } return isset($ret[$name]) ? $ret[$name] : null; } return PEAR::raiseError('Cannot open shmop.', 1); } // }}} // {{{ set() /** * set value of variable in shared mem * * @param string $name name of the variable * @param string $value value of the variable * * @return bool true on success * @access public */ function set($name, $value) { $lh = $this->_lock(); $val = $this->get(); if (!is_array($val)) { $val = array(); } $val[$name] = $value; $val = serialize($val); return $this->_write($val, $lh); } // }}} // {{{ rm() /** * remove variable from memory * * @param string $name name of the variable * * @return bool true on success * @access public */ function rm($name) { $lh = $this->_lock(); $val = $this->get(); if (!is_array($val)) { $val = array(); } unset($val[$name]); $val = serialize($val); return $this->_write($val, $lh); } // }}} // {{{ _ftok() /** * ftok emulation for Windows * * @param string $project project ID * * @access private */ function _ftok($project) { if (function_exists('ftok')) { return ftok(__FILE__, $project); } $s = stat(__FILE__); return sprintf("%u", (($s['ino'] & 0xffff) | (($s['dev'] & 0xff) << 16) | (($project & 0xff) << 24))); } // }}} // {{{ _write /** * write to the shared memory * * @param string $val values of all variables * @param resource $lh lock handler * * @return mixed PEAR_error or true on success * @access private */ function _write(&$val, &$lh) { $id = shmop_open($this->_h, 'c', 0600, $this->_options['size']); if ($id) { $ret = shmop_write($id, $val, 0) == strlen($val); shmop_close($id); $this->_unlock($lh); return $ret; } $this->_unlock($lh); return PEAR::raiseError('Cannot write to shmop.', 2); } // }}} // {{{ &_lock() /** * access locking function * * @return resource lock handler * @access private */ function &_lock() { if (function_exists('sem_get')) { $fp = PHP_VERSION < 4.3 ? sem_get($this->_h, 1, 0600) : sem_get($this->_h, 1, 0600, 1); sem_acquire ($fp); } else { $fp = fopen($this->_options['tmp'].'/sm_'.md5($this->_h), 'w'); flock($fp, LOCK_EX); } return $fp; } // }}} // {{{ _unlock() /** * access unlocking function * * @param resource $fp lock handler * * @access private */ function _unlock(&$fp) { if (function_exists('sem_get')) { sem_release($fp); } else { fclose($fp); } } // }}} } // }}} ?>