src/core/ngx_list.c - nginx-1.7.10

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>


  7. ngx_list_t *
  8. ngx_list_create(ngx_pool_t *pool, ngx_uint_t n, size_t size)
  9. {
  10.     ngx_list_t  *list;

  11.     list = ngx_palloc(pool, sizeof(ngx_list_t));
  12.     if (list == NULL) {
  13.         return NULL;
  14.     }

  15.     if (ngx_list_init(list, pool, n, size) != NGX_OK) {
  16.         return NULL;
  17.     }

  18.     return list;
  19. }


  20. void *
  21. ngx_list_push(ngx_list_t *l)
  22. {
  23.     void             *elt;
  24.     ngx_list_part_t  *last;

  25.     last = l->last;

  26.     if (last->nelts == l->nalloc) {

  27.         /* the last part is full, allocate a new list part */

  28.         last = ngx_palloc(l->pool, sizeof(ngx_list_part_t));
  29.         if (last == NULL) {
  30.             return NULL;
  31.         }

  32.         last->elts = ngx_palloc(l->pool, l->nalloc * l->size);
  33.         if (last->elts == NULL) {
  34.             return NULL;
  35.         }

  36.         last->nelts = 0;
  37.         last->next = NULL;

  38.         l->last->next = last;
  39.         l->last = last;
  40.     }

  41.     elt = (char *) last->elts + l->size * last->nelts;
  42.     last->nelts++;

  43.     return elt;
  44. }