Nginx内存池:外部资源释放和内存池销毁

外部资源的释放

什么是外部资源?
若存在指针在该内存池中,指针指向堆区获得的空间(这块空间不包括在内存池中),如果直接对内存池清理,就会丢失外部资源的地址(因为指针被清理),导致内存泄漏。

示例:在内存池中的char*指针指向堆内存。如果释放p,那么地址丢失,内存泄漏
在这里插入图片描述

所以Nginx针对这种情况,预置一个资源释放的函数(通过回调函数,函数指针来实现)。
就是这个cleanup:

在这里插入图片描述

typedef void (*ngx_pool_cleanup_pt)(void *data);

typedef struct ngx_pool_cleanup_s  ngx_pool_cleanup_t;

struct ngx_pool_cleanup_s {
    ngx_pool_cleanup_pt   handler;		// 函数指针
    void                 *data;			// 外部资源的地址 
    ngx_pool_cleanup_t   *next;			// 链表,相当于把外部资源头信息串起来
};

而且为了效率,这些头信息也和large信息一样,都是在小块内存池上分配的:

在这里插入图片描述

ngx_pool_cleanup_add()

ngx_pool_cleanup_t *ngx_pool_cleanup_add(ngx_pool_t *p, size_t size)
{
    ngx_pool_cleanup_t  *c;

    c = ngx_palloc(p, sizeof(ngx_pool_cleanup_t));	// 1
    if (c == NULL) {
        return NULL;
    }

    if (size) {
        c->data = ngx_palloc(p, size);	// 2
        if (c->data == NULL) {
            return NULL;
        }

    } else {
        c->data = NULL;
    }

    c->handler = NULL;
    c->next = p->cleanup;		// 3

    p->cleanup = c;

    ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, p->log, 0, "add cleanup: %p", c);

    return c;		// 4
}
  1. 使用ngx_palloc()开辟一个cleanup的头信息。
  2. 需要的内存大小通过malloc()开辟。
  3. 将该头信息串在链表上,从起始的cleanup开始。
  4. 返回头信息的起始位置。

ngx_destroy_pool()

void ngx_destroy_pool(ngx_pool_t *pool)
{
    ngx_pool_t          *p, *n;		// 内存池类型指针
    ngx_pool_large_t    *l;			// 大块内存头部信息
    ngx_pool_cleanup_t  *c;			// 清理函数头部信息

    for (c = pool->cleanup; c; c = c->next) {	// 1
        if (c->handler) {
            ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
                           "run cleanup: %p", c);
            c->handler(c->data);
        }
    }

#if (NGX_DEBUG)

    /*
     * we could allocate the pool->log from this pool
     * so we cannot use this log while free()ing the pool
     */

    for (l = pool->large; l; l = l->next) {		
        ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc);
    }

    for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
        ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
                       "free: %p, unused: %uz", p, p->d.end - p->d.last);

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

#endif

    for (l = pool->large; l; l = l->next) {	// 2
        if (l->alloc) {
            ngx_free(l->alloc);
        }
    }

	// 3
    for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
        ngx_free(p);

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

destroy的顺序

  1. 先遍历cleanup,释放每个外部资源data。
  2. 遍历大块内存,释放每个alloc。
  3. 再对小块内存池遍历,释放每个小块内存。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_索伦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值