Memory Pool functions:
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 chunk size memory if (p = = null) in 16-byte alignment {return null; } P->d.last = (U_char *) p + sizeof (ngx_pool_t); Last point to Data section start (skip header ngx_pool_t) structure p->d.end = (U_char *) p + size; End points to the end of the structure p->d.next = NULL; Next is the management pointer to the small block memory list p->d.failed = 0; This block memory allocation failure number is size = size-sizeof (ngx_pool_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;//Maximum memory pool node size is 4095 p->current = p; Current points to P p->chain = NULL; P->large = NULL; P->cleanup = NULL; P->log = log; return p; }
Where the initialization structure is as follows:
The Green line represents a struct member, such as Ngx_pool_data_t's members include last, end, next, failed
The black line indicates the address pointed to by the pointer
1 Max selects the smallest between creation 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 allocates space from the memory pool starting from D.last, with the allocation of memory pool space, the point of this field will move back,
Memory Pool allocation
void * Ngx_palloc (ngx_pool_t *pool, size_t size) { U_char *m; ngx_pool_t *p; If the requested memory size is less than the size of the created memory pool node (after removing ngx_pool_t of course) if (size <= pool->max) { p = pool->current; do { m = ngx_align_ptr (P->d.last, ngx_alignment);//First Align 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 () attempts to allocate a size space from the pool in two different cases.
First, if the allocated size is less than ngx_pool_t.max (small allocation, or code 127-140), you first need to align the last 16 bits, and if the request still has space, then use this memory node (that is, code 130) to move the last point to the assigned address. At the same time, the requested memory is returned to the call, if there is no space to allocate from the current traversal of the small memory node (that is, traversing the list until the memory pool node next is empty). You need to allocate a memory pool node call Ngx_palloc_block (Pool,size)
This function mainly allocates a memory pool node, and then inserts the node into the tail of the list, while updating the pool->current pointer, updating the algorithm is to traverse the list, if it is found that the node has been allocated a failure 6 times, the current refers to the next node, If all nodes fail more than 6 times, current points to the new allocation 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 new node pool->current = current? current:new;//Update
In the second case, allocating large chunks of memory directly to the malloc allocation, allocating, traversing the pool->large linked list, if there is a large pool of memory nodes exist Alloc is empty, then the newly allocated memory to the node, if there is no such node, then the allocation of the insertion p-> Large the list head.
Clear callback function
Each of these nodes is
struct ngx_pool_cleanup_s { ngx_pool_cleanup_pt handler;//callback handler void *data;//callback function parameter ngx_ pool_cleanup_t *next;//refers to a downward node };
ngx_pool_cleanup_t * ngx_pool_cleanup_add (ngx_pool_t *p, size_t size) { ngx_pool_cleanup_t *c; c = Ngx_palloc (p, sizeof (ngx_pool_cleanup_t));//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 a linked list header p->cleanup = c;//Insert list head ngx_log_debug1 (Ngx_log_debug_alloc, p-> Log, 0, "Add Cleanup:%p", c); return c; }
The above describes the [Nginx source Analysis]NGX memory pool implementation, including the aspects of the content, I hope that the PHP tutorial interested friends helpful.