static void
ngx_slab_free_pages(ngx_slab_pool_t *pool, ngx_slab_page_t *page,
    ngx_uint_t pages)
{
    ngx_uint_t        type;
    ngx_slab_page_t  *prev, *join;
    page->slab = pages--;
    if (pages) {
        ngx_memzero(&page[1], pages * sizeof(ngx_slab_page_t));
    }
    if (page->next) {
        prev = (ngx_slab_page_t *) (page->prev & ~NGX_SLAB_PAGE_MASK);
        prev->next = page->next;
        page->next->prev = page->prev;
    }
    join = page + page->slab;
    if (join < pool->last) {
        type = join->prev & NGX_SLAB_PAGE_MASK;
        if (type == NGX_SLAB_PAGE) {
            if (join->next != NULL) {
                pages += join->slab;
                page->slab += join->slab;
                prev = (ngx_slab_page_t *) (join->prev & ~NGX_SLAB_PAGE_MASK);
                prev->next = join->next;
                join->next->prev = join->prev;
                join->slab = NGX_SLAB_PAGE_FREE;
                join->next = NULL;
                join->prev = NGX_SLAB_PAGE;
            }
        }
    }
    if (page > pool->pages) {
        join = page - 1;
        type = join->prev & NGX_SLAB_PAGE_MASK;
        if (type == NGX_SLAB_PAGE) {
            if (join->slab == NGX_SLAB_PAGE_FREE) {
                join = (ngx_slab_page_t *) (join->prev & ~NGX_SLAB_PAGE_MASK);
            }
            if (join->next != NULL) {
                pages += join->slab;
                join->slab += page->slab;
                prev = (ngx_slab_page_t *) (join->prev & ~NGX_SLAB_PAGE_MASK);
                prev->next = join->next;
                join->next->prev = join->prev;
                page->slab = NGX_SLAB_PAGE_FREE;
                page->next = NULL;
                page->prev = NGX_SLAB_PAGE;
                page = join;
            }
        }
    }
    if (pages) {
        page[pages].prev = (uintptr_t) page;
    }
    page->prev = (uintptr_t) &pool->free;
    page->next = pool->free.next;
    page->next->prev = (uintptr_t) page;
    pool->free.next = page;
}
['void\nngx_slab_free_locked(ngx_slab_pool_t *pool, void *p)\n{\n    size_t            size;\n    uintptr_t         slab, m, *bitmap;\n    ngx_uint_t        n, type, slot, shift, map;\n    ngx_slab_page_t  *slots, *page;\n    ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, ngx_cycle->log, 0, "slab free: %p", p);\n    if ((u_char *) p < pool->start || (u_char *) p > pool->end) {\n        ngx_slab_error(pool, NGX_LOG_ALERT, "ngx_slab_free(): outside of pool");\n        goto fail;\n    }\n    n = ((u_char *) p - pool->start) >> ngx_pagesize_shift;\n    page = &pool->pages[n];\n    slab = page->slab;\n    type = page->prev & NGX_SLAB_PAGE_MASK;\n    switch (type) {\n    case NGX_SLAB_SMALL:\n        shift = slab & NGX_SLAB_SHIFT_MASK;\n        size = 1 << shift;\n        if ((uintptr_t) p & (size - 1)) {\n            goto wrong_chunk;\n        }\n        n = ((uintptr_t) p & (ngx_pagesize - 1)) >> shift;\n        m = (uintptr_t) 1 << (n & (sizeof(uintptr_t) * 8 - 1));\n        n /= (sizeof(uintptr_t) * 8);\n        bitmap = (uintptr_t *)\n                             ((uintptr_t) p & ~((uintptr_t) ngx_pagesize - 1));\n        if (bitmap[n] & m) {\n            if (page->next == NULL) {\n                slots = (ngx_slab_page_t *)\n                                   ((u_char *) pool + sizeof(ngx_slab_pool_t));\n                slot = shift - pool->min_shift;\n                page->next = slots[slot].next;\n                slots[slot].next = page;\n                page->prev = (uintptr_t) &slots[slot] | NGX_SLAB_SMALL;\n                page->next->prev = (uintptr_t) page | NGX_SLAB_SMALL;\n            }\n            bitmap[n] &= ~m;\n            n = (1 << (ngx_pagesize_shift - shift)) / 8 / (1 << shift);\n            if (n == 0) {\n                n = 1;\n            }\n            if (bitmap[0] & ~(((uintptr_t) 1 << n) - 1)) {\n                goto done;\n            }\n            map = (1 << (ngx_pagesize_shift - shift)) / (sizeof(uintptr_t) * 8);\n            for (n = 1; n < map; n++) {\n                if (bitmap[n]) {\n                    goto done;\n                }\n            }\n            ngx_slab_free_pages(pool, page, 1);\n            goto done;\n        }\n        goto chunk_already_free;\n    case NGX_SLAB_EXACT:\n        m = (uintptr_t) 1 <<\n                (((uintptr_t) p & (ngx_pagesize - 1)) >> ngx_slab_exact_shift);\n        size = ngx_slab_exact_size;\n        if ((uintptr_t) p & (size - 1)) {\n            goto wrong_chunk;\n        }\n        if (slab & m) {\n            if (slab == NGX_SLAB_BUSY) {\n                slots = (ngx_slab_page_t *)\n                                   ((u_char *) pool + sizeof(ngx_slab_pool_t));\n                slot = ngx_slab_exact_shift - pool->min_shift;\n                page->next = slots[slot].next;\n                slots[slot].next = page;\n                page->prev = (uintptr_t) &slots[slot] | NGX_SLAB_EXACT;\n                page->next->prev = (uintptr_t) page | NGX_SLAB_EXACT;\n            }\n            page->slab &= ~m;\n            if (page->slab) {\n                goto done;\n            }\n            ngx_slab_free_pages(pool, page, 1);\n            goto done;\n        }\n        goto chunk_already_free;\n    case NGX_SLAB_BIG:\n        shift = slab & NGX_SLAB_SHIFT_MASK;\n        size = 1 << shift;\n        if ((uintptr_t) p & (size - 1)) {\n            goto wrong_chunk;\n        }\n        m = (uintptr_t) 1 << ((((uintptr_t) p & (ngx_pagesize - 1)) >> shift)\n                              + NGX_SLAB_MAP_SHIFT);\n        if (slab & m) {\n            if (page->next == NULL) {\n                slots = (ngx_slab_page_t *)\n                                   ((u_char *) pool + sizeof(ngx_slab_pool_t));\n                slot = shift - pool->min_shift;\n                page->next = slots[slot].next;\n                slots[slot].next = page;\n                page->prev = (uintptr_t) &slots[slot] | NGX_SLAB_BIG;\n                page->next->prev = (uintptr_t) page | NGX_SLAB_BIG;\n            }\n            page->slab &= ~m;\n            if (page->slab & NGX_SLAB_MAP_MASK) {\n                goto done;\n            }\n            ngx_slab_free_pages(pool, page, 1);\n            goto done;\n        }\n        goto chunk_already_free;\n    case NGX_SLAB_PAGE:\n        if ((uintptr_t) p & (ngx_pagesize - 1)) {\n            goto wrong_chunk;\n        }\n        if (slab == NGX_SLAB_PAGE_FREE) {\n            ngx_slab_error(pool, NGX_LOG_ALERT,\n                           "ngx_slab_free(): page is already free");\n            goto fail;\n        }\n        if (slab == NGX_SLAB_PAGE_BUSY) {\n            ngx_slab_error(pool, NGX_LOG_ALERT,\n                           "ngx_slab_free(): pointer to wrong page");\n            goto fail;\n        }\n        n = ((u_char *) p - pool->start) >> ngx_pagesize_shift;\n        size = slab & ~NGX_SLAB_PAGE_START;\n        ngx_slab_free_pages(pool, &pool->pages[n], size);\n        ngx_slab_junk(p, size << ngx_pagesize_shift);\n        return;\n    }\n    return;\ndone:\n    ngx_slab_junk(p, size);\n    return;\nwrong_chunk:\n    ngx_slab_error(pool, NGX_LOG_ALERT,\n                   "ngx_slab_free(): pointer to wrong chunk");\n    goto fail;\nchunk_already_free:\n    ngx_slab_error(pool, NGX_LOG_ALERT,\n                   "ngx_slab_free(): chunk is already free");\nfail:\n    return;\n}', 'static void\nngx_slab_free_pages(ngx_slab_pool_t *pool, ngx_slab_page_t *page,\n    ngx_uint_t pages)\n{\n    ngx_uint_t        type;\n    ngx_slab_page_t  *prev, *join;\n    page->slab = pages--;\n    if (pages) {\n        ngx_memzero(&page[1], pages * sizeof(ngx_slab_page_t));\n    }\n    if (page->next) {\n        prev = (ngx_slab_page_t *) (page->prev & ~NGX_SLAB_PAGE_MASK);\n        prev->next = page->next;\n        page->next->prev = page->prev;\n    }\n    join = page + page->slab;\n    if (join < pool->last) {\n        type = join->prev & NGX_SLAB_PAGE_MASK;\n        if (type == NGX_SLAB_PAGE) {\n            if (join->next != NULL) {\n                pages += join->slab;\n                page->slab += join->slab;\n                prev = (ngx_slab_page_t *) (join->prev & ~NGX_SLAB_PAGE_MASK);\n                prev->next = join->next;\n                join->next->prev = join->prev;\n                join->slab = NGX_SLAB_PAGE_FREE;\n                join->next = NULL;\n                join->prev = NGX_SLAB_PAGE;\n            }\n        }\n    }\n    if (page > pool->pages) {\n        join = page - 1;\n        type = join->prev & NGX_SLAB_PAGE_MASK;\n        if (type == NGX_SLAB_PAGE) {\n            if (join->slab == NGX_SLAB_PAGE_FREE) {\n                join = (ngx_slab_page_t *) (join->prev & ~NGX_SLAB_PAGE_MASK);\n            }\n            if (join->next != NULL) {\n                pages += join->slab;\n                join->slab += page->slab;\n                prev = (ngx_slab_page_t *) (join->prev & ~NGX_SLAB_PAGE_MASK);\n                prev->next = join->next;\n                join->next->prev = join->prev;\n                page->slab = NGX_SLAB_PAGE_FREE;\n                page->next = NULL;\n                page->prev = NGX_SLAB_PAGE;\n                page = join;\n            }\n        }\n    }\n    if (pages) {\n        page[pages].prev = (uintptr_t) page;\n    }\n    page->prev = (uintptr_t) &pool->free;\n    page->next = pool->free.next;\n    page->next->prev = (uintptr_t) page;\n    pool->free.next = page;\n}']