


A brief discussion of PHP source code 32: emalloc/efree layer and heap layer in PHP memory pool
This article mainly introduces about PHP source code 32: emalloc/efree layer and heap layer in the PHP memory pool. It has certain reference value. Now I share it with you. Friends in need can Refer to
Brief Talk on PHP Source Code 32: emalloc/efree layer and heap layer in PHP memory pool
emalloc/efree layer is the top-level structure in the entire memory system. It is passed through Heap layer exchange uses PHP's own memory management mechanism. If USE_ZEND_ALLOC is set to 0, malloc/free and other functions can be used to directly operate the memory.
Here we will analyze the interaction between the emalloc/efree layer and the heap layer, and the memory management mechanism of the heap layer from the implementation of the emalloc and efree functions.
[emalloc]
The emalloc function starts from line 70 of zend_alloc.h.
emalloc is a macro, which corresponds to the _emalloc function.
In the _emalloc function, if zend's memory management mechanism is not used, the malloc function is called directly, otherwise it is called
_zend_mm_alloc_int [emalloc() -> _emalloc() -> _zend_mm_alloc_int() ]
In the _zend_mm_alloc_int function, the program will process whether the actual required memory is less than or greater than or equal to In the two cases of ZEND_MM_MAX_SMALL_SIZE (272), if it is less than ZEND_MM_MAX_SMALL_SIZE, free_buckets will be searched to see if there is a suitable memory block. If a suitable block can be found in free_buckets, jump directly to zend_mm_finished_searching_for_block, otherwise zend_mm_search_large_block()
[emalloc() -> _emalloc() -> _zend_mm_alloc_int() -> zend_mm_search_large_block()]
zend_mm_search_large_block function is used to find suitable memory blocks in large_free_buckets. When the ZEND_MM_LARGE_BUCKET_INDEX (true_size) size is not found, you need to find the smallest block in the larger block list.
If it is not found in both the large block list and the small block list, you need to search from the remaining list blocks. If it is found, it will also jump to zend_mm_finished_searching_for_block
If it is not found in the three lists, then Memory allocation needs to be increased again. At this time, the allocation function of the storage layer is called for allocation. The size of the memory. If the memory to be allocated is larger than block_size, it needs to be recalculated based on the size. Otherwise, memory of block_size size is allocated directly.
After the memory is allocated, the heap needs to be rearranged. At this time, the memory size in the heap needs to be recalculated and the newly allocated memory is added to the front of segments_list.
If you jump directly to zend_mm_finished_searching_for_block in the above operation, you need to remove the used memory block from the corresponding list (this should be a marking process, pseudo removal)
Next, depending on how much memory is left, move it to the free list or the remaining list.
Finally returns the allocated block.
During the entire process of emalloc, there are the following points to note.
ZEND_MM_BUCKET_INDEX(true_size) is located at the position in the bucket. This value is greater than or equal to 0 and 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 of free_bitmap and large_free_bitmap are both 0 to 31.
[efree]
The efree function starts from line 72 of zend_alloc.h.
efree is a macro, which corresponds to the _efree function.
In the _efree function, if the memory management mechanism of zend is not used, the free function is called directly, otherwise the _
zend_mm_free_int [efree() -> _efree() -> _zend_mm_free_int() ]
Heap first reduces the size of the entire heap. If the next block of the current block is a free block, then delete the next free block from the free block list and merge it with the current block. If the previous block of the current block is a free block, delete the previous free block from the free block list and merge it with the current block. , the pointer points to the previous free block. If the current block is the starting block at this time, call zend_mm_del_segment to clear the entire memory segment. If it is not the starting block, the merged block is added to the free block list.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
A brief discussion of PHP source code 31: Basics of the heap layer in the PHP memory pool
A brief discussion of PHP source code thirty: the storage layer in the PHP memory pool
A brief discussion of PHP source code twenty-nine: about the inheritance of interfaces
The above is the detailed content of A brief discussion of PHP source code 32: emalloc/efree layer and heap layer in PHP memory pool. For more information, please follow other related articles on the PHP Chinese website!

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
