blob: 84bc0026306498352afcb88b868bcf5819a811c2 [file] [log] [blame]
Igor Sysoevb9e34412004-09-03 15:50:30 +00001
Igor Sysoevd90282d2004-09-28 08:34:51 +00002/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Igor Sysoevd90282d2004-09-28 08:34:51 +00004 */
5
6
Igor Sysoevb9e34412004-09-03 15:50:30 +00007#include <ngx_config.h>
8#include <ngx_core.h>
9
10
Igor Sysoev35d52722006-10-11 12:47:11 +000011ngx_list_t *
12ngx_list_create(ngx_pool_t *pool, ngx_uint_t n, size_t size)
13{
14 ngx_list_t *list;
15
16 list = ngx_palloc(pool, sizeof(ngx_list_t));
17 if (list == NULL) {
18 return NULL;
19 }
20
21 list->part.elts = ngx_palloc(pool, n * size);
22 if (list->part.elts == NULL) {
23 return NULL;
24 }
25
26 list->part.nelts = 0;
27 list->part.next = NULL;
28 list->last = &list->part;
29 list->size = size;
30 list->nalloc = n;
31 list->pool = pool;
32
33 return list;
34}
35
36
37void *
38ngx_list_push(ngx_list_t *l)
Igor Sysoevb9e34412004-09-03 15:50:30 +000039{
40 void *elt;
41 ngx_list_part_t *last;
42
43 last = l->last;
44
45 if (last->nelts == l->nalloc) {
46
47 /* the last part is full, allocate a new list part */
48
Igor Sysoevc1571722005-03-19 12:38:37 +000049 last = ngx_palloc(l->pool, sizeof(ngx_list_part_t));
50 if (last == NULL) {
Igor Sysoevb9e34412004-09-03 15:50:30 +000051 return NULL;
52 }
53
Igor Sysoevc1571722005-03-19 12:38:37 +000054 last->elts = ngx_palloc(l->pool, l->nalloc * l->size);
55 if (last->elts == NULL) {
Igor Sysoevb9e34412004-09-03 15:50:30 +000056 return NULL;
57 }
58
59 last->nelts = 0;
60 last->next = NULL;
61
62 l->last->next = last;
63 l->last = last;
64 }
65
66 elt = (char *) last->elts + l->size * last->nelts;
67 last->nelts++;
68
69 return elt;
70}