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;
}
结果如下: