~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Nginx/core/ngx_array.c

Version: ~ [ nginx-0.8.20 ] ~ [ nginx-0.7.62 ] ~ [ nginx-0.6.39 ] ~

  1 
  2 /*
  3  * Copyright (C) Igor Sysoev
  4  */
  5 
  6 
  7 #include <ngx_config.h>
  8 #include <ngx_core.h>
  9 
 10 
 11 ngx_array_t *
 12 ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size)
 13 {
 14     ngx_array_t *a;
 15 
 16     a = ngx_palloc(p, sizeof(ngx_array_t));
 17     if (a == NULL) {
 18         return NULL;
 19     }
 20 
 21     a->elts = ngx_palloc(p, n * size);
 22     if (a->elts == NULL) {
 23         return NULL;
 24     }
 25 
 26     a->nelts = 0;
 27     a->size = size;
 28     a->nalloc = n;
 29     a->pool = p;
 30 
 31     return a;
 32 }
 33 
 34 
 35 void
 36 ngx_array_destroy(ngx_array_t *a)
 37 {
 38     ngx_pool_t  *p;
 39 
 40     p = a->pool;
 41 
 42     if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) {
 43         p->d.last -= a->size * a->nalloc;
 44     }
 45 
 46     if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) {
 47         p->d.last = (u_char *) a;
 48     }
 49 }
 50 
 51 
 52 void *
 53 ngx_array_push(ngx_array_t *a)
 54 {
 55     void        *elt, *new;
 56     size_t       size;
 57     ngx_pool_t  *p;
 58 
 59     if (a->nelts == a->nalloc) {
 60 
 61         /* the array is full */
 62 
 63         size = a->size * a->nalloc;
 64 
 65         p = a->pool;
 66 
 67         if ((u_char *) a->elts + size == p->d.last
 68             && p->d.last + a->size <= p->d.end)
 69         {
 70             /*
 71              * the array allocation is the last in the pool
 72              * and there is space for new allocation
 73              */
 74 
 75             p->d.last += a->size;
 76             a->nalloc++;
 77 
 78         } else {
 79             /* allocate a new array */
 80 
 81             new = ngx_palloc(p, 2 * size);
 82             if (new == NULL) {
 83                 return NULL;
 84             }
 85 
 86             ngx_memcpy(new, a->elts, size);
 87             a->elts = new;
 88             a->nalloc *= 2;
 89         }
 90     }
 91 
 92     elt = (u_char *) a->elts + a->size * a->nelts;
 93     a->nelts++;
 94 
 95     return elt;
 96 }
 97 
 98 
 99 void *
100 ngx_array_push_n(ngx_array_t *a, ngx_uint_t n)
101 {
102     void        *elt, *new;
103     size_t       size;
104     ngx_uint_t   nalloc;
105     ngx_pool_t  *p;
106 
107     size = n * a->size;
108 
109     if (a->nelts + n > a->nalloc) {
110 
111         /* the array is full */
112 
113         p = a->pool;
114 
115         if ((u_char *) a->elts + a->size * a->nalloc == p->d.last
116             && p->d.last + size <= p->d.end)
117         {
118             /*
119              * the array allocation is the last in the pool
120              * and there is space for new allocation
121              */
122 
123             p->d.last += size;
124             a->nalloc += n;
125 
126         } else {
127             /* allocate a new array */
128 
129             nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc);
130 
131             new = ngx_palloc(p, nalloc * a->size);
132             if (new == NULL) {
133                 return NULL;
134             }
135 
136             ngx_memcpy(new, a->elts, a->nelts * a->size);
137             a->elts = new;
138             a->nalloc = nalloc;
139         }
140     }
141 
142     elt = (u_char *) a->elts + a->size * a->nelts;
143     a->nelts += n;
144 
145     return elt;
146 }
147 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.