nginx 内存池

nginx内存池测试了一下:

#include <stdio.h>
#include "ngx_config.h"
#include "ngx_conf_file.h"
#include "nginx.h"
#include "ngx_core.h"
#include "ngx_string.h"
#include "ngx_palloc.h"
#include "ngx_array.h"

volatile ngx_cycle_t  *ngx_cycle;

void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
            const char *fmt, ...)
{
}

void dump_pool(ngx_pool_t* pool)
{
    while (pool)
    {   
        printf("pool = 0x%x\n", pool);
        printf("  .d\n");
        printf("    .last = 0x%x\n", pool->d.last);
        printf("    .end = 0x%x\n", pool->d.end);
        printf("available pool memory = %d\n\n", pool->d.end - pool->d.last);
        pool = pool->d.next;
    }   
}

int main()
{
    ngx_pool_t *pool;
    int i;

    printf("==== create a new pool:\n");
    pool = ngx_create_pool(1024, NULL);
    dump_pool(pool);

    printf("==== alloc array A int[10] :\n");
    ngx_array_t *a = ngx_array_create(pool, 10, sizeof(int));
    dump_pool(pool);

    for (i = 0; i < 10; i++)
    {   
        int *ptr = ngx_array_push(a);
        *ptr = i + 1;
    }   

    //dump_array(a);
    //ngx_array_destroy(a);
    printf("==== alloc array B int[10]:\n");
    ngx_array_t *b = ngx_array_create(pool, 10, sizeof(int));
    dump_pool(pool);
    
    printf("==== destroy A:\n");
    ngx_array_destroy(a);
    dump_pool(pool);
    
    printf("==== destroy B:\n");
    ngx_array_destroy(b);
    dump_pool(pool);
    ngx_destroy_pool(pool);
    return 0;
}
结果如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值