1================================================================================
2Solr Notes about Memory Allocation and interaction with HashTables
3================================================================================
4
5The following notes are for C-space PHP developers.
6
7If you are not really familiar with how the Zend HashTable API or the
8memory allocation marcros really works, please take sometime to read the notes below :
9
10This is correct as of Zend Engine version 2.3.0
11
12- If memory was allocated with emalloc(), it has to be freed with efree().
13
14- If memory was allocated using pemalloc(), the same value for the persistent
15parameter must be used during pefree() to deallocated the allocated memory.
16The memory manager for the Zend API will then decide whether to use free() or
17efree() depending on whether persistent is 1 or 0 respectively. The same
18principle applies to pestrdup(), pecalloc() and the other memory allocation
19macros.
20
21- When inserting values into the HashTables, if the value for the persistent
22parameter is set, then the memory allocation for the entered item should be
23persistent to i.e. pemalloc() with persistent set to 1.
24
25The following will apply when adding new values into a HashTable for items
26that were dynamically allocated :
27
28(a) If the value for the nDataSize parameter is the size of that of a
29pointer sizeof(void *), the HashTable API copies the contents of
30the pData parameter into the Bucket->pDataPtr member for that data Bucket.
31Then it sets the Bucket->pData member to the address of the Bucket->pDataPtr
32member for that data Bucket.
33
34(b) If the value for the nDataSize parameter is not equal to sizeof(void *),
35the HashTable API allocates new memory for the Bucket->pData member using
36the size equivalent to nDataSize and the same value for the persistent flag
37set for the target HashTable. The the contents of the pData parameter is
38copied into the Bucket->pData member in this newly allocated memory location.
39Then the Bucket->pDataPtr member for this Bucket is set to NULL.
40
41Do not worry about the newly allocated memory allocated by the HashTable API;
42if the nDataSize parameter was not equal to sizeof(void *), then
43during the cleanup process, the Zend API will free the new memory it allocated
44during the insert process.
45
46It will also call the destructor function if a valid one was passed when the
47HashTable was initialized with zend_hash_init().
48
49In the extension, I have used the p* version of the memory allocation functions
50to allow me toggle if there is any need to do so in the future. This will prevent
51a massive search and replace effort.
52
53For all the HashTables, I set the intial size to 8 to reduce the looping when
54zend_hash_init() is setting up the HashTable pointer to use.
55