列表头文件ngx_list.h


#ifndef _NGX_LIST_H_INCLUDED_
#define _NGX_LIST_H_INCLUDED_ #include <ngx_config.h>
#include <ngx_core.h> typedef struct ngx_list_part_s ngx_list_part_t; // 一个 part 相当于列表的一个节点
struct ngx_list_part_s {
void *elts; // 数据存储区
ngx_uint_t nelts; // 已存储元素个数
ngx_list_part_t *next; //下一个 part
}; // 列表定义
typedef struct {
ngx_list_part_t *last; // 最后一个part的位置
ngx_list_part_t part; // 表头
size_t size;
ngx_uint_t nalloc; // 列表单个节点的最大元素数
ngx_pool_t *pool; // 内存池
} ngx_list_t; ngx_list_t *ngx_list_create(ngx_pool_t *pool, ngx_uint_t n, size_t size); static ngx_inline ngx_int_t
ngx_list_init(ngx_list_t *list, ngx_pool_t *pool, ngx_uint_t n, size_t size)
{
// 为 list 的第一个元素的数据存储区分配 n * size 的大小
list->part.elts = ngx_palloc(pool, n * size);
if (list->part.elts == NULL) {
return NGX_ERROR;
} list->part.nelts = 0; // 已存元素数为 0
list->part.next = NULL;
list->last = &list->part; // 当前可用,就是表头节点
list->size = size; // 列表每个节点的每个元素的大小(字节数)
list->nalloc = n; // 列表单个节点的最大元素个数
list->pool = pool; // 内存池 return NGX_OK;
} // 列表的每个节点都是一样大的(一样的元素数,每个元素大小一样) /*
*
* the iteration through the list:
*
* part = &list.part;
* data = part->elts;
*
* for (i = 0 ;; i++) {
*
* if (i >= part->nelts) {
* if (part->next == NULL) {
* break;
* }
*
* part = part->next;
* data = part->elts;
* i = 0;
* }
*
* ... data[i] ...
*
* }
*/ void *ngx_list_push(ngx_list_t *list); #endif /* _NGX_LIST_H_INCLUDED_ */

列表源文件ngx_list.c


#include <ngx_config.h>
#include <ngx_core.h> ngx_list_t *
ngx_list_create(ngx_pool_t *pool, ngx_uint_t n, size_t size)
{
ngx_list_t *list; // 链表定义
list = ngx_palloc(pool, sizeof(ngx_list_t));
if (list == NULL) {
return NULL;
} // 链表表头节点的数据存储区
list->part.elts = ngx_palloc(pool, n * size);
if (list->part.elts == NULL) {
return NULL;
} // 链表表头节点的已存元素数为 0
list->part.nelts = 0;
list->part.next = NULL;
list->last = &list->part;
list->size = size;
list->nalloc = n;
list->pool = pool; // 返回这个创建好的列表
return list;
} // 插入一个元素到列表中
void *
ngx_list_push(ngx_list_t *l)
{
void *elt;
ngx_list_part_t *last; last = l->last; // 最后一个节点的已存元素数 == 列表的单个节点的最大元素数,就是需要分配新节点了
if (last->nelts == l->nalloc) { /* the last part is full, allocate a new list part */ // 从列表的内存池,分配一个节点出来给 last
last = ngx_palloc(l->pool, sizeof(ngx_list_part_t));
if (last == NULL) {
return NULL;
} // 从列表的内存池,分配空间给最后一个节点的数据存储区
last->elts = ngx_palloc(l->pool, l->nalloc * l->size);
if (last->elts == NULL) {
return NULL;
} // 最后一个节点的已存元素数为 0
last->nelts = 0;
last->next = NULL; // 新搞出来这个节点,接到最后一个节点后边
l->last->next = last;
// 把这个新节点当成最后一个节点
l->last = last;
} // 最后一个节点的数据存储区起始位置 + 元素大小 * 最后节点的已存元素数
// 就是下一个可以放元素的位置
elt = (char *) last->elts + l->size * last->nelts; // 最后一个节点的已存元素数加1
last->nelts++; // 返回下一个可以放元素的位置
return elt;
}

从上面可以看出,create 是从 pool 分配定义 list 结构的内存,分配表头节点的内存。init 是初始化已有的 list。

Nginx源码完全注释(3)ngx_list.h / ngx_list.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源码完全注释(1)ngx_alloc.h / ngx_alloc.c

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

  5. Nginx源码完全注释(7)ngx_palloc.h/ngx_palloc.c

    ngx_palloc.h /* * NGX_MAX_ALLOC_FROM_POOL should be (ngx_pagesize - 1), i.e. 4095 on x86. * On Windo ...

  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. “RPC好,还是RESTful好?”

    REST 和 RESTful 什么区别?REST,即Representational State Transfer的缩写.翻译过来是表现层状态转换.如果一个架构符合REST原则,就称它为RESTful ...

  2. caddy server 了解

    Caddy 是一个Go写的服务器软件,官方的宣传语“The HTTP/2 web server with automatic HTTPS”以及“Serve The Web Like It's 2016 ...

  3. Errors running builder 'DeploymentBuilder' on project ' 解决方法

    此问题一般发生在Myeclipse 保存文件并自动部署时候. Errors occurred during the build. Errors running builder 'DeploymentB ...

  4. fail2ban的介绍

    fail2ban的介绍 http://www.jb51.net/article/48591.htm http://lilinji.blog.51cto.com/5441000/1784726 fail ...

  5. xunsearch之php索引维护(四)

    1.添加文档 $xs = new XS('njw'); $index = $xs->index; $data = array( 'pid' => 234, // 此字段为主键,必须指定 ' ...

  6. Charles修改返回值的方法(构造返回值最大值的情况,比如100,99) (自己没有试过)

    第一步:save respond到电脑 第二步:打开文件,修改相应的参数 第三步:导入修改后的文件 第四步:手机刷新数据,查看结果

  7. mybatis MySQL返回插入的主键ID,oracle不行

    <insertid=“doSomething"parameterType="map"useGeneratedKeys="true"keyProp ...

  8. CentOS下j2ee环境搭建

    转自:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/01/2994485.html 因为是做j2ee后台开发的,所以在Linux上搭建 ...

  9. IOCP的缓冲区

    IOCP的缓冲区: 应用程序的缓冲区 套接字的缓冲区 TCP的缓冲区 两个会造成WSAENOBUFS错误的原因: 锁定页面达到上限 非分页页面达到上限

  10. SQL Server 2008系统信息查询常用命令 查看表大小、记录数等

    1.返回所有数据库信息(数据库名,创建日期,存储路径等).   use master; GO select * from dbo.sysdatabases 2.返回当前数据库所有对象(可根据type字 ...