: This article mainly introduces [nginx source code analysis] ngx memory pool implementation. if you are interested in the PHP Tutorial, please refer to it. Memory pool function:
ngx_create_poolngx_destroy_poolngx_reset_poolngx_pallocngx_pnallocngx_palloc_blockngx_palloc_largengx_pool_cleanup_add
Memory pool creation
Ngx_pool_t * ngx_create_pool (size_t size, ngx_log_t * log) {ngx_pool_t * p; p = ngx_memalign (NGX_POOL_ALIGNMENT, size, log ); // allocate a size of memory in 16-byte alignment if (p = NULL) {return NULL;} p-> d. last = (u_char *) p + sizeof (ngx_pool_t); // the structure where last points to the data section (skip header ngx_pool_t) p-> d. end = (u_char *) p + size; // end points to the end structure p-> d. next = NULL; // next is the management pointer to the small memory linked list p-> d. failed = 0; // Number of memory allocation failures for this block size = size-sizeof (ngx_po Ol_t); // remove the actual size of the ngx_pool_t memory pool p-> max = (size <NGX_MAX_ALLOC_FROM_POOL )? Size: NGX_MAX_ALLOC_FROM_POOL; // The maximum memory pool node size is 4095 p-> current = p; // current points to p-> chain = NULL; p-> large = NULL; p-> cleanup = NULL; p-> log = log; return p ;}
The initialization structure is as follows:
The green line indicates a struct member. for example, the ngx_pool_data_t members include last, end, next, and failed.
Black line indicates the address pointed to by the pointer
1 max selects the minimum value between the size-sizeof (ngx_pool_t) and 4095
2. log calls the ngx_log_t structure created by ngx_log_init in main (that is, the global ngx_log object)
Principle:
Nginx actually starts from the starting position of the space allocated by the memory pool from d. last. as the memory pool space is allocated externally, this field will be backward shifted,
Memory pool allocation
Void * ngx_palloc (ngx_pool_t * pool, size_t size) {u_char * m; ngx_pool_t * p; // if the applied memory size is smaller than the size of the created memory pool node (of course, after ngx_pool_t is removed) if (size <= pool-> max) {p = pool-> current; do {m = ngx_align_ptr (p-> d. last, NGX_ALIGNMENT); // first alignment the last pointer if (size_t) (p-> d. end-m)> = size) {p-> d. last = m + size; return m;} p = p-> d. next;} while (p); return ngx_palloc_block (pool, size);} return ngx_palloc_large (pool, size );}
Ngx_palloc () tries to allocate size space from the pool memory pool.
First, if the allocated size is smaller than ngx_pool_t.max (small block allocation, that is, code 127-140), you must first align the last 16 bits. if the application still has space, this memory node (code 130) is used to move the last point to the allocated address, and the requested memory is returned to the call, if no space can be allocated for traversing small memory nodes from current (that is, the next of the memory pool node is empty until the traversal link table ends ). You need to allocate memory pool nodes to call ngx_palloc_block (pool, size)
This function mainly allocates a memory pool node, inserts the node to the end of the linked list, and updates the pool-> current pointer. the update algorithm is a traversal table, if you find that the node has failed to be allocated six times, point current to the next node. If all nodes fail more than six times, current points to the new allocated node.
The core code is as follows:
Current = pool-> current; // initialize for (p = current; p-> d. next; p = p-> d. next) {// traverse if (p-> d. failed ++> 4) {// allocate more than 6 times current = p-> d. next;} p-> d. next = new; // insert a new node pool-> current = current? Current: new; // update current
In the second case, allocate a large block of memory and directly call malloc for allocation. After allocation, traverse the pool-> large linked list. if a large memory pool node exists, alloc is empty, then, the newly allocated memory will be mounted to this node. if such a node does not exist, it will be allocated and inserted into the head of the p-> large linked list.
Clear callback function
Each node is
Struct ngx_pool_cleanup_s {ngx_pool_cleanup_pt handler; // callback handler void * data; // callback function parameter ngx_pool_cleanup_t * next; // points to the next node };
Optional * ngx_pool_cleanup_add (ngx_pool_t * p, size_t size) {ngx_pool_cleanup_t * c; c = ngx_palloc (p, sizeof (bytes); // assign a node if (c = NULL) {return NULL;} if (size) {c-> data = ngx_palloc (p, size); if (c-> data = NULL) {return NULL ;}} else {c-> data = NULL;} c-> handler = NULL; c-> next = p-> cleanup; // Insert the chain table header p-> cleanup = c; // Insert the head of the linked list ngx_log_debug1 (NGX_LOG_DEBUG_ALLOC, p-> log, 0, "add cleanup: % p", c); return c ;}
The above introduces the [nginx source code analysis] ngx memory pool implementation, including some content, hope to be helpful to friends who are interested in PHP tutorials.