defining a global object table
| From: | j adams | Date: | Mon, 07 Nov 2011 01:14:35 +0000 |
| Subject: | defining a global object table | ||
| Groups: | php.pecl.dev | ||
| Request: | Send a blank email to pecl-dev+get-8818@lists.php.net to get a copy of this message | ||
Forgive me for my n00bishness and this lengthy email, but I'm very new
to the PHP code base and my C is quite rusty.
I'm working on AMFEXT, trying to implement the serialization of
objects and arrays by reference. The basic idea is that arrays and
objects, once encountered during the serialization of a data object,
are listed in a global array. If they are encountered again, then
they are serialized as a reference rather than being stored all over
again redundantly. This allows for serialization of recursively
defined data objects and reduces the size of the output string.
In my source code (amf.c and php_amf.h) I am trying to define the
global array of array/object references such that it satisfies these
conditions:
condition 1) the array is initialized as empty with each new call in a
PHP script to the function amf_encode
condition 2) the array is available within the scope of all my
functions in amf.c
condition 3) given an array or object to be serialized (e.g. zval *val
parameter supplied to a function), I can search the array to check of
val has been encountered previously
condition 4) the array is destroyed and any memory it used is freed up
once serialization is complete.
condition 5) there is no danger of race conditions or data cross-talk
between concurrent threads or processes or subsequent calls to
amf_encode
I believe this should be accomplished by:
step 1) declaring something like this in php_amf.h:
/*
Declare any global variables you may need between the BEGIN
and END macros here:
*/
ZEND_BEGIN_MODULE_GLOBALS(amf)
zval **objects_and_arrays; // array containing memory addresses of
arrays and objects encountered
int objects_and_arrays_count; // number of serialized arrays/objects
stored in the array
int objects_and_arrays_allocated; // number of slots allocated for the array
ZEND_END_MODULE_GLOBALS(amf)
step 2) initialize the array at the entry point of the amf_encode function:
/*
The entry point for all calls to amf_encode
*/
static PHP_FUNCTION(amf_encode) {
AMF_G(objects_and_arrays) = NULL;
AMF_G(objects_and_arrays_count) = 0;
AMF_G(objects_and_arrays_allocated) = 0;
...
}
step 3) when arrays/objects/zvals are encountered during
serialization, store their memory address in the array
Code TBD
step 4) when serialization is complete, free the memory allocated to the array.
Code TBD
I have some questions
question 1) Should I initialize my objects_and_arrays variables in the
PHP_GINIT_FUNCTION instead?
question 2) have I properly declared my objects_and_arrays variables?
I.e., can I be sure there will be no crosstalk between threads or
processes?
question 3) when I allocate memory for my dynamic objects_and_arrays
array, can I use realloc or should I use some other allocation
function? I've seen lots of macros defining new function names in
zend_alloc.h i expect to write an AddToArray function like the one
described here:
http://fydo.net/gamedev/dynamic-arrays
question 4) when I allocate memory for my dynamic objects_and_arrays
array, how much should I allocate per array element? Given that these
are pointers, I expect they can be either 4 bytes or 8 depending on
the OS. Are there any best practices for php development that might
guide me in allocating memory for 32-bit vs. 64-bit systems? Can
someone refer me to good source code examples?
question 5) Does the overall approach I've outlined look sound?
I'd be truly grateful for any advice, tips, heuristic guidelines, etc.