Explanation of the PHP memory pool's storage layer (1) _ PHP Tutorial

Source: Internet
Author: User
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:

 
 
  1. /* Heaps with user defined storage */
  2. Typedef struct _ zend_mm_storage;
  3. Typedef struct _ zend_mm_segment {
  4. Size_t size;
  5. Struct _ zend_mm_segment * next_segment;
  6. } Zend_mm_segment;
  7. Typedef struct _ zend_mm_mem_handlers {
  8. Const char * name;
  9. Zend_mm_storage * (* init) (void * params); // initialize the function
  10. Void (* dtor) (zend_mm_storage * storage); // destructor
  11. Void (* compact) (zend_mm_storage * storage );
  12. Zend_mm_segment * (* _ alloc) (zend_mm_storage * storage, size_t size); // memory allocation function
  13. Zend_mm_segment * (* _ realloc) (zend_mm_storage * storage, zend_mm_segment * ptr, size_t size); // realallocates the memory function.
  14. Void (* _ free) (zend_mm_storage * storage, zend_mm_segment * ptr); // releases the memory function
  15. } Zend_mm_mem_handlers;
  16. Struct _ zend_mm_storage {
  17. Const zend_mm_mem_handlers * handlers; // process the function set
  18. Void * data;
  19. };

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:

 
 
  1. /* 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. */
  2. # 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}
  3. /* Use the mmap memory ing function to allocate private mappings copied during memory write and map them to/dev/zero. */
  4. # 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}
  5. /* 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 */
  6. # 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}
  7. /* Use malloc to allocate memory. by default, if ZEND_WIN32 macro is added, the win32 allocation scheme is used */
  8. # Define ZEND_MM_MEM_MALLOC_DSC {"malloc", zend_mm_mem_dummy_init, runtime, zend_mm_mem_malloc_realloc, zend_mm_mem_malloc_free}
  9. Static const zend_mm_mem_handlers mem_handlers [] = {
  10. # Ifdef HAVE_MEM_WIN32
  11. ZEND_MM_MEM_WIN32_DSC,
  12. # Endif
  13. # Ifdef HAVE_MEM_MALLOC
  14. ZEND_MM_MEM_MALLOC_DSC,
  15. # Endif
  16. # Ifdef HAVE_MEM_MMAP_ANON
  17. ZEND_MM_MEM_MMAP_ANON_DSC,
  18. # Endif
  19. # Ifdef HAVE_MEM_MMAP_ZERO
  20. ZEND_MM_MEM_MMAP_ZERO_DSC,
  21. # Endif
  22. {NULL, NULL}
  23. };

1

Random (hierarchical. This manager has three layers: storage, heap, and emalloc/efree. The storage layer uses malloc (), mmap...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.