ngx_palloc.h


/*
* NGX_MAX_ALLOC_FROM_POOL should be (ngx_pagesize - 1), i.e. 4095 on x86.
* On Windows NT it decreases a number of locked pages in a kernel.
*/
#define NGX_MAX_ALLOC_FROM_POOL (ngx_pagesize - 1) #define NGX_DEFAULT_POOL_SIZE (16 * 1024) #define NGX_POOL_ALIGNMENT 16
#define NGX_MIN_POOL_SIZE \
ngx_align((sizeof(ngx_pool_t) + 2 * sizeof(ngx_pool_large_t)), \
NGX_POOL_ALIGNMENT) 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;
}; typedef struct ngx_pool_large_s ngx_pool_large_t; struct ngx_pool_large_s {
ngx_pool_large_t *next;
void *alloc;
}; typedef struct {
u_char *last; // 数据存储的已用区尾地址
u_char *end; // 数据存储区的尾地址
ngx_pool_t *next; // 下一个内存池地址
ngx_uint_t failed; // 失败次数
} ngx_pool_data_t; struct ngx_pool_s {
ngx_pool_data_t d; // 数据区
size_t max; // 内存池的最大存储空间
ngx_pool_t *current; // 内存池
ngx_chain_t *chain;
ngx_pool_large_t *large; // 用于存储大数据,链表结构
ngx_pool_cleanup_t *cleanup; // 用于清理,链表结构
ngx_log_t *log;
}; typedef struct {
ngx_fd_t fd; // 文件描述符,用于 ngx_pool_cleanup_file
u_char *name; // 文件名,用于 ngx_pool_delete_file
ngx_log_t *log;
} ngx_pool_cleanup_file_t; void *ngx_alloc(size_t size, ngx_log_t *log);
void *ngx_calloc(size_t size, ngx_log_t *log); ngx_pool_t *ngx_create_pool(size_t size, ngx_log_t *log);
void ngx_destroy_pool(ngx_pool_t *pool);
void ngx_reset_pool(ngx_pool_t *pool); void *ngx_palloc(ngx_pool_t *pool, size_t size);
void *ngx_pnalloc(ngx_pool_t *pool, size_t size);
void *ngx_pcalloc(ngx_pool_t *pool, size_t size);
void *ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment);
ngx_int_t ngx_pfree(ngx_pool_t *pool, void *p); ngx_pool_cleanup_t *ngx_pool_cleanup_add(ngx_pool_t *p, size_t size);
void ngx_pool_run_cleanup_file(ngx_pool_t *p, ngx_fd_t fd);
void ngx_pool_cleanup_file(void *data);
void ngx_pool_delete_file(void *data);

ngx_palloc.c



static void *ngx_palloc_block(ngx_pool_t *pool, size_t size);
static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); // 创建 size 大小的内存池
ngx_pool_t *
ngx_create_pool(size_t size, ngx_log_t *log)
{
ngx_pool_t *p; p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log);
if (p == NULL) {
return NULL;
} p->d.last = (u_char *) p + sizeof(ngx_pool_t);
p->d.end = (u_char *) p + size;
p->d.next = NULL;
p->d.failed = 0; size = size - sizeof(ngx_pool_t);
p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; p->current = p;
p->chain = NULL;
p->large = NULL;
p->cleanup = NULL;
p->log = log; return p;
} // 销毁内存池 pool
void
ngx_destroy_pool(ngx_pool_t *pool)
{
ngx_pool_t *p, *n;
ngx_pool_large_t *l;
ngx_pool_cleanup_t *c; // 处理 pool->cleanup 链表,处理函数由此前赋值到 pool->cleanup->handler 的函数指针确定
for (c = pool->cleanup; c; c = c->next) {
if (c->handler) {
ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
"run cleanup: %p", c);
c->handler(c->data);
}
} // 释放 pool->large 链表
for (l = pool->large; l; l = l->next) { ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); if (l->alloc) {
ngx_free(l->alloc);
}
} #if (NGX_DEBUG) /*
* we could allocate the pool->log from this pool
* so we cannot use this log while free()ing the pool
*/ 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 // 释放 pool->d 链表
for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
ngx_free(p); if (n == NULL) {
break;
}
}
} // 重置内存池
void
ngx_reset_pool(ngx_pool_t *pool)
{
ngx_pool_t *p;
ngx_pool_large_t *l; // 释放 large 链的每个节点的内存
for (l = pool->large; l; l = l->next) {
if (l->alloc) {
ngx_free(l->alloc);
}
} pool->large = NULL; // 重置数据 d 链的每个节点,即重置每个节点的可用区首地址 d.last
for (p = pool; p; p = p->d.next) {
p->d.last = (u_char *) p + sizeof(ngx_pool_t);
}
} // 从内存池 pool 分配大小为 size 的内存块,并返回其地址
// 是被外部使用最多的内存池相关 API,并且考虑对齐问题
void *
ngx_palloc(ngx_pool_t *pool, size_t size)
{
u_char *m;
ngx_pool_t *p; // 如果还未超出内存池的 max 值,超过了则用 large
if (size <= pool->max) { p = pool->current; do { // 对齐内存
m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); // 该节点剩余可用空间够用
if ((size_t) (p->d.end - m) >= size) {
p->d.last = m + size; return m;
} // 该节点剩余空间不够用,看下一个节点
p = p->d.next; } while (p); // 现有节点都不给力,重新分配一个 d 节点
return ngx_palloc_block(pool, size);
} // size 超过 pool->max,从 large 取
return ngx_palloc_large(pool, size);
} // 类似 ngx_palloc,不考虑对齐问题
void *
ngx_pnalloc(ngx_pool_t *pool, size_t size)
{
u_char *m;
ngx_pool_t *p; if (size <= pool->max) { p = pool->current; do {
m = p->d.last; if ((size_t) (p->d.end - m) >= size) {
p->d.last = m + size; return m;
} p = p->d.next; } while (p); return ngx_palloc_block(pool, size);
} return ngx_palloc_large(pool, size);
} static void *
ngx_palloc_block(ngx_pool_t *pool, size_t size)
{
u_char *m;
size_t psize;
ngx_pool_t *p, *new, *current; // pool 结构定义区和 pool->d 数据区的总大小
psize = (size_t) (pool->d.end - (u_char *) pool); // 分配 psize 大小的内存
m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log);
if (m == NULL) {
return NULL;
} // 用 new 来表示上面分配的新内存块
new = (ngx_pool_t *) m; // 初始化这个 new,设定 new 的 d.end、d.next、d.failed
new->d.end = m + psize;
new->d.next = NULL;
new->d.failed = 0; // m 加上内存池数据定义结构体的大小
m += sizeof(ngx_pool_data_t);
// 内存对齐 m
m = ngx_align_ptr(m, NGX_ALIGNMENT);
// 设定 new 的 d.last
new->d.last = m + size; current = pool->current; // TODO
for (p = current; p->d.next; p = p->d.next) {
if (p->d.failed++ > 4) {
current = p->d.next;
}
} // new 节点放入内存池数据链
p->d.next = new; pool->current = current ? current : new; return m;
} static void *
ngx_palloc_large(ngx_pool_t *pool, size_t size)
{
void *p;
ngx_uint_t n;
ngx_pool_large_t *large; // 分配 size 大小的内存
p = ngx_alloc(size, pool->log);
if (p == NULL) {
return NULL;
} n = 0; // 在 pool 的 large 链中寻找存储区为空的节点,把新分配的内存区首地址赋给它
for (large = pool->large; large; large = large->next) { // 找到 large 链末尾,在其后插入之,并返回给外部使用
if (large->alloc == NULL) {
large->alloc = p;
return p;
} // 查看的 large 节点超过 3 个,不再尝试和寻找,由下面代码实现创建新 large 节点的逻辑
if (n++ > 3) {
break;
}
} // 创建 large 链的一个新节点,如果失败则释放刚才创建的 size 大小的内存,并返回 NULL
large = ngx_palloc(pool, sizeof(ngx_pool_large_t));
if (large == NULL) {
ngx_free(p);
return NULL;
} // 一切顺利,善后工作
large->alloc = p;
large->next = pool->large;
pool->large = large; return p;
} void *
ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment)
{
void *p;
ngx_pool_large_t *large; // 创建一块 size 大小的内存,内存以 alignment 字节对齐
p = ngx_memalign(alignment, size, pool->log);
if (p == NULL) {
return NULL;
} // 创建一个 large 节点
large = ngx_palloc(pool, sizeof(ngx_pool_large_t));
if (large == NULL) {
ngx_free(p);
return NULL;
} // 将这个新的 large 节点交付给 pool 的 large 字段
large->alloc = p;
large->next = pool->large;
pool->large = large; return p;
} ngx_int_t
ngx_pfree(ngx_pool_t *pool, void *p)
{
ngx_pool_large_t *l; // 逐一释放 large 链表的每一个节点
for (l = pool->large; l; l = l->next) {
if (p == l->alloc) {
ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
"free: %p", l->alloc);
ngx_free(l->alloc);
l->alloc = NULL; return NGX_OK;
}
} return NGX_DECLINED;
} // 封装 palloc 为 pcalloc,实现分配内存并初始化为 0
void *
ngx_pcalloc(ngx_pool_t *pool, size_t size)
{
void *p; p = ngx_palloc(pool, size);
if (p) {
ngx_memzero(p, size);
} return p;
} // 向 cleanup 链添加 p->cleanup 这个节点
ngx_pool_cleanup_t *
ngx_pool_cleanup_add(ngx_pool_t *p, size_t size)
{
ngx_pool_cleanup_t *c; // 创建一个 cleanup 节点
c = ngx_palloc(p, sizeof(ngx_pool_cleanup_t));
if (c == NULL) {
return NULL;
} if (size) {
// cleanup 节点数据区
c->data = ngx_palloc(p, size);
if (c->data == NULL) {
return NULL;
} } else {
c->data = NULL;
} // 善后
c->handler = NULL;
c->next = p->cleanup; p->cleanup = c; ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, p->log, 0, "add cleanup: %p", c); return c;
} // 查找指定的 fd,且其 handler 为 ngx_pool_cleanup_file,执行相应动作
// 这里面有一个遍历的操作
void
ngx_pool_run_cleanup_file(ngx_pool_t *p, ngx_fd_t fd)
{
ngx_pool_cleanup_t *c;
ngx_pool_cleanup_file_t *cf; for (c = p->cleanup; c; c = c->next) {
if (c->handler == ngx_pool_cleanup_file) { cf = c->data; if (cf->fd == fd) {
c->handler(cf);
c->handler = NULL;
return;
}
}
}
} // 释放文件描述符
void
ngx_pool_cleanup_file(void *data)
{
ngx_pool_cleanup_file_t *c = data; ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, c->log, 0, "file cleanup: fd:%d",
c->fd); if (ngx_close_file(c->fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
ngx_close_file_n " \"%s\" failed", c->name);
}
} // 从文件系统删除文件,data 指针指向一个 ngx_pool_cleanup_file_t 类型的数据
void
ngx_pool_delete_file(void *data)
{
ngx_pool_cleanup_file_t *c = data; ngx_err_t err; ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, c->log, 0, "file cleanup: fd:%d %s",
c->fd, c->name); // 删除文件
if (ngx_delete_file(c->name) == NGX_FILE_ERROR) {
err = ngx_errno; if (err != NGX_ENOENT) {
ngx_log_error(NGX_LOG_CRIT, c->log, err,
ngx_delete_file_n " \"%s\" failed", c->name);
}
} // 关闭对应的文件描述符
if (ngx_close_file(c->fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
ngx_close_file_n " \"%s\" failed", c->name);
}
} #if 0 static void *
ngx_get_cached_block(size_t size)
{
void *p;
ngx_cached_block_slot_t *slot; if (ngx_cycle->cache == NULL) {
return NULL;
} slot = &ngx_cycle->cache[(size + ngx_pagesize - 1) / ngx_pagesize]; slot->tries++; if (slot->number) {
p = slot->block;
slot->block = slot->block->next;
slot->number--;
return p;
} return NULL;
}

Nginx源码完全注释(7)ngx_palloc.h/ngx_palloc.c的更多相关文章

  1. Nginx源码完全注释(6)core/murmurhash

    下面是摘自 Google Code 的 Murmurhash 开源项目主页上的 Murmurhash2,Nginx 就是采用的这个. uint32_t MurmurHash2 ( const void ...

  2. Nginx 源码完全注释(11)ngx_spinlock

    Nginx 是多进程模式的,一个 master 与多个 workers,一般工作在多核 CPU 上,所以自旋锁就是必须用到的.Nginx 中的自旋锁的定义,位于 ngx_spinlock.c 中,如下 ...

  3. Nginx源码完全注释(2)ngx_array.h / ngx_array.c

    数组头文件 ngx_array.h #include <ngx_config.h> #include <ngx_core.h> struct ngx_array_s { voi ...

  4. Nginx源码完全注释(3)ngx_list.h / ngx_list.c

    列表头文件ngx_list.h #ifndef _NGX_LIST_H_INCLUDED_ #define _NGX_LIST_H_INCLUDED_ #include <ngx_config. ...

  5. nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c

    首先看 ngx_alloc.h 文件,主要声明或宏定义了 ngx_alloc,ngx_calloc,ngx_memalign,ngx_free. /* * Copyright (C) Igor Sys ...

  6. Nginx源码完全注释(4)ngx_queue.h / ngx_queue.c

    队列头文件ngx_queue.h #include <ngx_config.h> #include <ngx_core.h> #ifndef _NGX_QUEUE_H_INCL ...

  7. Nginx 源码完全注释(10)ngx_radix_tree

    ngx_radix_tree.h // 未被使用的节点 #define NGX_RADIX_NO_VALUE (uintptr_t) -1 typedef struct ngx_radix_node_ ...

  8. Nginx源码完全注释(9)nginx.c: ngx_get_options

    本文分析 ngxin.c 中的 ngx_get_options 函数,其影响: nginx.c 中的: static ngx_uint_t ngx_show_help; static ngx_uint ...

  9. Nginx源码完全注释(8)ngx_errno.c

    errno.h中的strerror(int errno)可以确定指定的errno的错误的提示信息.在 Nginx 中,将所有错误提示信息预先存储在一个数组里,而预先确定这个数组的大小,是在自动化脚本中 ...

随机推荐

  1. (转)Android短信的发送和接收监听

    /**发送与接收的广播**/     String SENT_SMS_ACTION = "SENT_SMS_ACTION";     String DELIVERED_SMS_AC ...

  2. 2.Python输入pip命令出现Unknown or unsupported command 'install'问题解决

    1.在学习python时,输入pip命令的时候出现以下错误: 2.原因:输入where pip命令查找,发现结果如下图,原因是因为电脑原先装了LoadRunner,导致系统无法识别应该使用哪一个pip ...

  3. app.js:1274 [Vue warn]: Error in render: "TypeError: Cannot read property 'object_id' of undefined"问题小记

    凌晨遇到一个控制台报错的信息,总是显示有对象中的元素未定义 明明是有把定义对象的值的,后面发现是把没有返回值的函数又赋值一遍给未定义的元素所属的对象,

  4. Micro-PaaS(Docker+K8S)

    1.概述 Docker是一种Linux容器工具集,它是为构建(Build).交付(Ship)和运行(Run)分布式应用而设计的. Kubernates:是开源的容器集群管理系统.它构建在Docker技 ...

  5. SQL语句操作全集

    SQL语句操作全集 下列语句部分是MySQL语句 SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDAT ...

  6. websphere删除概要文件(profiles)的方式

    [b]删除概要文件:[/b]方案一:1.找到profileRegistry.xml,在目录IBM\WebSphere\AppServer\properties里,去掉想删除的profile的配置即可. ...

  7. Windows下安装HBase

    本文转载自:http://blog.csdn.net/kangkanglou/article/details/30748139 本文主要参照Hbase官网:http://hbase.apache.or ...

  8. 30个让人兴奋的视差滚动(Parallax Scrolling)效果网站--转

    视差滚动(Parallax Scrolling)是指让多层背景以不同的速度移动,形成立体的运动效果,带来非常出色的视觉体验.作为今年网页设计的热点趋势,越来越多的网站应用了这项技术.今天这篇文章就与大 ...

  9. POJ 2566 Bound Found(尺取法,前缀和)

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5207   Accepted: 1667   Spe ...

  10. maven环境的搭建,lemon-OA办公系统的搭建

    当时要搭建activiti工作流,但是这个工作流是基于maven启动的,于是,学习了一下,maven环境的搭建 准备的环境: Jdk  1.6 Eclipse IDE 一个或者 MyEclipse M ...