3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
8 #include <ngx_config.h>
13 ngx_array_create(ngx_pool_t
*p
, ngx_uint_t n
, size_t size
)
17 a
= ngx_palloc(p
, sizeof(ngx_array_t
));
22 if (ngx_array_init(a
, p
, n
, size
) != NGX_OK
) {
31 ngx_array_destroy(ngx_array_t
*a
)
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
;
48 ngx_array_push(ngx_array_t
*a
)
54 if (a
->nelts
== a
->nalloc
) {
56 /* the array is full */
58 size
= a
->size
* a
->nalloc
;
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
74 /* allocate a new array */
76 new = ngx_palloc(p
, 2 * size
);
81 ngx_memcpy(new, a
->elts
, size
);
87 elt
= (u_char
*) a
->elts
+ a
->size
* a
->nelts
;
95 ngx_array_push_n(ngx_array_t
*a
, ngx_uint_t n
)
104 if (a
->nelts
+ n
> a
->nalloc
) {
106 /* the array is full */
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
122 /* allocate a new array */
124 nalloc
= 2 * ((n
>= a
->nalloc
) ? n
: a
->nalloc
);
126 new = ngx_palloc(p
, nalloc
* a
->size
);
131 ngx_memcpy(new, a
->elts
, a
->nelts
* a
->size
);
137 elt
= (u_char
*) a
->elts
+ a
->size
* a
->nelts
;