Apache Array Analysis

Source: Internet
Author: User
3.1 Arrays 3.1.1 Array OverviewThe array is the most often and most common data structure in Apache, although the C language has provided a certain array of functions, but the C language array used in Apache still has some of the following problems: (1), C language array in the definition of the time must determine the number of dimensions, once determined, its length can not be changed. However, many things in Apache do not know the maximum number of the array can be saved, if the predefined is too large, you may waste too much space, the definition is too small and may not meet the system requirements, so Apache needs a dynamic elastic scaling capability of the array, In this way, the actual number of elements allocated to allocate how many elements, (2), C provides another array of dynamic expansion of the scheme, that is, using malloc allocation space, the space as an array for processing. However, only one type of data can be saved for the space allocated by malloc.         If you need to save the remaining types, you need to reassign them. The array implementations in Apache are similar to vectors in STL or Java, whose lengths can remain dynamic and can hold different types of data, including basic data types to composite data types. 3.1.2 Array Structure         The associated array structure in Apache is defined in the file apr_table.h and apr_table.c. The apr_array_header_t structure is the core structure of Apache, which is defined as follows:         struct apr_array_header_t {         apr_pool_t *pool;         int elt_size;         int nelts;         int nalloc;         Char *elts;        }; The structure diagram of the         array can be represented by the following diagram:                       Figure 2.1 Apache array Map      The ELTs pointer in the     apr_array_header_t structure points to the actual space allocated with malloc, with the size of each space being elt_size. Because the number of array elements can be changed dynamically, the structure uses NALLOC to record the number of elements in an array that is currently allocated. In addition, because the array elements in Apache are not all used, it is also necessary to nelts record the number of elements currently in use, and the number of elements that can be used for the natural remainder of the array is nalloc-nelts. All the space required to allocate an array is obtained from the pool of memory pools. 3.1.3 Creating an array The creation of         arrays is implemented by calling Apr_array_make, but its internal main work is implemented by Make_array_core:          static void Make_array_core (apr_array_header_t *res, apr_pool_t *p,int nelts, int elt_ size, int clear)         {             if (Nelts < 1) {                 nelts = 1;            }              if (clear) {                 res->elts = Apr_pcalloc (P, nelts * elt_size);            }              else {                 Res->elts = Apr_palloc (P, nelts * elt_size);            }              res->pool = p;             res->elt_size = elt_size;             res->nelts = 0;     */No Active elements yet ... */            Res->nalloc = nelts;& nbsp;   /* ... but this many allocated */       }          Apr_declare (apr_array_header_t *) apr_array_make (apr_pool_t *p,int nelts, int elt_size )         {            apr_array_header_t *res;             res = (apr_array_header_t *) APr_palloc (p, sizeof (apr_array_header_t));             Make_array_core (res, p, Nelts, elt_size, 1);             return res;        }         Apr_array_ The Make function is responsible primarily for allocating apr_array_head_t header structures, while Make_array_core is primarily responsible for creating and initializing res element arrays. The size of each element in the RES array is elt_size and the total number of elements is nelts, so the total allocated space is nelts*elt_size, and the space allocation continues after the members in the apr_array_header_t structure are initialized. If clear is 1, call Apr_pcalloc to allocate, otherwise call Apr_palloc, the only difference is that Apr_pcalloc will initialize the allocated space 0, and Apr_palloc will not initialize.         In addition to creating a new array from scratch, the APR allows you to copy an array to a new array using apr_array_copy:          Apr_declare (apr_array_header_t *) apr_array_copy (apr_pool_t *p,const Apr_array_ header_t *arr)         arr is the source array that needs to be copied, and the copied array is returned by the function. The copy function internally first defines a arp_array_header_t variable res, allocates enough space for it from the memory pool p, and then calls Make_array_core to generate an array of the same size as the ARR array, and then calls memcpyA copy of the memory. The space is then returned. 3.1.4 element Press-in and eject         Apache operations on array elements are similar to stacks, allowing only pop-up (pop) and push-in, so you can't arbitrarily access the elements in the arrays as you would in C.         when the data needs to be saved in the array, the function Apr_array_push function, its prototype declaration is as follows:          apr_declare (void *) Apr_array_push (apr_array_header_t *arr)          arr is an element that needs to be saved to an array. The preservation of elements is stored in order from left to right, that is, from low index to high index, and it is not possible to randomly save in C, so the array is called a stack more appropriate from this point of view.         before the element is pressed into the array, the function first checks to see if there is free space available in the array, and if there is free space remaining, it will directly copy the element to the position of the nelts+1 array. Adjusts nelts to nelts++ and returns the element. If not, that means that the nalloc space in the current data is already in use, i.e. Nelts=nalloc, then the array space must be enlarged to hold the pressed data. However, the increase in array space does not require several additions, but follows the bulk increment principle:         If Nalloc is 0 before the data is pressed, that is, no space has been allocated. At this point, only space of size elt_size is created, that is, the space of an element.         if the current element is pressed in, the system-assigned NALLOC units are already in use, then the space will be expanded directly to the nalloc*2 element space at a time.         once these spaces are allocated, the first available free space is the nalloc+1 element. Simply copy the elements that need to be pressed into the address space, then add a elt_size after the copy, andReturns the address of the new element.         similar to Apr_array_push, Apache provides another version of the press-in function Apr_array_push_noclear, This function is basically similar to the function of the Apr_array_push function, whose only difference is the function name, which is "noclear".         Apr_array_push calls Memset to clear 0 of new blocks of memory before generating new free blocks, pressing into new elements, and "Noclear" function eliminates this step.         is matched with the element indentation, and the element is taken from the array through the function Apr_array_pop, whose function is declared as follows:          apr_declare (void *) Apr_array_pop (apr_array_header_t *arr)        The   element popup follows the FIFO principle, so the element that is ejected is always the first nelts, unless the current array is empty and there is no valid data. The return statement can be simplified to return arr->elts + (Arr->elt_size * (--arr->nelts)); 3.1.5 Array Merge         Apache uses Apr_array_cat to merge two elements, the function is defined as follows: Apr_declare (void) Apr_array_ Cat (apr_array_header_t *dst,const apr_array_header_t *src)         The function adds the array src to the end of the array DST. The function first checks whether the current idle cell can tolerate the SRC array, the concept of which is not to accommodate the nalloc element in SRC, but merely to accommodate the nelts non-idle unit in SRC, not including the Nalloc-nelts idle unit, so the function needs to be compared ( Dst->nalloc)-(dst->nelts) >= (src->nelts), not (Dst->nalloc)-(dst->nelts) >= (Src->nalloc). If it can be accommodated, then there is no need to allocate new space, direct memory copy on it.         If all idle empty cells in DST are not enough to hold the free cells in SRC, then there is no doubt that DST needs to allocate new space, and the allocation algorithm is as follows:          1), if the number of elements in DST is zero, a new space is generated.         2), if the number of elements in DST Nalloc nonzero, the first nalloc*2 of free space is generated.         3), however, if SRC has too many non-idle elements, and DST itself has a small amount of free space, then even one generation of          Nalloc a free block will not necessarily be able to hold the elements in SRC. The only way to do this is to keep generating new free blocks, each time the total number of blocks is the current one times, until the total number of free blocks can accommodate non-idle elements in SRC. This is really what the following code does: &NBSP;&NBSP;&NBSP;&NBsp;    if (dst->nelts + src->nelts > Dst->nalloc) {         int new_size = (dst->nalloc <= 0)? 1:dst->nalloc * 2;         while (dst->nelts + src->nelts > new_size) {         new_size *= 2;        }        }          Once you determine the total number of free blocks that need to be generated, the function will apply one time from the memory pool Dst->pool. The data in SRC is then copied into space.
About the author
Zhang Zhengqing, the main research direction at present is embedded browser, mobile middleware and large-scale server design. Apache is currently in the source code analysis, planning to publish the Apache source of the Panorama analysis of the book. The Apache series is a draft part of this book, and friends interested in Apache can contact them through flydish1234 at sina.com.cn!
If you think this article is good, please click "recommend this article" link after the text.

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.