note 85085 added to function.shm-attach
| From: | zeppelinux at comcast dot net | Date: | Thu, 14 Aug 2008 07:19:12 +0000 |
| Subject: | note 85085 added to function.shm-attach | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143482@lists.php.net to get a copy of this message | ||
//how to calculate the minimum $memsize required to store the variable $foo where
$foo='foobar'.
// when shm_attach() is called for the first time, PHP writes a header to the beginning of the
shared memory.
$shmHeaderSize = (PHP_INT_SIZE * 4) + 8;
// when shm_put_var() is called, the variable is serialized and a small header is placed in front of
it before it is written to shared memory.
$shmVarSize = (((strlen(serialize($foo))+ (4 * PHP_INT_SIZE)) /4 ) * 4 ) + 4;
// now add the two together to get the total memory required. Of course, if you are storing more
than one variable then you dont need to add $shmHeaderSize for each variable, only add it once.
$memsize = $shmHeaderSize + $shmVarSize;
//this will give you just enough memory to store the one variable using shm_put_var().
$shm_id = shm_attach ( $key, $memsize, 0666 ) ;
shm_put_var ( $shm_id , $variable_key , $foo );
any attempt to store another variable will result in a 'not enough memory' error.
Be aware that if you change the contents of $foo to a larger value and then you try to write it to
shared memory again using shm_put_var(), then you will get a 'not enough memory' error. In
this case, you will have to resize your shared memory segment and then write the new value.
If you are only storing variables that contain a single integer value, then you can avoid having to
resize by always allocating the largest amount of memory that is required to store an int, which
should be:
$shmIntVarSize = (((strlen(serialize(PHP_INT_MAX))+ (4 * PHP_INT_SIZE)) /4 ) * 4 ) + 4;
----
Server IP: 64.71.164.2
Probable Submitter: 67.180.43.174
----
Manual Page -- http://www.php.net/manual/en/function.shm-attach.php
Edit -- https://master.php.net/note/edit/85085
Del: integrated -- https://master.php.net/note/delete/85085/integrated
Del: useless -- https://master.php.net/note/delete/85085/useless
Del: bad code -- https://master.php.net/note/delete/85085/bad+code
Del: spam -- https://master.php.net/note/delete/85085/spam
Del: non-english -- https://master.php.net/note/delete/85085/non-english
Del: in docs -- https://master.php.net/note/delete/85085/in+docs
Del: other reasons-- https://master.php.net/note/delete/85085
Reject -- https://master.php.net/note/reject/85085
Search -- https://master.php.net/manage/user-notes.php