src/core/ngx_palloc.c - nginx source code

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. static ngx_inline void *ngx_palloc_small(ngx_pool_t *pool, size_t size,
  8.     ngx_uint_t align);
  9. static void *ngx_palloc_block(ngx_pool_t *pool, size_t size);
  10. static void *ngx_palloc_large(ngx_pool_t *pool, size_t size);


  11. ngx_pool_t *
  12. ngx_create_pool(size_t size, ngx_log_t *log)
  13. {
  14.     ngx_pool_t  *p;

  15.     p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log);
  16.     if (p == NULL) {
  17.         return NULL;
  18.     }

  19.     p->d.last = (u_char *) p + sizeof(ngx_pool_t);
  20.     p->d.end = (u_char *) p + size;
  21.     p->d.next = NULL;
  22.     p->d.failed = 0;

  23.     size = size - sizeof(ngx_pool_t);
  24.     p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL;

  25.     p->current = p;
  26.     p->chain = NULL;
  27.     p->large = NULL;
  28.     p->cleanup = NULL;
  29.     p->log = log;

  30.     return p;
  31. }


  32. void
  33. ngx_destroy_pool(ngx_pool_t *pool)
  34. {
  35.     ngx_pool_t          *p, *n;
  36.     ngx_pool_large_t    *l;
  37.     ngx_pool_cleanup_t  *c;

  38.     for (c = pool->cleanup; c; c = c->next) {
  39.         if (c->handler) {
  40.             ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
  41.                            "run cleanup: %p", c);
  42.             c->handler(c->data);
  43.         }
  44.     }

  45. #if (NGX_DEBUG)

  46.     /*
  47.      * we could allocate the pool->log from this pool
  48.      * so we cannot use this log while free()ing the pool
  49.      */

  50.     for (l = pool->large; l; l = l->next) {
  51.         ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc);
  52.     }

  53.     for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
  54.         ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
  55.                        "free: %p, unused: %uz", p, p->d.end - p->d.last);

  56.         if (n == NULL) {
  57.             break;
  58.         }
  59.     }

  60. #endif

  61.     for (l = pool->large; l; l = l->next) {
  62.         if (l->alloc) {
  63.             ngx_free(l->alloc);
  64.         }
  65.     }

  66.     for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
  67.         ngx_free(p);

  68.         if (n == NULL) {
  69.             break;
  70.         }
  71.     }
  72. }


  73. void
  74. ngx_reset_pool(ngx_pool_t *pool)
  75. {
  76.     ngx_pool_t        *p;
  77.     ngx_pool_large_t  *l;

  78.     for (l = pool->large; l; l = l->next) {
  79.         if (l->alloc) {
  80.             ngx_free(l->alloc);
  81.         }
  82.     }

  83.     for (p = pool; p; p = p->d.next) {
  84.         p->d.last = (u_char *) p + sizeof(ngx_pool_t);
  85.         p->d.failed = 0;
  86.     }

  87.     pool->current = pool;
  88.     pool->chain = NULL;
  89.     pool->large = NULL;
  90. }


  91. void *
  92. ngx_palloc(ngx_pool_t *pool, size_t size)
  93. {
  94. #if !(NGX_DEBUG_PALLOC)
  95.     if (size <= pool->max) {
  96.         return ngx_palloc_small(pool, size, 1);
  97.     }
  98. #endif

  99.     return ngx_palloc_large(pool, size);
  100. }


  101. void *
  102. ngx_pnalloc(ngx_pool_t *pool, size_t size)
  103. {
  104. #if !(NGX_DEBUG_PALLOC)
  105.     if (size <= pool->max) {
  106.         return ngx_palloc_small(pool, size, 0);
  107.     }
  108. #endif

  109.     return ngx_palloc_large(pool, size);
  110. }


  111. static ngx_inline void *
  112. ngx_palloc_small(ngx_pool_t *pool, size_t size, ngx_uint_t align)
  113. {
  114.     u_char      *m;
  115.     ngx_pool_t  *p;

  116.     p = pool->current;

  117.     do {
  118.         m = p->d.last;

  119.         if (align) {
  120.             m = ngx_align_ptr(m, NGX_ALIGNMENT);
  121.         }

  122.         if ((size_t) (p->d.end - m) >= size) {
  123.             p->d.last = m + size;

  124.             return m;
  125.         }

  126.         p = p->d.next;

  127.     } while (p);

  128.     return ngx_palloc_block(pool, size);
  129. }


  130. static void *
  131. ngx_palloc_block(ngx_pool_t *pool, size_t size)
  132. {
  133.     u_char      *m;
  134.     size_t       psize;
  135.     ngx_pool_t  *p, *new;

  136.     psize = (size_t) (pool->d.end - (u_char *) pool);

  137.     m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log);
  138.     if (m == NULL) {
  139.         return NULL;
  140.     }

  141.     new = (ngx_pool_t *) m;

  142.     new->d.end = m + psize;
  143.     new->d.next = NULL;
  144.     new->d.failed = 0;

  145.     m += sizeof(ngx_pool_data_t);
  146.     m = ngx_align_ptr(m, NGX_ALIGNMENT);
  147.     new->d.last = m + size;

  148.     for (p = pool->current; p->d.next; p = p->d.next) {
  149.         if (p->d.failed++ > 4) {
  150.             pool->current = p->d.next;
  151.         }
  152.     }

  153.     p->d.next = new;

  154.     return m;
  155. }


  156. static void *
  157. ngx_palloc_large(ngx_pool_t *pool, size_t size)
  158. {
  159.     void              *p;
  160.     ngx_uint_t         n;
  161.     ngx_pool_large_t  *large;

  162.     p = ngx_alloc(size, pool->log);
  163.     if (p == NULL) {
  164.         return NULL;
  165.     }

  166.     n = 0;

  167.     for (large = pool->large; large; large = large->next) {
  168.         if (large->alloc == NULL) {
  169.             large->alloc = p;
  170.             return p;
  171.         }

  172.         if (n++ > 3) {
  173.             break;
  174.         }
  175.     }

  176.     large = ngx_palloc_small(pool, sizeof(ngx_pool_large_t), 1);
  177.     if (large == NULL) {
  178.         ngx_free(p);
  179.         return NULL;
  180.     }

  181.     large->alloc = p;
  182.     large->next = pool->large;
  183.     pool->large = large;

  184.     return p;
  185. }


  186. void *
  187. ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment)
  188. {
  189.     void              *p;
  190.     ngx_pool_large_t  *large;

  191.     p = ngx_memalign(alignment, size, pool->log);
  192.     if (p == NULL) {
  193.         return NULL;
  194.     }

  195.     large = ngx_palloc_small(pool, sizeof(ngx_pool_large_t), 1);
  196.     if (large == NULL) {
  197.         ngx_free(p);
  198.         return NULL;
  199.     }

  200.     large->alloc = p;
  201.     large->next = pool->large;
  202.     pool->large = large;

  203.     return p;
  204. }


  205. ngx_int_t
  206. ngx_pfree(ngx_pool_t *pool, void *p)
  207. {
  208.     ngx_pool_large_t  *l;

  209.     for (l = pool->large; l; l = l->next) {
  210.         if (p == l->alloc) {
  211.             ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
  212.                            "free: %p", l->alloc);
  213.             ngx_free(l->alloc);
  214.             l->alloc = NULL;

  215.             return NGX_OK;
  216.         }
  217.     }

  218.     return NGX_DECLINED;
  219. }


  220. void *
  221. ngx_pcalloc(ngx_pool_t *pool, size_t size)
  222. {
  223.     void *p;

  224.     p = ngx_palloc(pool, size);
  225.     if (p) {
  226.         ngx_memzero(p, size);
  227.     }

  228.     return p;
  229. }


  230. ngx_pool_cleanup_t *
  231. ngx_pool_cleanup_add(ngx_pool_t *p, size_t size)
  232. {
  233.     ngx_pool_cleanup_t  *c;

  234.     c = ngx_palloc(p, sizeof(ngx_pool_cleanup_t));
  235.     if (c == NULL) {
  236.         return NULL;
  237.     }

  238.     if (size) {
  239.         c->data = ngx_palloc(p, size);
  240.         if (c->data == NULL) {
  241.             return NULL;
  242.         }

  243.     } else {
  244.         c->data = NULL;
  245.     }

  246.     c->handler = NULL;
  247.     c->next = p->cleanup;

  248.     p->cleanup = c;

  249.     ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, p->log, 0, "add cleanup: %p", c);

  250.     return c;
  251. }


  252. void
  253. ngx_pool_run_cleanup_file(ngx_pool_t *p, ngx_fd_t fd)
  254. {
  255.     ngx_pool_cleanup_t       *c;
  256.     ngx_pool_cleanup_file_t  *cf;

  257.     for (c = p->cleanup; c; c = c->next) {
  258.         if (c->handler == ngx_pool_cleanup_file) {

  259.             cf = c->data;

  260.             if (cf->fd == fd) {
  261.                 c->handler(cf);
  262.                 c->handler = NULL;
  263.                 return;
  264.             }
  265.         }
  266.     }
  267. }


  268. void
  269. ngx_pool_cleanup_file(void *data)
  270. {
  271.     ngx_pool_cleanup_file_t  *c = data;

  272.     ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, c->log, 0, "file cleanup: fd:%d",
  273.                    c->fd);

  274.     if (ngx_close_file(c->fd) == NGX_FILE_ERROR) {
  275.         ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
  276.                       ngx_close_file_n " \"%s\" failed", c->name);
  277.     }
  278. }


  279. void
  280. ngx_pool_delete_file(void *data)
  281. {
  282.     ngx_pool_cleanup_file_t  *c = data;

  283.     ngx_err_t  err;

  284.     ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, c->log, 0, "file cleanup: fd:%d %s",
  285.                    c->fd, c->name);

  286.     if (ngx_delete_file(c->name) == NGX_FILE_ERROR) {
  287.         err = ngx_errno;

  288.         if (err != NGX_ENOENT) {
  289.             ngx_log_error(NGX_LOG_CRIT, c->log, err,
  290.                           ngx_delete_file_n " \"%s\" failed", c->name);
  291.         }
  292.     }

  293.     if (ngx_close_file(c->fd) == NGX_FILE_ERROR) {
  294.         ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
  295.                       ngx_close_file_n " \"%s\" failed", c->name);
  296.     }
  297. }


  298. #if 0

  299. static void *
  300. ngx_get_cached_block(size_t size)
  301. {
  302.     void                     *p;
  303.     ngx_cached_block_slot_t  *slot;

  304.     if (ngx_cycle->cache == NULL) {
  305.         return NULL;
  306.     }

  307.     slot = &ngx_cycle->cache[(size + ngx_pagesize - 1) / ngx_pagesize];

  308.     slot->tries++;

  309.     if (slot->number) {
  310.         p = slot->block;
  311.         slot->block = slot->block->next;
  312.         slot->number--;
  313.         return p;
  314.     }

  315.     return NULL;
  316. }

  317. #endif