Details about the storage layer (1) in the PHP memory pool ). The memory manager of PHP is hierarchical. This manager has three layers: storage, heap, and emallocefree. The storage layer uses the memory manager of malloc () and mmap PHP to hierarchical (hierarchical. This manager has three layers: storage, heap, and emalloc/efree. The storage layer uses functions such as malloc () and mmap () to apply for memory from the system, and releases the applied memory through the free () function.
The storage layer usually applies for a large amount of memory blocks. the memory size requested here does not mean that the storage layer structure requires a large amount of memory, but when the heap layer calls the storage layer allocation method, the memory requested in the format of segments is relatively large. the storage layer is used to make the memory allocation method transparent to the heap layer.
First, let's look at the structure of the storage layer:
- /* Heaps with user defined storage */
- Typedef struct _ zend_mm_storage;
-
- Typedef struct _ zend_mm_segment {
- Size_t size;
- Struct _ zend_mm_segment * next_segment;
- } Zend_mm_segment;
-
- Typedef struct _ zend_mm_mem_handlers {
- Const char * name;
- Zend_mm_storage * (* init) (void * params); // initialize the function
- Void (* dtor) (zend_mm_storage * storage); // destructor
- Void (* compact) (zend_mm_storage * storage );
- Zend_mm_segment * (* _ alloc) (zend_mm_storage * storage, size_t size); // memory allocation function
- Zend_mm_segment * (* _ realloc) (zend_mm_storage * storage, zend_mm_segment * ptr, size_t size); // realallocates the memory function.
- Void (* _ free) (zend_mm_storage * storage, zend_mm_segment * ptr); // releases the memory function
- } Zend_mm_mem_handlers;
-
- Struct _ zend_mm_storage {
- Const zend_mm_mem_handlers * handlers; // process the function set
- Void * data;
- };
Memory allocation method. the called function is the processing function set in the _ zend_mm_storage structure, and the memory is represented in segments.
Four memory solutions
PHP has four memory allocation schemes at the storage layer: malloc, win32, mmap_anon, and mmap_zero. By default, malloc is used to allocate memory. if ZEND_WIN32 macro is set, HeapAlloc is called to allocate memory for windows. The remaining two memory schemes are anonymous memory ING, in addition, the PHP memory solution can be modified by setting variables.
Official instructions are as follows:
The Zend MM can be tweaked using ZEND_MM_MEM_TYPE and ZEND_MM_SEG_SIZE environment variables. default values are "malloc" and "256K ". dependent on target system you can also use "mmap_anon", "mmap_zero" and "win32" storage managers.
In the code, for the four memory allocation schemes, each processing function in zend_mm_mem_handlers is implemented. The code is described as follows:
- /* Use the mmap memory ing function to allocate private mappings copied during memory write, and perform anonymous Ing. the ing area is not associated with any files. */
- # Define register {"mmap_anon", zend_mm_mem_dummy_init, commit, zend_mm_mem_dummy_compact, commit, zend_mm_mem_mmap_realloc, zend_mm_mem_mmap_free}
-
- /* Use the mmap memory ing function to allocate private mappings copied during memory write and map them to/dev/zero. */
- # Define register {"mmap_zero", zend_mm_mem_mmap_zero_init, commit, zend_mm_mem_dummy_compact, commit, zend_mm_mem_mmap_realloc, zend_mm_mem_mmap_free}
-
- /* Use HeapAlloc to allocate memory for windows. in this regard, VirtualAlloc () to allocate memory is written in the comment, and HeapAlloc is used in the program */
- # Define ZEND_MM_MEM_WIN32_DSC {"win32", zend_mm_mem_win32_init, terminal, zend_mm_mem_win32_compact, terminal, terminal, zend_mm_mem_win32_free}
-
- /* Use malloc to allocate memory. by default, if ZEND_WIN32 macro is added, the win32 allocation scheme is used */
- # Define ZEND_MM_MEM_MALLOC_DSC {"malloc", zend_mm_mem_dummy_init, runtime, zend_mm_mem_malloc_realloc, zend_mm_mem_malloc_free}
-
- Static const zend_mm_mem_handlers mem_handlers [] = {
- # Ifdef HAVE_MEM_WIN32
- ZEND_MM_MEM_WIN32_DSC,
- # Endif
- # Ifdef HAVE_MEM_MALLOC
- ZEND_MM_MEM_MALLOC_DSC,
- # Endif
- # Ifdef HAVE_MEM_MMAP_ANON
- ZEND_MM_MEM_MMAP_ANON_DSC,
- # Endif
- # Ifdef HAVE_MEM_MMAP_ZERO
- ZEND_MM_MEM_MMAP_ZERO_DSC,
- # Endif
- {NULL, NULL}
- };
1
Random (hierarchical. This manager has three layers: storage, heap, and emalloc/efree. The storage layer uses malloc (), mmap...