diff options
author | Igor Sysoev <igor@sysoev.ru> | 2017-01-17 20:00:00 +0300 |
---|---|---|
committer | Igor Sysoev <igor@sysoev.ru> | 2017-01-17 20:00:00 +0300 |
commit | 16cbf3c076a0aca6d47adaf3f719493674cf2363 (patch) | |
tree | e6530480020f62a2bdbf249988ec3e2a751d3927 /src/nxt_list.c | |
download | unit-16cbf3c076a0aca6d47adaf3f719493674cf2363.tar.gz unit-16cbf3c076a0aca6d47adaf3f719493674cf2363.tar.bz2 |
Initial version.
Diffstat (limited to 'src/nxt_list.c')
-rw-r--r-- | src/nxt_list.c | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/nxt_list.c b/src/nxt_list.c new file mode 100644 index 00000000..6ba7c9fa --- /dev/null +++ b/src/nxt_list.c @@ -0,0 +1,108 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> + + +nxt_list_t * +nxt_list_create(nxt_mem_pool_t *mp, nxt_uint_t n, size_t size) +{ + nxt_list_t *list; + + list = nxt_mem_alloc(mp, sizeof(nxt_list_t) + n * size); + + if (nxt_fast_path(list != NULL)) { + list->last = &list->part; + list->size = size; + list->nalloc = n; + list->mem_pool = mp; + list->part.next = NULL; + list->part.nelts = 0; + } + + return list; +} + + +void * +nxt_list_add(nxt_list_t *list) +{ + void *elt; + nxt_list_part_t *last; + + last = list->last; + + if (last->nelts == list->nalloc) { + + /* The last list part is filled up, allocating a new list part. */ + + last = nxt_mem_alloc(list->mem_pool, + sizeof(nxt_list_part_t) + list->nalloc * list->size); + + if (nxt_slow_path(last == NULL)) { + return NULL; + } + + last->next = NULL; + last->nelts = 0; + + list->last->next = last; + list->last = last; + } + + elt = nxt_list_data(last) + last->nelts * list->size; + last->nelts++; + + return elt; +} + + +void * +nxt_list_zero_add(nxt_list_t *list) +{ + void *p; + + p = nxt_list_add(list); + + if (nxt_fast_path(p != NULL)) { + nxt_memzero(p, list->size); + } + + return p; +} + + +void * +nxt_list_next(nxt_list_t *list, nxt_list_next_t *next) +{ + if (next->part != NULL) { + next->elt++; + + if (next->elt < next->part->nelts) { + return nxt_list_next_value(list, next); + } + + next->part = next->part->next; + + if (next->part != NULL) { + next->elt = 0; + return nxt_list_data(next->part); + } + + } else { + next->part = nxt_list_part(list); + /* + * The first list part is allocated together with + * a nxt_list_t itself and it may never be NULL. + */ + if (next->part->nelts != 0) { + return nxt_list_data(next->part); + } + + } + + return NULL; +} |