blob: 8ecb83f86f9476e9391129de60acfce6ca4c97d1 [file] [log] [blame]
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00001
2#include <ngx_config.h>
Igor Sysoev1c104622003-06-03 15:42:58 +00003#include <ngx_core.h>
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00004
Igor Sysoev4e9393a2003-01-09 05:36:00 +00005
Igor Sysoev10a543a2004-03-16 07:10:12 +00006ngx_array_t *ngx_create_array(ngx_pool_t *p, ngx_uint_t n, size_t size)
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00007{
8 ngx_array_t *a;
9
Igor Sysoev4e9393a2003-01-09 05:36:00 +000010 ngx_test_null(a, ngx_palloc(p, sizeof(ngx_array_t)), NULL);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000011
Igor Sysoev4e9393a2003-01-09 05:36:00 +000012 ngx_test_null(a->elts, ngx_palloc(p, n * size), NULL);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000013
14 a->pool = p;
15 a->nelts = 0;
16 a->nalloc = n;
17 a->size = size;
18
19 return a;
20}
21
Igor Sysoev4e9393a2003-01-09 05:36:00 +000022
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000023void ngx_destroy_array(ngx_array_t *a)
24{
Igor Sysoev4e9393a2003-01-09 05:36:00 +000025 ngx_pool_t *p;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000026
Igor Sysoev4e9393a2003-01-09 05:36:00 +000027 p = a->pool;
28
Igor Sysoev1c13c662003-05-20 15:37:55 +000029 if ((char *) a->elts + a->size * a->nalloc == p->last) {
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000030 p->last -= a->size * a->nalloc;
Igor Sysoev4e9393a2003-01-09 05:36:00 +000031 }
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000032
Igor Sysoev4e9393a2003-01-09 05:36:00 +000033 if ((char *) a + sizeof(ngx_array_t) == p->last) {
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000034 p->last = (char *) a;
Igor Sysoev4e9393a2003-01-09 05:36:00 +000035 }
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000036}
37
Igor Sysoev4e9393a2003-01-09 05:36:00 +000038
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000039void *ngx_push_array(ngx_array_t *a)
40{
Igor Sysoev4e9393a2003-01-09 05:36:00 +000041 void *elt, *new;
42 ngx_pool_t *p;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000043
44 /* array is full */
45 if (a->nelts == a->nalloc) {
Igor Sysoev4e9393a2003-01-09 05:36:00 +000046 p = a->pool;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000047
48 /* array allocation is the last in the pool */
Igor Sysoev1c13c662003-05-20 15:37:55 +000049 if ((char *) a->elts + a->size * a->nelts == p->last
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000050 && (unsigned) (p->end - p->last) >= a->size)
51 {
52 p->last += a->size;
53 a->nalloc++;
54
55 /* allocate new array */
56 } else {
Igor Sysoev4e9393a2003-01-09 05:36:00 +000057 ngx_test_null(new, ngx_palloc(p, 2 * a->nalloc * a->size), NULL);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000058
Igor Sysoev4e9393a2003-01-09 05:36:00 +000059 ngx_memcpy(new, a->elts, a->nalloc * a->size);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000060 a->elts = new;
61 a->nalloc *= 2;
62 }
63 }
64
Igor Sysoev1c13c662003-05-20 15:37:55 +000065 elt = (char *) a->elts + a->size * a->nelts;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000066 a->nelts++;
67
68 return elt;
69}