comparison src/core/ngx_array.c @ 44:0e81ac0bb3e2

nginx-0.0.1-2003-01-09-08:36:00 import
author Igor Sysoev <igor@sysoev.ru>
date Thu, 09 Jan 2003 05:36:00 +0000
parents 4eff17414a43
children 19cc647ecd91
comparison
equal deleted inserted replaced
43:53cd05892261 44:0e81ac0bb3e2
1 1
2 #include <ngx_config.h> 2 #include <ngx_config.h>
3 3
4 #include <ngx_alloc.h> 4 #include <ngx_alloc.h>
5 #include <ngx_string.h>
5 #include <ngx_array.h> 6 #include <ngx_array.h>
7
6 8
7 ngx_array_t *ngx_create_array(ngx_pool_t *p, int n, size_t size) 9 ngx_array_t *ngx_create_array(ngx_pool_t *p, int n, size_t size)
8 { 10 {
9 ngx_array_t *a; 11 ngx_array_t *a;
10 12
11 a = ngx_palloc(p, sizeof(ngx_array_t)); 13 ngx_test_null(a, ngx_palloc(p, sizeof(ngx_array_t)), NULL);
12 if (a == NULL)
13 return NULL;
14 14
15 a->elts = ngx_palloc(p, n * size); 15 ngx_test_null(a->elts, ngx_palloc(p, n * size), NULL);
16 if (a->elts == NULL)
17 return NULL;
18 16
19 a->pool = p; 17 a->pool = p;
20 a->nelts = 0; 18 a->nelts = 0;
21 a->nalloc = n; 19 a->nalloc = n;
22 a->size = size; 20 a->size = size;
23 21
24 return a; 22 return a;
25 } 23 }
26 24
25
27 void ngx_destroy_array(ngx_array_t *a) 26 void ngx_destroy_array(ngx_array_t *a)
28 { 27 {
29 ngx_pool_t *p = a->pool; 28 ngx_pool_t *p;
30 29
31 if (a->elts + a->size * a->nalloc == p->last) 30 p = a->pool;
31
32 if (a->elts + a->size * a->nalloc == p->last) {
32 p->last -= a->size * a->nalloc; 33 p->last -= a->size * a->nalloc;
34 }
33 35
34 if ((char *) a + sizeof(ngx_array_t) == p->last) 36 if ((char *) a + sizeof(ngx_array_t) == p->last) {
35 p->last = (char *) a; 37 p->last = (char *) a;
38 }
36 } 39 }
40
37 41
38 void *ngx_push_array(ngx_array_t *a) 42 void *ngx_push_array(ngx_array_t *a)
39 { 43 {
40 void *elt; 44 void *elt, *new;
45 ngx_pool_t *p;
41 46
42 /* array is full */ 47 /* array is full */
43 if (a->nelts == a->nalloc) { 48 if (a->nelts == a->nalloc) {
44 ngx_pool_t *p = a->pool; 49 p = a->pool;
45 50
46 /* array allocation is the last in the pool */ 51 /* array allocation is the last in the pool */
47 if (a->elts + a->size * a->nelts == p->last 52 if (a->elts + a->size * a->nelts == p->last
48 && (unsigned) (p->end - p->last) >= a->size) 53 && (unsigned) (p->end - p->last) >= a->size)
49 { 54 {
50 p->last += a->size; 55 p->last += a->size;
51 a->nalloc++; 56 a->nalloc++;
52 57
53 /* allocate new array */ 58 /* allocate new array */
54 } else { 59 } else {
55 void *new = ngx_palloc(p, 2 * a->nalloc * a->size); 60 ngx_test_null(new, ngx_palloc(p, 2 * a->nalloc * a->size), NULL);
56 if (new == NULL)
57 return NULL;
58 61
59 memcpy(new, a->elts, a->nalloc * a->size); 62 ngx_memcpy(new, a->elts, a->nalloc * a->size);
60 a->elts = new; 63 a->elts = new;
61 a->nalloc *= 2; 64 a->nalloc *= 2;
62 } 65 }
63 } 66 }
64 67