comparison src/core/ngx_array.c @ 0:4eff17414a43

nginx-0.0.1-2002-08-06-20:39:45 import The first code that uses "ngx_" prefix, the previous one used "gx_" prefix. At that point the code is not yet usable. The first draft ideas are dated back to 23.10.2001.
author Igor Sysoev <igor@sysoev.ru>
date Tue, 06 Aug 2002 16:39:45 +0000
parents
children 0e81ac0bb3e2
comparison
equal deleted inserted replaced
-1:000000000000 0:4eff17414a43
1
2 #include <ngx_config.h>
3
4 #include <ngx_alloc.h>
5 #include <ngx_array.h>
6
7 ngx_array_t *ngx_create_array(ngx_pool_t *p, int n, size_t size)
8 {
9 ngx_array_t *a;
10
11 a = ngx_palloc(p, sizeof(ngx_array_t));
12 if (a == NULL)
13 return NULL;
14
15 a->elts = ngx_palloc(p, n * size);
16 if (a->elts == NULL)
17 return NULL;
18
19 a->pool = p;
20 a->nelts = 0;
21 a->nalloc = n;
22 a->size = size;
23
24 return a;
25 }
26
27 void ngx_destroy_array(ngx_array_t *a)
28 {
29 ngx_pool_t *p = a->pool;
30
31 if (a->elts + a->size * a->nalloc == p->last)
32 p->last -= a->size * a->nalloc;
33
34 if ((char *) a + sizeof(ngx_array_t) == p->last)
35 p->last = (char *) a;
36 }
37
38 void *ngx_push_array(ngx_array_t *a)
39 {
40 void *elt;
41
42 /* array is full */
43 if (a->nelts == a->nalloc) {
44 ngx_pool_t *p = a->pool;
45
46 /* array allocation is the last in the pool */
47 if (a->elts + a->size * a->nelts == p->last
48 && (unsigned) (p->end - p->last) >= a->size)
49 {
50 p->last += a->size;
51 a->nalloc++;
52
53 /* allocate new array */
54 } else {
55 void *new = ngx_palloc(p, 2 * a->nalloc * a->size);
56 if (new == NULL)
57 return NULL;
58
59 memcpy(new, a->elts, a->nalloc * a->size);
60 a->elts = new;
61 a->nalloc *= 2;
62 }
63 }
64
65 elt = a->elts + a->size * a->nelts;
66 a->nelts++;
67
68 return elt;
69 }