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->last) {
43 p->last -= a->size * a->nalloc;
44 }
45
46 if ((u_char *) a + sizeof(ngx_array_t) == p->last) {
47 p->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->last && p->last + a->size <= p->end)
68 {
69 /*
70 * the array allocation is the last in the pool
71 * and there is space for new allocation
72 */
73
74 p->last += a->size;
75 a->nalloc++;
76
77 } else {
78 /* allocate a new array */
79
80 new = ngx_palloc(p, 2 * size);
81 if (new == NULL) {
82 return NULL;
83 }
84
85 ngx_memcpy(new, a->elts, size);
86 a->elts = new;
87 a->nalloc *= 2;
88 }
89 }
90
91 elt = (u_char *) a->elts + a->size * a->nelts;
92 a->nelts++;
93
94 return elt;
95 }
96
97
98 void *
99 ngx_array_push_n(ngx_array_t *a, ngx_uint_t n)
100 {
101 void *elt, *new;
102 size_t size;
103 ngx_uint_t nalloc;
104 ngx_pool_t *p;
105
106 size = n * a->size;
107
108 if (a->nelts + n > a->nalloc) {
109
110 /* the array is full */
111
112 p = a->pool;
113
114 if ((u_char *) a->elts + a->size * a->nalloc == p->last
115 && p->last + size <= p->end)
116 {
117 /*
118 * the array allocation is the last in the pool
119 * and there is space for new allocation
120 */
121
122 p->last += size;
123 a->nalloc += n;
124
125 } else {
126 /* allocate a new array */
127
128 nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc);
129
130 new = ngx_palloc(p, nalloc * a->size);
131 if (new == NULL) {
132 return NULL;
133 }
134
135 ngx_memcpy(new, a->elts, a->nelts * a->size);
136 a->elts = new;
137 a->nalloc = nalloc;
138 }
139 }
140
141 elt = (u_char *) a->elts + a->size * a->nelts;
142 a->nelts += n;
143
144 return elt;
145 }
146
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.