Cainiao nginx source code analysis data structure (I) Dynamic Arrays ngx_array_t and nginxngx_array_t

Source: Internet
Author: User

Cainiao nginx source code analysis data structure (I) Dynamic Arrays ngx_array_t and nginxngx_array_t

 

Cainiao nginx source code analysis data structure (I) Dynamic Array ngx_array_t

 

  • Author: Echo Chen (Chen Bin)

  • Email: chenb19870707@gmail.com

  • Blog: Blog.csdn.net/chen19870707

  • Date: October 20 h, 2014

     

    1. Advantages and features of ngx_array

     

    Ngx_array _ t is an ordered container that dynamically changes the size of an array when the array capacity is reached. Similar to the vector in STL, ngx_array _ t has the following features:

    • Direct Index of subscript, fast access
    • Dynamic Growth
    • The memory allocated by the slab Memory Pool is centrally managed, with High Efficiency
    2. Source Code Location

     

    Header file: http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_array.h

    Source File: http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_array.c

     

    3. data structure definition

     

    Typedef struct {void * elts; // elts points to the first address of the array ngx_uint_t nelts; // nelts is the number of elements used in the array size_t size; // memory size occupied by each array element ngx_uint_t nalloc; // The total size ngx_pool_t * pool of the number of Yuan in the current array; // memory pool object} ngx_array_t;

    The structure is shown in:

    4. Create a dynamic array ngx_array_create and initialize ngx_array_init

     

     

    // P indicates the memory pool, and n indicates the number of elements initially allocated. size indicates the memory size occupied by each element.

     

    Ngx_array_t * ngx_array_create (optional * p, ngx_uint_t n, size_t size) {ngx_array_t * a; // assign a dynamic array pointer a = ngx_palloc (p, sizeof (ngx_array_t )); if (a = NULL) {return NULL;} if (ngx_array_init (a, p, n, size )! = NGX_ OK) {return NULL;} return a;} static ngx_inline ngx_int_tngx_array_init (ngx_array_t * array, ngx_pool_t * pool, ngx_uint_t n, size_t size) {/** set "array-> nelts" before "array-> elts ", otherwise MSVC thinks * that "array-> nelts" may be used without having been initialized * // initialize the data array-> nelts = 0; array-> size = size; array-> nalloc = n; array-> pool = pool; // allocates n memory sizes. elts points to the first address array-> elts = ngx_palloc (pool, n * size); if (array-> elts = NULL) {return NGX_ERROR;} return NGX_ OK ;}

    5. Release ngx_array_destroy from the dynamic array

     

    Voidngx_array_destroy (ngx_array_t * a) {ngx_pool_t * p; p = a-> pool; // release the dynamic array. if (u_char *) a-> elts + a-> size * a-> nalloc = p-> d. last) {p-> d. last-= a-> size * a-> nalloc;} // release the first pointer of the dynamic array if (u_char *) a + sizeof (ngx_array_t) = p-> d. last) {p-> d. last = (u_char *) ;}}

    Here we will use the release operation of the memory pool, which will be explained in detail later. Here we will use it as release. 6. Add element operations for dynamic arrays ngx_array_push and ngx_array_push_n 1. ngx_array_push

     

    // A is the void * ngx_array_push (ngx_array_t * a) {void * elt, * new; size_t size; ngx_pool_t * p; // use the same number as the pre-allocated number. The array is full if (a-> nelts = a-> nalloc) {/* the array is full * // pre-allocated nalloc files. Now there are 2 * nalloc files with size = a-> size * a-> nalloc; p = a-> pool; // if the memory pool memory is sufficient, it is allocated directly from the memory pool and only one if (u_char *) is allocated *) a-> elts + size = p-> d. last & p-> d. last + a-> size <= p-> d. end) {/** the array allocation is the last in the pool * and there is space for new allocation * // the end pointer of the memory pool moves the size of an element and allocates one element of memory, and put nalloc + 1 p-> d. last + = a-> size; a-> nalloc ++; // if the memory pool is insufficient, allocate a new array, nalloc} else {/* allocate a new array * // memory allocation new = ngx_palloc (p, 2 * size); if (new = NULL) {return NULL;} // copy the previous array to the new array, and set the array size to twice the previous ngx_memcpy (new, a-> elts, size ); a-> elts = new; a-> nalloc * = 2;} // Number of allocated instances + 1, and the returned elt = (u_char *) a-> elts + a-> size * a-> nelts; a-> nelts ++; return elt ;}

    It can be seen that when the memory pool has space, only one element is added after the array is full. When the memory pool does not allocate space, 2 * nalloc values are allocated directly, with the memory pool, it is more effective than vector direct 2n + 1.
    2. ngx_array_push_n

     

    // A is the array to be placed, and n is the number of void * ngx_array_push_n (ngx_array_t * a, ngx_uint_t n) {void * elt, * new; size_t size; ngx_uint_t nalloc; ngx_pool_t * p; size = n * a-> size; // if the remaining number of arrays is insufficient, assign if (a-> nelts + n> a-> nalloc) {/* the array is full */p = a-> pool; // if the remaining n elements in the memory pool are sufficient, allocate if (u_char *) from the memory pool *) a-> elts + a-> size * a-> nalloc = p-> d. last & p-> d. last + size <= p-> d. end) {/** the array allocation is the last in The pool * and there is space for new allocation */p-> d. last + = size; a-> nalloc + = n; // if there are not enough remaining elements in the memory pool} else {/* allocate a new array * // when n is greater than the number of pre-allocated arrays nalloc, 2n elements are allocated, otherwise 2 * nalloc = 2 * (n> = a-> nalloc )? N: a-> nalloc); new = ngx_palloc (p, nalloc * a-> size); if (new = NULL) {return NULL;} // copy the previous element, set nalloc ngx_memcpy (new, a-> elts, a-> nelts * a-> size); a-> elts = new; a-> nalloc = nalloc ;}} // Add the allocated quantity and return elt = (u_char *) a-> elts + a-> size * a-> nelts; a-> nelts + = n; return elt ;}

    7. Practice

     

    The main significance of researching open source code is to understand the design principles and applicable scenarios, and use code in appropriate scenarios. If code is simply analyzed, it cannot be used, certainly cannot achieve the purpose of learning. Here we will provide the ngx_array test code and hope to do more.

       1: typedef struct
       2: {    
       3:     u_char *name;    
       4:     int age;
       5: }Student;
       6:  
       7: ngx_array_t* pArray = ngx_array_create(cf->pool,1,sizeof(Student));
       8:  
       9: Student *pStudent = ngx_array_push(pArray);
      10: pStudent->age = 10;
      11:  
      12: Students *pStudents  = ngx_array_push_n(pArray,3);
      13: pStudents->age = 1;
      14: (pStudents  + 1 )->age =2;
      15: (pStudents  + 2 )->age = 3; 
      16:  
    17: // traverse
      18: Student *pStudent = pArray->elts;
      19: ngx_uint_i = 0;
      20: for(; i < pArray->nelts;i++)
      21: {
      22:     a = pStudent  + i;
      23:     //....
      24: }
     

     

    -

    Echo Chen: Blog.csdn.net/chen19870707

    -


  • How to add a dynamic array to a class? Is an element a pointer to a struct? It is better to have source code examples

    The use of STL container vector implementation is quite simple, such:
    Struct MyStruct
    {
    }

    Class
    {
    Private:
    Vector <MyStruct *> myVector;

    }


    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.