On PHP source 32: Emalloc/efree layer and heap layer in PHP memory pool

Source: Internet
Author: User
This article mainly introduces about PHP source 32: PHP memory Pool in the Emalloc/efree layer and heap (heap) layer, has a certain reference value, now share to everyone, the need for friends can refer to

On PHP source 32: Emalloc/efree layer and heap layer in PHP memory pool
The Emalloc/efree layer is the topmost structure in the entire memory system, and it uses PHP's own memory management mechanism through the exchange with the heap layer. If you have set Use_zend_alloc to 0, use functions such as malloc/free directly to manipulate memory directly.
This will parse the interaction of emalloc/efree layer and heap layer from the implementation of Emalloc and Efree two functions, and the management mechanism of heap layer for memory.

"Emalloc"
The Emalloc function starts with the ZEND_ALLOC.H 70 line.
Emalloc is a macro that corresponds to the _emalloc function.
In the _emalloc function, if the Zend memory management mechanism is not used, the malloc function is called directly, otherwise

_zend_mm_alloc_int[emalloc (), _emalloc (), _zend_mm_alloc_int ()

In the _zend_mm_alloc_int function, the program processes the actual required memory less than or greater than or equal to Zend_mm_max_small_size (272), and if it is less than zend_mm_max_small_size, it searches Free_ Buckets, to see if there is a suitable block of memory, if you can find the appropriate block in free_buckets to use, with the direct jump to Zend_mm_finished_searching_for_block, otherwise execute Zend_mm_search_ Large_block ()

[Emalloc (), _emalloc (), _zend_mm_alloc_int (), Zend_mm_search_large_block ()]
The Zend_mm_search_large_block function is used to find the appropriate block of memory in the large_free_buckets. Where the size of Zend_mm_large_bucket_index (true_size) is not found, you need to look for the smallest chunk in the larger list.

If you do not have a chunk list and a small block list, you will need to find it from the remaining list block and jump to Zend_mm_finished_searching_for_block if found.
If none of the three lists are found, you will need to increase the memory allocation again. At this point the allocation function of the storage layer is called to allocate, where the size of the memory, if you need to allocate more memory than block_size, you need to recalculate according to the size, otherwise directly allocated block_size size of memory.
After allocating memory, the heap needs to be re-organized, and the memory size in the heap needs to be recalculated, adding the newly allocated memory to the front of the segments_list.

If you jump directly to Zend_mm_finished_searching_for_block in the above operation, you will need to remove the used memory block from the corresponding list (this should be a marked procedure, pseudo-removal)

Next, move it to the free list or to the remaining list, depending on the amount of memory left.

Finally, the allocated block is returned.

There are a few points to note during the Emalloc process.
Zend_mm_bucket_index (true_size) locates the position in the BUCKET with a value greater than or equal to 0, less than 32.
The implementation is as follows:

#define ZEND_MM_BUCKET_INDEX (True_size) ((TRUE_SIZE>>ZEND_MM_ALIGNMENT_LOG2)-(Zend_mm_aligned_min_header_ SIZE>>ZEND_MM_ALIGNMENT_LOG2))

The values for both Free_bitmap and Large_free_bitmap are 0 to 31.

"Efree"
The Efree function starts with the Zend_alloc.h 72 line.
Efree is a macro that corresponds to the _efree function.
In the _efree function, if you do not use Zend's memory management mechanism, call the free function directly, otherwise call _

Zend_mm_free_int[efree (), _efree (), _zend_mm_free_int ()

The heap first reduces the size of the entire heap, and if the latter block of the current block is an idle block, the latter free block is removed from the list of free blocks and merged with the current block, if the previous block of the current block is a free block, the previous free block is removed from the list of free blocks and merged with the current block, and the pointer points If the current block is the starting block at this point, call zend_mm_del_segment to clear the whole block of memory, and if it is not the start block, add the merged blocks to the free block list.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.