501-nginx源码编译测试内存池接口函数功能

测试代码

#include <ngx_config.h>
#include <nginx.h>
#include <ngx_core.h>
#include <ngx_palloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

}

typedef struct Data stData;
struct Data
{
    char *ptr;
    FILE *pfile;
};

void func1(char *p)
{
    printf("free ptr mem!");
    free(p);
}
void func2(FILE *pf)
{
    printf("close file!");
    fclose(pf);
}
void main()
{
	//512 - sizeof(ngx_pool_t) - 4095   =>   max
    ngx_pool_t *pool = ngx_create_pool(512, NULL);
    if(pool == NULL)
    {
        printf("ngx_create_pool fail...");
        return;
    }

    void *p1 = ngx_palloc(pool, 128);//从小块内存池分配的
    if(p1 == NULL)
    {
        printf("ngx_palloc 128 bytes fail...");
        return;
    }

    stData *p2 = ngx_palloc(pool, 512);//从大块内存池分配的
    if(p2 == NULL)
    {
        printf("ngx_palloc 512 bytes fail...");
        return;
    }
    p2->ptr = malloc(12);
    strcpy(p2->ptr, "hello world");
    p2->pfile = fopen("data.txt", "w");
    
    ngx_pool_cleanup_t *c1 = ngx_pool_cleanup_add(pool, sizeof(char*));
    c1->handler = func1;
    c1->data = p2->ptr;

    ngx_pool_cleanup_t *c2 = ngx_pool_cleanup_add(pool, sizeof(FILE*));
    c2->handler = func2;
    c2->data = p2->pfile;

    ngx_destroy_pool(pool);//1.调用所有的预置的清理函数 2.释放大块内存 3.释放小块内存池所有内存

    return;
}

示意图如下
在这里插入图片描述

关于nginx的安装,见我另外一篇博客

在这里插入图片描述

nginx代码编译

gcc -c -g -I src/core -I src/event -I src/event/modules -I 
src/os/unix -I objs -I src/http -I src/http/modules -o 
ngx_testpool.o  ngx_testpool.c


gcc -o ngx_testpool ngx_testpool.o objs/src/core/ngx_palloc.o 
objs/src/os/unix/ngx_alloc.o

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林林林ZEYU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值