Nginx advanced data structure Source Code Analysis (4) ----- memory pool

Source: Internet
Author: User
: This article mainly introduces Nginx advanced data structure Source Code Analysis (4) ----- memory pool. For more information about PHP tutorials, see. The use of the memory pool brings many benefits to Nginx, such as convenient memory usage, simplified logic code, and improved program performance.

Several key knowledge points are listed as follows:

(1) when the ngx_palloc () function tries to allocate the size of memory from the memory, there are two situations: one is that the size is smaller than pool-> max, which is called small memory allocation, if the current memory pool node is smaller than the size, apply for a new memory pool node of the same size, and then allocate the size of memory space from the new memory pool node. If the size is greater than pool-> max, a large block of memory is allocated. the called function directly applies for a large block of memory from the operating system.

(2) the application for small memory blocks is inserted at the end of the linked list, while the new large memory blocks are inserted at the front of the linked list.

(3) Nginx only releases large blocks of memory and does not release small blocks of memory. This means that the memory allocated from the memory pool will not be returned to the memory pool, the memory will be recycled to the system memory only when the entire memory pool is destroyed.

(4) current field in ngx_pool_t: This field records the start node for memory allocation from the memory pool in the descending order. Nginx specifies that when a memory node fails to be allocated more than or equal to 6 times, current points to the next memory node.

(5) Why limit the maximum value of the pool-> max field to one page of memory, which is also the critical between small memory and large memory, the reason is that the cache is necessary only when the allocated space is smaller than one page. Otherwise, it is better to directly apply to the operating system using the system interface malloc.

Struct definitions:

// Large memory management structure struct ngx_pool_large_s {ngx_pool_large_t * next; // connect to the next large memory management void * alloc; // The requested large memory address }; // typedef struct {u_char * last; // start address of available memory u_char * end; // ngx_pool_t * next address of available memory * next; // point to the next memory pool node ngx_uint_t failed; // number of failed requests} ngx_pool_data_t; // memory pool struct ngx_pool_s {ngx_pool_data_t d; // store data size_t max; // Available memory size for data storage, up to 1 page ngx_pool_t * current; // point to the memory pool ngx_chain_t * chain for memory allocation; ngx_pool_large_t * large; // connect to the large memory management structure ngx_pool_cleanup_t * cleanup; // clear the object header ngx_log_t * log ;};
Memory pool initialization:

// Create a size memory pool 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 ); // Apply for size byte memory in alignment mode if (p = NULL) {return NULL;} p-> d. last = (u_char *) p + sizeof (ngx_pool_t); // point to the available memory start address p-> d. end = (u_char *) p + size; // point to the end address of available memory p-> d. next = NULL; // initially, the next available memory is NULL p-> d. failed = 0; // This memory application fails for zero times size = size-sizeof (ngx_pool_t); // real <G id = "1"> max = (size <NGX_MAX_ALLOC_FROM_POOL )? Size: NGX_MAX_ALLOC_FROM_POOL; // The maximum size is only one page. p-> current = p; // point to the memory pool in which memory is being allocated. p-> chain = NULL; p-> large = NULL; p-> cleanup = NULL; p-> log = log; return p ;}
Memory pool destruction and resetting:

// Destroy the memory pool void ngx_destroy_pool (ngx_pool_t * pool) {ngx_pool_t * p, * n; ngx_pool_large_t * l; ngx_pool_cleanup_t * c; // run the handler for (c = pool-> cleanup; c = c-> next) {if (c-> handler) {ngx_log_debug1 (NGX_LOG_DEBUG_ALLOC, pool-> log, 0, "run cleanup: % p", c); c-> handler (c-> data );}} // release large memory for (l = pool-> large; l = l-> next) {ngx_log_debug1 (NGX_LOG_DEBUG_ALLOC, pool-> log, 0, "free: % p ", l-> alloc); if (l-> alloc) {ngx_free (l-> alloc ); // use free to release the memory requested by malloc.} # if (NGX_DEBUG) /** we cocould allocate the pool-> log from this pool * so we cannot use this log while free () ing the pool */for (p = pool, n = pool-> d. next;/* void */; p = n, n = n-> d. next) {ngx_log_debug2 (NGX_LOG_DEBUG_ALLOC, pool-> log, 0, "free: % p, unused: % uz", p, p-> d. end-p-> d. last); if (n = NULL) {break ;}# endif // release each applied memory pool object ngx_pool_t for (p = pool, n = pool-> d. next;/* void */; p = n, n = n-> d. next) {ngx_free (p); if (n = NULL) {break ;}}// reset the memory pool void ngx_reset_pool (ngx_pool_t * pool) {ngx_pool_t * p; ngx_pool_large_t * l; // release large memory for (l = pool-> large; l = l-> next) {if (l-> alloc) {ngx_free (l-> alloc) ;}// memory pool object, only change the last pointer position for (p = pool; p = p-> d. next) {p-> d. last = (u_char *) p + sizeof (ngx_pool_t); // causes the starting address offset of available memory for all memory pool objects to be the same p-> d. failed = 0;} pool-> current = pool; pool-> chain = NULL; pool-> large = NULL ;}
Memory allocation:

// Allocate memory (address alignment) void * ngx_palloc (ngx_pool_t * pool, size_t size) {u_char * m; ngx_pool_t * p; if (size <= pool-> max) {// when applying for small memory, use size as the standard p = pool-> current; do {m = ngx_align_ptr (p-> d. last, NGX_ALIGNMENT); // first set d. last address alignment if (size_t) (p-> d. end-m)> = size) {// the available memory is greater than the memory to be applied p-> d. last = m + size; // directly update d. last return m; // directly return} p = p-> d. next; // Otherwise, find the next available memory pool object} while (p); // if not found, apply for a new memory pool object return ngx_palloc_block (pool, size );} return ngx_palloc_large (pool, size); // Large memory application processing} // allocate memory (address can be not aligned) void * ngx_pnalloc (ngx_pool_t * pool, size_t size) {u_char * m; ngx_pool_t * p; if (size <= pool-> max) {// small memory p = pool-> current; do {m = p-> d. last; 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); // apply for a new memory pool object} return ngx_palloc_large (pool, size); // Large memory}
Small block memory allocation:

// Apply for a new memory pool object static void * ngx_palloc_block (ngx_pool_t * pool, size_t size) {u_char * m; size_t psize; ngx_pool_t * p, * new; psize = (size_t) (pool-> d. end-(u_char *) pool); // The total size of the applied memory m = ngx_memalign (NGX_POOL_ALIGNMENT, psize, pool-> log ); // Apply for memory alignment if (m = NULL) {return NULL;} new = (ngx_pool_t *) m; // new memory new-> d. end = m + psize; // The last available memory address new-> d. next = NULL; new-> d. failed = 0; m + = sizeof (ngx_pool_data_t); // only one worker saves other ngx_pool_t overhead m = ngx_align_ptr (m, NGX_ALIGNMENT); new-> d. last = m + size; // the starting address of available memory // if the number of times the applied memory has failed 5 times, 6th times, current will point to the new memory pool object for (p = pool-> current; p-> d. next; p = p-> d. next) {if (p-> d. failed ++> 4) {pool-> current = p-> d. next;} p-> d. next = new; // connect to the newly applied memory pool object return m ;}
Large memory allocation

// Large memory application processing static void * ngx_palloc_large (ngx_pool_t * pool, size_t size) {void * p; ngx_uint_t n; ngx_pool_large_t * large; p = ngx_alloc (size, pool-> log); // malloc directly applies for memory if (p = NULL) {return NULL;} n = 0; for (large = pool-> large; large = large-> next) {if (large-> alloc = NULL) {// if memory is released, you can reuse large-> alloc = p; return p;} if (n ++> 3) {// However, it is only found 4 times, 5th times directly break, and a large memory management structure break is created ;}} large = ngx_palloc (pool, sizeof (ngx_pool_large_t); // Apply for memory if (large = NULL) {ngx_free (p); return NULL from the memory pool object ;} large-> alloc = p; // point to the applied large memory // Insert the large header large-> next = pool-> large; pool-> large = large; return p ;}
Direct memory allocation:

// Regardless of the memory size, apply to the operating system for the memory void * ngx_pmemalign (ngx_pool_t * pool, size_t size, size_t alignment) {void * p; ngx_pool_large_t * large; p = ngx_memalign (alignment, size, pool-> log); // applied memory if (p = NULL) {return NULL;} large = ngx_palloc (pool, sizeof (ngx_pool_large_t); // apply for a large memory management structure if (large = NULL) {ngx_free (p); return NULL ;} // put it into the memory pool ngx_pool_t to manage large-> alloc = p; // point to the applied memory // Insert it to the header large-> next = pool-> large; pool-> large = large; return p ;}
Release memory:

// Release the memory ngx_int_t ngx_pfree (ngx_pool_t * pool, void * p) {ngx_pool_large_t * l; // only release the large memory for (l = pool-> large; l; l = l-> next) {if (p = l-> alloc) {ngx_log_debug1 (NGX_LOG_DEBUG_ALLOC, pool-> log, 0, "free: % p ", l-> alloc); ngx_free (l-> alloc); l-> alloc = NULL; // return NGX_ OK;} return NGX_DECLINED ;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the Nginx advanced data structure Source Code Analysis (4) ----- memory pool, including content, hope to be helpful to friends interested in PHP tutorials.

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.