Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.12 ]​[ nginx-1.16.0 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 
0011 
0012 ngx_array_t *
0013 ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size)
0014 {
0015     ngx_array_t *a;
0016 
0017     a = ngx_palloc(p, sizeof(ngx_array_t));
0018     if (a == NULL) {
0019         return NULL;
0020     }
0021 
0022     if (ngx_array_init(a, p, n, size) != NGX_OK) {
0023         return NULL;
0024     }
0025 
0026     return a;
0027 }
0028 
0029 
0030 void
0031 ngx_array_destroy(ngx_array_t *a)
0032 {
0033     ngx_pool_t  *p;
0034 
0035     p = a->pool;
0036 
0037     if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) {
0038         p->d.last -= a->size * a->nalloc;
0039     }
0040 
0041     if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) {
0042         p->d.last = (u_char *) a;
0043     }
0044 }
0045 
0046 
0047 void *
0048 ngx_array_push(ngx_array_t *a)
0049 {
0050     void        *elt, *new;
0051     size_t       size;
0052     ngx_pool_t  *p;
0053 
0054     if (a->nelts == a->nalloc) {
0055 
0056         /* the array is full */
0057 
0058         size = a->size * a->nalloc;
0059 
0060         p = a->pool;
0061 
0062         if ((u_char *) a->elts + size == p->d.last
0063             && p->d.last + a->size <= p->d.end)
0064         {
0065             /*
0066              * the array allocation is the last in the pool
0067              * and there is space for new allocation
0068              */
0069 
0070             p->d.last += a->size;
0071             a->nalloc++;
0072 
0073         } else {
0074             /* allocate a new array */
0075 
0076             new = ngx_palloc(p, 2 * size);
0077             if (new == NULL) {
0078                 return NULL;
0079             }
0080 
0081             ngx_memcpy(new, a->elts, size);
0082             a->elts = new;
0083             a->nalloc *= 2;
0084         }
0085     }
0086 
0087     elt = (u_char *) a->elts + a->size * a->nelts;
0088     a->nelts++;
0089 
0090     return elt;
0091 }
0092 
0093 
0094 void *
0095 ngx_array_push_n(ngx_array_t *a, ngx_uint_t n)
0096 {
0097     void        *elt, *new;
0098     size_t       size;
0099     ngx_uint_t   nalloc;
0100     ngx_pool_t  *p;
0101 
0102     size = n * a->size;
0103 
0104     if (a->nelts + n > a->nalloc) {
0105 
0106         /* the array is full */
0107 
0108         p = a->pool;
0109 
0110         if ((u_char *) a->elts + a->size * a->nalloc == p->d.last
0111             && p->d.last + size <= p->d.end)
0112         {
0113             /*
0114              * the array allocation is the last in the pool
0115              * and there is space for new allocation
0116              */
0117 
0118             p->d.last += size;
0119             a->nalloc += n;
0120 
0121         } else {
0122             /* allocate a new array */
0123 
0124             nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc);
0125 
0126             new = ngx_palloc(p, nalloc * a->size);
0127             if (new == NULL) {
0128                 return NULL;
0129             }
0130 
0131             ngx_memcpy(new, a->elts, a->nelts * a->size);
0132             a->elts = new;
0133             a->nalloc = nalloc;
0134         }
0135     }
0136 
0137     elt = (u_char *) a->elts + a->size * a->nelts;
0138     a->nelts += n;
0139 
0140     return elt;
0141 }