php - Predict size needed to store data in shared memory -


I am working with PHP shm (part of the project, not to be confused with people!) Functions in a project I am Actually sharing memory works as a kind of heap, I have only one array in which I am storing keys with key (stored index), I just check "ah, it's already" I am Now my problem is that the array can be quite big on time, but it is not always the case that I do not want to reserve large amounts of memory which I usually do not need, rather it dynamically shapes.

I have registered an error handler which converts the errors to ErrorException s, so I hold error shm_put_var When there is minimal memory to store the area - but unfortunately the PHP data does not recover, then clears the segment, then all other data is lost, too. This is not an option therefore.

For this reason I need a way to predict the size to store the data. Specifies that PHP (PHP_INT_SIZE * 4) + 8 length of bytes, and a variable is needed strlen (serialize ($ foo)) + 4 * PHP_INT_SIZE) + 4 (I have simplified the expression given in the comment, it is equal to me but it was indispensable)
While the size of the header is correct (in an error, smaller than 24 byte results On memory creation, hence the 24 bytes heading seems to be the php), now the variation of PHP's recent entries in the size of each variable entry Does not look true in the scans:
- I "1" 24 + strlen (serialize ("1") + 3 * PHP_INT_SIZE) + 4 in the shared memory segment with the size of the byte << Strong> 3 instead of 3 ), I - "999" in a size 24 + strlen (serialize ("999") + 4 * PHP_INT_SIZE) Can not store + 4

Does anyone know a way to predict how much memory is required to use shm functions in any shared memory or how to store shm stores Well on the store G reference? (I read the entire contents of shmop functions and printed them, but since it is binary data, it is not reverse-engineer at the right time)

(I'm not sure if I will provide code samples as needed Parts will be relevant - if you want to see some work samples, ping me, I have tried a lot, so I have samples ready for most cases) [Update T] is very bad, so I can not see the source (and) far, but I already have a problem that was suggested on php.net: when I can simplify the complex formula Whatever I have included here (which was originally taken from the source code), it is not possible with the original one, because typecast and no floating point are math. The formula divides by size (long) and multiplies with it again - which is useless in PHP but rounds the coefficient of shaped (long) Is i right in PHP first? However, this is not all, because the test has shown that I can store some values ​​as compared to returning formula (see above) in low memory.

When trying to update it, as the solution to the problem of the variable being removed and the new value There is not enough free space in the Segment, if there is enough free space, then you can check first, and if so, then you proceed to update.
Uses the following function used to get free, and total space used in the section shmop _ * created in the code created with shm_attach .

function getMemSegmentStats ($ segmentKey) {$ segId = shmop_open ($ block, 'a', 0, 0); $ Wc = PHP_INT_SIZE / 4; $ Stats = unpack ("I {$ wc} / I {$ wc} Free / I {$ wc} total", shmop_read ($ segId, 8 + PHP_INT_SIZE, 3 * PHP_INT_SIZE)); Shmop_close ($ segId); Return Actions UnpackLHwords ($ figures); } Function combineUnpackLHwords ($ array) {foreach ($ key as key = & gt; & amp; $ val) if (preg_match ('/ ([^ \ d] +) (\ d +) /', $ Key, $ match)) {$ Key2 = $ matches [1]. ($ Matches [2] +1); $ Array [$ matches [1]] = $ vine | $ Array [$ 2] & lt; & Lt; 4 * PHP_INT_SIZE; Unset ($ array [$ k], $ array [$ key2]); } $ Array returned; }

In the 64bit machines, the function combineUnpackLHwords is required because the function does not unpack 64bit integers, so they have low-order and high-32-bit term (No effect of function on 32 bit machines) Order

Example:

  $ segmentKey = ftok ('/ path / to / a / file', 'A'); $ SegmentStats = getMemSegmentStats ($ segmentKey); Print_r ($ Segment Stats);  

Output:

  Array ([used] => 32 9 6 [free] => 96704 [total] => 100000)  

Comments