Nginx Source parsing Memory pool

Source: Internet
Author: User

Nginx itself implements the memory pool, and all memory allocations are based on the memory pool. The basic idea is to pre-apply a memory space, lower than the specified size of memory (small segment of memory) directly from the memory pool request, more than the specified size of memory (large segment of memory) directly call malloc request. The relevant code is in Os/unix/ngx_alloc. {c,h} and Core/ngx_palloc. {c,h}.

Os/unix/ngx_alloc. The {c,h} file encapsulates a system call for memory allocation, where:

    • Ngx_alloc call malloc to request a memory space
    • Ngx_calloc calls malloc to request a memory space, and then calls Memset to initialize the space to 0
    • Ngx_memalign call Memalign/posix_memalign Request a space for memory address alignment

Core/ngx_palloc. {C,H} contains the main code for the Nginx memory pool implementation, first look at the relevant data structure of the memory pool:

//Memory pool data areatypedef struct{U_char *last;//Memory pool free space start addressU_char *end;//Memory pool End Addressngx_pool_t *next; Ngx_uint_t failed;//Attempt to allocate failed times} ngx_pool_data_t;typedef structNgx_pool_large_s ngx_pool_large_t;structngx_pool_large_s {ngx_pool_large_t *next;void*alloc;//Large segment memory space, calling malloc request};structngx_pool_s {ngx_pool_data_t D; size_t Max;//Memory pool allocation space (no more than page_size-1)ngx_pool_t *current;//Current memory poolNgx_chain_t *chain; ngx_pool_large_t *large;//Large segment memory linked listngx_pool_cleanup_t *cleanup; ngx_log_t *Log;};

Converting to a data structure diagram may be clearer:

Note that the current pointer in the figure is pointing to a memory pool where the current pointer is actually pointing.

View the code in the ngx_palloc.c file in turn:

    • The Ngx_create_poll function is used to create a size-sized memory pool
ngx_pool_t  *p;// 申请NGX_POLL_ALIGNMENT对齐的内存空间p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log);// 实际可分配的空间为size-sizeof(ngx_poll_t)p->d.last = (u_char *) p + sizeof(ngx_pool_t);p->d.end = (u_char *) p + size;size = size - sizeof(ngx_pool_t);p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL;...
    • The Ngx_destroy_poll function is used to destroy the memory pool, destroying the memory in the cleanup list (the data structure is defined as follows: Call the handler function to destroy the data space), destroy the large segment of memory (large linked list), and finally destroy Ngx_poll_ T linked list.
typedefvoid (*ngx_pool_cleanup_pt)(void *data);typedefstruct ngx_pool_cleanup_s  ngx_pool_cleanup_t;struct ngx_pool_cleanup_s {    ngx_pool_cleanup_pt   handler;    void                 *data;    ngx_pool_cleanup_t   *next;};
    • The Ngx_reset_pool function is used to reset the memory pool (destroying large memory spaces, resetting the memory pool fields, such as the last field set to P+sizeof (ngx_poll_t))
    • The NGX_PALLOC function requests a space from the memory pool and returns the address
void *Ngx_palloc (ngx_pool_t*Pool, size_t size) {U_char*M ngx_pool_t*P//application size not greater than memory pool space size    if(Size<=Pool -Max) {P=Pool -Current Do{//Calculates the address after the last address is alignedM=Ngx_align_ptr (P -D.Last, ngx_alignment);///free space exceeds size to return directly            if((size_t) (p -D.End-M>=Size) {P -D.Last=M+SizereturnM } p=P -D.Next } while(p);//re-request memory pool        returnNgx_palloc_block (pool, size); }//apply for large segment of memory    returnNgx_palloc_large (pool, size);}
    • The Ngx_pnalloc function is the same as the Ngx_palloc function, except that the last-aligned address is missing, that is, the address that Ngx_palloc returns must be a memory address aligned, and ngx_pnalloc not necessarily.
 do  {
    
     //the only place different from Ngx_palloc  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); 
     
    • The Ngx_palloc_block function is called in both Ngx_palloc and Ngx_pnalloc to re-request the memory pool and return the aligned address space
Staticvoid *Ngx_palloc_block (ngx_pool_t*Pool, size_t size) {U_char*M    size_t psize; ngx_pool_t*P*New,*Current//Calculate current memory pool allocation sizePsize=(size_t) (Pool -D.End-(U_char*) pool);//Apply memory address to align the spaceM=Ngx_memalign (Ngx_pool_alignment, Psize, POOL -Log);if(M== NULL) {return NULL; }//initialization work, similar to Ngx_create_poll    New =(ngx_pool_t*) m;New -D.End=M+PsizeNew -D.Next= NULL;New -D.Failed= 0; M+=sizeof (ngx_pool_data_t); M=Ngx_align_ptr (M, ngx_alignment);New -D.Last=M+Size//Reset the current pointer and place the memory pool you just requested at the end of the listCurrent=Pool -Current For (p=Current P -D.Next P=P -D.Next) {if(p -D.Failed++ > 4) {Current=P -D.Next }} P -D.Next= New; Pool -Current=Current?Current:New;returnm;}
    • Ngx_palloc_large is used to request a large amount of memory space (larger than the memory pool space)
Staticvoid *Ngx_palloc_large (ngx_pool_t*Pool, size_t size) {void              *P    ngx_uint_t N; ngx_pool_large_t*Large//Call malloc application spaceP=Ngx_alloc (Size, pool -Log);if(p== NULL) {return NULL; } n= 0;//Try to put the requested space in the large list (try only 4 times?) )For (large=Pool -Large Large Large=Large -Next) {if(Large -Alloc== NULL) {Large -Alloc=PreturnP }if(n++ > 3) {break; }    }//Apply ngx_pool_large_t data structure, initialize alloc to point to the application of large memory, put it into the large chain headerLarge=Ngx_palloc (pool, sizeof (ngx_pool_large_t));if(Large== NULL) {Ngx_free (P);return NULL; } Large -Alloc=P Large -Next=Pool -Large Pool -Large=LargereturnP;}
    • The Ngx_pmemalign function is used to request a large space of memory address alignment
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);if(p== NULL) {return NULL; } Large=Ngx_palloc (pool, sizeof (ngx_pool_large_t));if(Large== NULL) {Ngx_free (P);return NULL; } Large -Alloc=P Large -Next=Pool -Large Pool -Large=LargereturnP;}
    • The Ngx_pfree function is used to release the specified memory space (for large spaces)
Ngx_int_tngx_pfree (ngx_pool_t*Poolvoid *P) {ngx_pool_large_t*L//Traverse large linked listfor (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;returnNGX_OK; }    }returnngx_declined;}
    • The Ngx_pcalloc function initializes this space to 0 based on the Ngx_palloc.

The Ngx_pool_cleanup_add, Ngx_pool_run_cleanup_file, Ngx_pool_cleanup_file, and Ngx_pool_delete_file functions temporarily do not parse them. The above is the Nginx memory pool implementation of all, this code is based on nginx1.7.0, under the CentOS6.0 to complete the compilation.

Nginx Source parsing Memory pool

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.