Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ngx_list.c
Go to the documentation of this file.
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) Nginx, Inc.
5  */
6 
7 
8 #include <ngx_config.h>
9 #include <ngx_core.h>
10 
11 
12 ngx_list_t *
13 ngx_list_create(ngx_pool_t *pool, ngx_uint_t n, size_t size)
14 {
15  ngx_list_t *list;
16 
17  list = ngx_palloc(pool, sizeof(ngx_list_t));
18  if (list == NULL) {
19  return NULL;
20  }
21 
22  list->part.elts = ngx_palloc(pool, n * size);
23  if (list->part.elts == NULL) {
24  return NULL;
25  }
26 
27  list->part.nelts = 0;
28  list->part.next = NULL;
29  list->last = &list->part;
30  list->size = size;
31  list->nalloc = n;
32  list->pool = pool;
33 
34  return list;
35 }
36 
37 
38 void *
40 {
41  void *elt;
42  ngx_list_part_t *last;
43 
44  last = l->last;
45 
46  if (last->nelts == l->nalloc) {
47 
48  /* the last part is full, allocate a new list part */
49 
50  last = ngx_palloc(l->pool, sizeof(ngx_list_part_t));
51  if (last == NULL) {
52  return NULL;
53  }
54 
55  last->elts = ngx_palloc(l->pool, l->nalloc * l->size);
56  if (last->elts == NULL) {
57  return NULL;
58  }
59 
60  last->nelts = 0;
61  last->next = NULL;
62 
63  l->last->next = last;
64  l->last = last;
65  }
66 
67  elt = (char *) last->elts + l->size * last->nelts;
68  last->nelts++;
69 
70  return elt;
71 }