note 85085 modified in function.shm-attach by felipe

From: Date: Fri, 15 Aug 2008 03:32:09 +0000
Subject: note 85085 modified in function.shm-attach by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-143513@lists.php.net to get a copy of this message
<?php //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; ?> --was-- //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; http://php.net/manual/en/function.shm-attach.php

« previous php.notes (#143513) next »