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

数组头文件 ngx_array.h


#include <ngx_config.h>
#include <ngx_core.h>

struct ngx_array_s {
    void        *elts;
    ngx_uint_t   nelts;
    size_t       size;
    ngx_uint_t   nalloc;
    ngx_pool_t  *pool;
};

ngx_array_t *ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size);
void ngx_array_destroy(ngx_array_t *a);
void *ngx_array_push(ngx_array_t *a);
void *ngx_array_push_n(ngx_array_t *a, ngx_uint_t n);

// 初始化似乎没什么好说的
static ngx_inline ngx_int_t
ngx_array_init(ngx_array_t *array, ngx_pool_t *pool, ngx_uint_t n, size_t size)
{
    /*
     * set "array->nelts" before "array->elts", otherwise MSVC thinks
     * that "array->nelts" may be used without having been initialized
     */

    array->nelts = 0;
    array->size = size;
    array->nalloc = n;
    array->pool = pool;

    array->elts = ngx_palloc(pool, n * size);
    if (array->elts == NULL) {
        return NGX_ERROR;
    }

    return NGX_OK;
}

数组源文件 ngx_array.c


#include <ngx_config.h>
#include <ngx_core.h>

/* 创建一个 Nginx 数组,内存池地址为 p,元素个数为 n,每个元素大小为 size */
ngx_array_t *
ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size)
{
    ngx_array_t *a;

    // 调用 ngx_palloc,从 内存池 p,为 Nginx 数组 a 分配内存
    a = ngx_palloc(p, sizeof(ngx_array_t));
    if (a == NULL) {
        return NULL;
    }

    // 调用 ngx_palloc,为 Nginx 数组 a 的 elts 分配内存,大小为 n * size
    a->elts = ngx_palloc(p, n * size);
    if (a->elts == NULL) {
        return NULL;
    }

    // 注意,上面是先分配定义结构区,再定义数据存储区

    // 其他初始化项
    a->nelts = 0; // 已存元素数
    a->size = size; // 每个元素大小(字节)
    a->nalloc = n; // 最大元素数
    a->pool = p; // 内存池地址

    return a;
}

void
ngx_array_destroy(ngx_array_t *a)
{
    ngx_pool_t  *p;

    // 数组 a 的内存池地址
    p = a->pool;

    // 数组 a 的最大存储区的末尾,到了内存池的已用区域的末尾(可用区的开头)
    // 即表示内存池目前最后一段存储的是一个数组的存储区
    if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) {
        // 内存池已用区域末尾向前挪(每个元素大小 x 最大元素个数)字节
        p->d.last -= a->size * a->nalloc;
    }

    // 数组 a 结构体所占用内存的末尾,为内存池的可用区的开头
    // 即表示内存池目前最后一段存储的是一个数组的结构体
    if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) {
        p->d.last = (u_char *) a;
    }

    // 上面先剪掉数据存储区,再剪掉定义结构区
}

void *
ngx_array_push(ngx_array_t *a)
{
    void        *elt, *new;
    size_t       size;
    ngx_pool_t  *p;

    // 数组满了,即已存储元素数 nelts 已达到最大容纳数 nalloc
    if (a->nelts == a->nalloc) {

        /* the array is full */
        // size 为数组的最大容量(最大容纳数 x 每个元素大小)
        size = a->size * a->nalloc;

        p = a->pool;

        // 数组的数据存储区的末尾,到达了内存池的已用区域的末尾
        // 且,内存池的已用区域末尾的下一个位置<=内存池的末尾,就是说内存池还有够用的剩余空间
        if ((u_char *) a->elts + size == p->d.last
            && p->d.last + a->size <= p->d.end)
        {
            /*
             * the array allocation is the last in the pool
             * and there is space for new allocation
             */

            // 分配一个元素,就得把 last 往下挪一个,即已用区域往下挪一个
            p->d.last += a->size;

            // 分配一个元素,且内存池没满,就得把“最大元素数”加1
            a->nalloc++;

        } else { // 内存池剩余空间不够
            /* allocate a new array */

            // 分配一个两倍的空间(原来空间的两倍)
            new = ngx_palloc(p, 2 * size);
            if (new == NULL) {
                return NULL;
            }

            // 把 a->elts 的东东拷贝到 new 里,拷贝 size 字节个数据
            ngx_memcpy(new, a->elts, size);
            a->elts = new; // 新地址
            a->nalloc *= 2; // 新最大容量

            // size 不变(最大容量,即单个元素大小 x 最大元素数)
            // pool 不变
            // nelts 已用元素数不变
        }
    }

    // elt 为已用数据区的末尾
    elt = (u_char *) a->elts + a->size * a->nelts;

    // 已存数据个数加 1
    a->nelts++;

    // 返回这个已用数据区的末尾(可用数据区的开头,或者说是下一个元素可以用的位置)
    return elt;
}

void *
ngx_array_push_n(ngx_array_t *a, ngx_uint_t n)
{
    void        *elt, *new;
    size_t       size;
    ngx_uint_t   nalloc;
    ngx_pool_t  *p;

    // 要放的 n 个元素所占的总字节数
    size = n * a->size;

    // 已存元素数 + 要存的 n 个元素 > 最大容量(最大元素数)
    if (a->nelts + n > a->nalloc) {

        /* the array is full */

        p = a->pool;

        // 数组的数据存储区的末尾,到达了内存池的已用区域的末尾
        // 且,内存池的已用区域末尾的下一个位置<=内存池的末尾,就是说内存池还有够用的剩余空间
        if ((u_char *) a->elts + a->size * a->nalloc == p->d.last
            && p->d.last + size <= p->d.end)
        {
            /*
             * the array allocation is the last in the pool
             * and there is space for new allocation
             */

            // 分配 n 个元素,就得把 last 往后挪 size = n * a->size
            p->d.last += size;

            // 分配 n 个元素,且内存池没满,就得把“最大元素数(最大容量)”加 n
            a->nalloc += n;

        } else { // 内存池剩余空间不够
            /* allocate a new array */

            // 准备分配一个两倍的空间,nalloc是新空间的大小
            // (如果原来最大容量大,则分配原来空间的两倍;如果新要的n个元素更多,则分配n的两倍)
            nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc);

            // 分配新空间
            new = ngx_palloc(p, nalloc * a->size);
            if (new == NULL) {
                return NULL;
            }

            // 把老空间的东东,拷贝到新空间
            ngx_memcpy(new, a->elts, a->nelts * a->size);

            // elts 新空间地址
            a->elts = new;

            // nalloc 数组最大容量
            a->nalloc = nalloc;
        }
    }

    // 要放入的 n 个元素的起始位置
    elt = (u_char *) a->elts + a->size * a->nelts;

    // 已用元素数加 n
    a->nelts += n;

    // 返回n个东东的位置
    return elt;
}
时间: 2024-08-27 21:44:41

Nginx源码完全注释(2)ngx_array.h / ngx_array.c的相关文章

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

下面是摘自 Google Code 的 Murmurhash 开源项目主页上的 Murmurhash2,Nginx 就是采用的这个. uint32_t MurmurHash2 ( const void * key, int len, uint32_t seed ) { // 'm' and 'r' are mixing constants generated offline. // They're not really 'magic', they just happen to work well

Nginx 源码完全注释(11)ngx_spinlock

Nginx 是多进程模式的,一个 master 与多个 workers,一般工作在多核 CPU 上,所以自旋锁就是必须用到的.Nginx 中的自旋锁的定义,位于 ngx_spinlock.c 中,如下: void ngx_spinlock(ngx_atomic_t *lock, ngx_atomic_int_t value, ngx_uint_t spin) { #if (NGX_HAVE_ATOMIC_OPS) ngx_uint_t i, n; for ( ;; ) { // lock 即为锁

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

首先看 ngx_alloc.h 文件,主要声明或宏定义了 ngx_alloc,ngx_calloc,ngx_memalign,ngx_free. /* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc. */ #ifndef _NGX_ALLOC_H_INCLUDED_ #define _NGX_ALLOC_H_INCLUDED_ #include #include void *ngx_alloc(size_t size, ngx_lo

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

队列头文件ngx_queue.h #include <ngx_config.h> #include <ngx_core.h> #ifndef _NGX_QUEUE_H_INCLUDED_ #define _NGX_QUEUE_H_INCLUDED_ typedef struct ngx_queue_s ngx_queue_t; // 队列的节点,也直接表示队列.注意这是一个双向循环队列 struct ngx_queue_s { ngx_queue_t *prev; ngx_queu

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 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 * 102

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

列表头文件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_u

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

errno.h中的strerror(int errno)可以确定指定的errno的错误的提示信息.在 Nginx 中,将所有错误提示信息预先存储在一个数组里,而预先确定这个数组的大小,是在自动化脚本中完成的,如下是auto/unix脚本:(其中自动化脚本auto/feature的作用参考<解剖 Nginx·自动脚本篇(4)工具型脚本系列>一文) // auto/unix ngx_feature="sys_nerr" ngx_feature_name="NGX_SY

Nginx 源码完全注释(10)ngx_radix_tree

ngx_radix_tree.h // 未被使用的节点 #define NGX_RADIX_NO_VALUE (uintptr_t) -1 typedef struct ngx_radix_node_s ngx_radix_node_t; struct ngx_radix_node_s { ngx_radix_node_t *right; // 右子树的根节点 ngx_radix_node_t *left; // 左子树的根节点 ngx_radix_node_t *parent; // 父节点

Nginx源码完全注释(5)core/ngx_cpuinfo.c

/* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc. */ #include <ngx_config.h> #include <ngx_core.h> // 如果 CPU 架构是 i386 或 amd64,并且编译器是 GNU Compiler 或 Intel Compiler,则定义 cngx_puid 函数 // 否则 ngx_cpuid 函数为空 #if (( __i386__ || __amd64__ ) &a