Update and clean Tomato RAF files
[tomato.git] / release / src / router / nginx / src / core / ngx_array.c
blob4ea226f06826924456c778106a1c991f0d98f102
2 /*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
8 #include <ngx_config.h>
9 #include <ngx_core.h>
12 ngx_array_t *
13 ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size)
15 ngx_array_t *a;
17 a = ngx_palloc(p, sizeof(ngx_array_t));
18 if (a == NULL) {
19 return NULL;
22 if (ngx_array_init(a, p, n, size) != NGX_OK) {
23 return NULL;
26 return a;
30 void
31 ngx_array_destroy(ngx_array_t *a)
33 ngx_pool_t *p;
35 p = a->pool;
37 if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) {
38 p->d.last -= a->size * a->nalloc;
41 if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) {
42 p->d.last = (u_char *) a;
47 void *
48 ngx_array_push(ngx_array_t *a)
50 void *elt, *new;
51 size_t size;
52 ngx_pool_t *p;
54 if (a->nelts == a->nalloc) {
56 /* the array is full */
58 size = a->size * a->nalloc;
60 p = a->pool;
62 if ((u_char *) a->elts + size == p->d.last
63 && p->d.last + a->size <= p->d.end)
66 * the array allocation is the last in the pool
67 * and there is space for new allocation
70 p->d.last += a->size;
71 a->nalloc++;
73 } else {
74 /* allocate a new array */
76 new = ngx_palloc(p, 2 * size);
77 if (new == NULL) {
78 return NULL;
81 ngx_memcpy(new, a->elts, size);
82 a->elts = new;
83 a->nalloc *= 2;
87 elt = (u_char *) a->elts + a->size * a->nelts;
88 a->nelts++;
90 return elt;
94 void *
95 ngx_array_push_n(ngx_array_t *a, ngx_uint_t n)
97 void *elt, *new;
98 size_t size;
99 ngx_uint_t nalloc;
100 ngx_pool_t *p;
102 size = n * a->size;
104 if (a->nelts + n > a->nalloc) {
106 /* the array is full */
108 p = a->pool;
110 if ((u_char *) a->elts + a->size * a->nalloc == p->d.last
111 && p->d.last + size <= p->d.end)
114 * the array allocation is the last in the pool
115 * and there is space for new allocation
118 p->d.last += size;
119 a->nalloc += n;
121 } else {
122 /* allocate a new array */
124 nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc);
126 new = ngx_palloc(p, nalloc * a->size);
127 if (new == NULL) {
128 return NULL;
131 ngx_memcpy(new, a->elts, a->nelts * a->size);
132 a->elts = new;
133 a->nalloc = nalloc;
137 elt = (u_char *) a->elts + a->size * a->nelts;
138 a->nelts += n;
140 return elt;