comparison src/core/ngx_array.c @ 0:f0b350454894 NGINX_0_1_0

nginx 0.1.0 *) The first public version.
author Igor Sysoev <http://sysoev.ru>
date Mon, 04 Oct 2004 00:00:00 +0400
parents
children aab2ea7c0458
comparison
equal deleted inserted replaced
-1:000000000000 0:f0b350454894
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 *ngx_create_array(ngx_pool_t *p, ngx_uint_t n, size_t size)
12 {
13 ngx_array_t *a;
14
15 ngx_test_null(a, ngx_palloc(p, sizeof(ngx_array_t)), NULL);
16
17 ngx_test_null(a->elts, ngx_palloc(p, n * size), 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
28 void ngx_destroy_array(ngx_array_t *a)
29 {
30 ngx_pool_t *p;
31
32 p = a->pool;
33
34 if ((char *) a->elts + a->size * a->nalloc == p->last) {
35 p->last -= a->size * a->nalloc;
36 }
37
38 if ((char *) a + sizeof(ngx_array_t) == p->last) {
39 p->last = (char *) a;
40 }
41 }
42
43
44 void *ngx_push_array(ngx_array_t *a)
45 {
46 void *elt, *new;
47 ngx_pool_t *p;
48
49 /* array is full */
50 if (a->nelts == a->nalloc) {
51 p = a->pool;
52
53 /* array allocation is the last in the pool */
54 if ((char *) a->elts + a->size * a->nelts == p->last
55 && (unsigned) (p->end - p->last) >= a->size)
56 {
57 p->last += a->size;
58 a->nalloc++;
59
60 /* allocate new array */
61 } else {
62 ngx_test_null(new, ngx_palloc(p, 2 * a->nalloc * a->size), NULL);
63
64 ngx_memcpy(new, a->elts, a->nalloc * a->size);
65 a->elts = new;
66 a->nalloc *= 2;
67 }
68 }
69
70 elt = (char *) a->elts + a->size * a->nelts;
71 a->nelts++;
72
73 return elt;
74 }