ngx_pool_t *
ngx_create_pool(size_t size, ngx_log_t *log)
{
    ngx_pool_t  *p;

    p = (ngx_pool_t*)ngx_memalign(NGX_POOL_ALIGNMENT, size, log);
    if (p == NULL) 
    {
        return NULL;
    }

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

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

    p->current = p;
    p->chain = NULL;
    p->large = NULL;
    p->cleanup = NULL;
    p->log = log;

    return p;
}
Ejemplo n.º 2
0
static void *
ngx_palloc_block(ngx_pool_t *pool, size_t size)
{
    u_char      *m;
    size_t       psize;
    ngx_pool_t  *p, *newp, *current;

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

    m = (u_char*)ngx_memalign(NGX_POOL_ALIGNMENT, psize/*, pool->log*/);
    if (m == NULL) {
        return NULL;
    }

    newp = (ngx_pool_t *) m;

    newp->d.end = m + psize;
    newp->d.next = NULL;
    newp->d.failed = 0;

    m += sizeof(ngx_pool_data_t);
    m = ngx_align_ptr(m, NGX_ALIGNMENT);
    newp->d.last = m + size;

    current = pool->current;

    for (p = current; p->d.next; p = p->d.next) {
        if (p->d.failed++ > 4) {
            current = p->d.next;
        }
    }

    p->d.next = newp;

    pool->current = current ? current : newp;

    return m;
}
Ejemplo n.º 3
0
void *
ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment)
{
    void              *p;
    ngx_pool_large_t  *large;

    p = ngx_memalign(alignment, size/*, pool->log*/);
    if (p == NULL) {
        return NULL;
    }

    large = (ngx_pool_large_t*)ngx_palloc(pool, sizeof(ngx_pool_large_t));
    if (large == NULL) {
        ngx_free(p);
        return NULL;
    }

    large->alloc = p;
    large->next = pool->large;
    pool->large = large;

    return p;
}
Ejemplo n.º 4
0
void *ngx_palloc(ngx_pool_t *pool, size_t size)
{
  char              *m;
  ngx_pool_t        *p, *n;
  ngx_pool_large_t  *large, *last;

  if (size <= (size_t) NGX_MAX_ALLOC_FROM_POOL
    && size <= (size_t) (pool->end - (char *) pool) - sizeof(ngx_pool_t))
  {
    for (p = pool, n = pool->next; /* void */; p = n, n = n->next) {
      m = ngx_align(p->last);

      if ((size_t) (p->end - m) >= size) {
        p->last = m + size ;

        return m;
      }

      if (n == NULL) {
        break;
      }
    }

    /* allocate a new pool block */

    if (!(n = ngx_create_pool((size_t) (p->end - (char *) p), p->log))) {
      return NULL;
    }

    p->next = n;
    m = n->last;
    n->last += size;

    return m;
  }

  /* allocate a large block */

  large = NULL;
  last = NULL;

  if (pool->large) {
    for (last = pool->large; /* void */ ; last = last->next) {
      if (last->alloc == NULL) {
        large = last;
        last = NULL;
        break;
      }

      if (last->next == NULL) {
        break;
      }
    }
  }

  if (large == NULL) {
    if (!(large = ngx_palloc(pool, sizeof(ngx_pool_large_t)))) {
      return NULL;
    }

    large->next = NULL;
  }

#if 0
  if (!(p = ngx_memalign(ngx_pagesize, size, pool->log))) {
    return NULL;
  }
#else
  if (!(p = ngx_alloc(size, pool->log))) {
    return NULL;
  }
#endif

  if (pool->large == NULL) {
    pool->large = large;

  } else if (last) {
    last->next = large;
  }

  large->alloc = p;

  return p;
}
Ejemplo n.º 5
0
static void *
ngx_http_js_memalign(void *mem, size_t alignment, size_t size)
{
    return ngx_memalign(alignment, size, ngx_cycle->log);
}