3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size)
16 a = ngx_palloc(p, sizeof(ngx_array_t));
21 a->elts = ngx_palloc(p, n * size);
22 if (a->elts == NULL) {
36 ngx_array_destroy(ngx_array_t *a)
42 if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) {
43 p->d.last -= a->size * a->nalloc;
46 if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) {
47 p->d.last = (u_char *) a;
53 ngx_array_push(ngx_array_t *a)
59 if (a->nelts == a->nalloc) {
61 /* the array is full */
63 size = a->size * a->nalloc;
67 if ((u_char *) a->elts + size == p->d.last
68 && p->d.last + a->size <= p->d.end)
71 * the array allocation is the last in the pool
72 * and there is space for new allocation
79 /* allocate a new array */
81 new = ngx_palloc(p, 2 * size);
86 ngx_memcpy(new, a->elts, size);
92 elt = (u_char *) a->elts + a->size * a->nelts;
100 ngx_array_push_n(ngx_array_t *a, ngx_uint_t n)
109 if (a->nelts + n > a->nalloc) {
111 /* the array is full */
115 if ((u_char *) a->elts + a->size * a->nalloc == p->d.last
116 && p->d.last + size <= p->d.end)
119 * the array allocation is the last in the pool
120 * and there is space for new allocation
127 /* allocate a new array */
129 nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc);
131 new = ngx_palloc(p, nalloc * a->size);
136 ngx_memcpy(new, a->elts, a->nelts * a->size);
142 elt = (u_char *) a->elts + a->size * a->nelts;