Storage layer in the PHP memory pool

Source: Internet
Author: User
The memory manager of PHP is 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. Here we apply for "> <LI

The memory manager of PHP is 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.      
  4. Typedef struct _ zend_mm_segment {
  5. Size_t size;
  6. Struct _ zend_mm_segment * next_segment;
  7. } Zend_mm_segment;
  8.      
  9. Typedef struct _ zend_mm_mem_handlers {
  10. Const char * name;
  11. Zend_mm_storage * (* init) (void * params); // initialize the function
  12. Void (* dtor) (zend_mm_storage * storage); // destructor
  13. Void (* compact) (zend_mm_storage * storage );
  14. Zend_mm_segment * (* _ alloc) (zend_mm_storage * storage, size_t size); // memory allocation function
  15. Zend_mm_segment * (* _ realloc) (zend_mm_storage * storage, zend_mm_segment * ptr, size_t size); // realallocates the memory function.
  16. Void (* _ free) (zend_mm_storage * storage, zend_mm_segment * ptr); // releases the memory function
  17. } Zend_mm_mem_handlers;
  18.      
  19. Struct _ zend_mm_storage {
  20. Const zend_mm_mem_handlers * handlers; // process the function set
  21. Void * data;
  22. };

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

Advantages of anonymous memory ing

Mmem_zero solution:

(SVR 4)/dev/zero Memory Mapping

1. You can pass the pseudo device "/dev/zero" as a parameter to mmap and create a ing zone. The special feature of/dev/zero is that all read operations on the device file return a byte stream with a specified length of 0, and any written content is discarded. We are interested in using it to create a ing area, and using/dev/zero to create a ing area. Its content is initially 0.

2. The advantage of using/dev/zero is that when mmap creates a ing zone, it does not need a time-existent file. The pseudo file/dev/zero is enough. The disadvantage is that it can only be used between related processes. Compared with inter-process communication, inter-thread communication is more efficient. Regardless of the technology, access to shared data must be synchronized.

Mmem_anon solution:

(4.4 BSD) Anonymous Memory Mapping

1. Anonymous memory ING and use of the/dev/zero type do not require real files. To use anonymous ing, you need to input the MAP_ANON flag to mmap and set the fd parameter to-1.

2. The so-called anonymity means that the fd ing area is not associated with the file path name through fd. Anonymous memory ING is used between kinship-related processes.

Heap memory allocation statement in win32 solution

The HeapAlloc function declaration is as follows:

 

 
  1. WINBASEAPI
  2. _ Out_opt
  3. HANDLE
  4. WINAPI
  5. HeapCreate (
  6. _ In DWORD flOptions,
  7. _ In SIZE_T dwInitialSize,
  8. _ In SIZE_T dwMaximumSize
  9. );
  10.      
  11. WINBASEAPI
  12. BOOL
  13. WINAPI
  14. HeapDestroy (
  15. _ In HANDLE hHeap
  16. );
  17.      
  18. WINBASEAPI
  19. _ Bcount (dwBytes)
  20. LPVOID
  21. WINAPI
  22. HeapAlloc (
  23. _ In HANDLE hHeap,
  24. _ In DWORD dwFlags,
  25. _ In SIZE_T dwBytes
  26. );
  27.      
  28.      
  29. WINBASEAPI
  30. BOOL
  31. WINAPI
  32. HeapFree (
  33. _ Inout HANDLE hHeap,
  34. _ In DWORD dwFlags,
  35. _ Deref LPVOID lpMem
  36. );
  37.      
  38. WINBASEAPI
  39. SIZE_T
  40. WINAPI
  41. HeapSize (
  42. _ In HANDLE hHeap,
  43. _ In DWORD dwFlags,
  44. _ In LPCVOID lpMem
  45. );

◆ HHeap indicates the starting position of the process heap memory.

◆ DwFlags indicates heap memory allocation.

◆ DwBytes is the size of heap memory allocated.

Initialization

When zend_mm_startup is started, the program sets the memory allocation scheme and segment allocation size based on the configuration, as shown in the following code:

 

 
  1. ZEND_API zend_mm_heap * zend_mm_startup (void)
  2. {    
  3. Int I;
  4. Size_t seg_size;
  5. Char * mem_type = getenv ("ZEND_MM_MEM_TYPE ");
  6. Char * tmp;
  7. Const zend_mm_mem_handlers * handlers;
  8. Zend_mm_heap * heap;
  9.      
  10. If (mem_type = NULL ){
  11. I = 0;
  12. } Else {
  13. For (I = 0; mem_handlers [I]. name; I ++ ){
  14. If (strcmp (mem_handlers [I]. name, mem_type) = 0 ){
  15. Break;
  16.       }    
  17.      }    
  18. If (! Mem_handlers [I]. name ){
  19. Fprintf (stderr, "Wrong or unsupported zend_mm storage type '% s' \ n", mem_type );
  20. Fprintf (stderr, "supported types: \ n ");
  21. For (I = 0; mem_handlers [I]. name; I ++ ){
  22. Fprintf (stderr, "'% s' \ n", mem_handlers [I]. name );
  23.       }    
  24. Exit (255 );
  25.      }    
  26.     }    
  27. Handlers = & mem_handlers [I];
  28.      
  29. Tmp = getenv ("ZEND_MM_SEG_SIZE ");
  30. If (tmp ){
  31. Seg_size = zend_atoi (tmp, 0 );
  32. If (zend_mm_low_bit (seg_size )! = Zend_mm_high_bit (seg_size )){
  33. Fprintf (stderr, "ZEND_MM_SEG_SIZE must be a power of two \ n ");
  34. Exit (255 );
  35. } Else if (seg_size <ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE ){
  36. Fprintf (stderr, "ZEND_MM_SEG_SIZE is too small \ n ");
  37. Exit (255 );
  38.      }    
  39. } Else {
  40. Seg_size = ZEND_MM_SEG_SIZE;
  41.     }    
  42.      
  43. //... The code is omitted
  44. }    

1,121st ~ Row 3 traverses the entire mem_handlers array and confirms the memory allocation scheme. If the ZEND_MM_MEM_TYPE variable is not set, the malloc scheme is used by default. If it is windows (ZEND_WIN32), the win32 scheme is used by default, if the ZEND_MM_MEM_TYPE variable is set, the setting scheme is used.

1,140th ~ Row 1152 confirms the size of the segment allocation. If the ZEND_MM_SEG_SIZE variable is set, the set size is used. Here, the set size is determined to be a multiple of 2 and greater than or equal to limit + ZEND_MM_ALIGNED_HEADER_SIZE; if no value is set, the default ZEND_MM_SEG_SIZE is not used.

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.