码迷,mamicode.com
首页 > 编程语言 > 详细

nginx源码分析——数组

时间:2018-02-16 17:07:54      阅读:348      评论:0      收藏:0      [点我收藏+]

标签:oid   null   body   sizeof   efi   c++   使用   before   分析   

ngx_array.h

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) Nginx, Inc.
 */


#ifndef _NGX_ARRAY_H_INCLUDED_
#define _NGX_ARRAY_H_INCLUDED_


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


typedef struct {
    // elts指针,指向存储数组元素的内存块
    void        *elts;
    // nelts,数组中的元素数量
    ngx_uint_t   nelts;
    // size,单个数组元素的所占内存大小
    size_t       size;
    // nalloc,数组中的元素数量的最大值
    ngx_uint_t   nalloc;
    // pool指针,指向数组创建时所使用的内存池
    ngx_pool_t  *pool;
} ngx_array_t;


// 创建数组(内存池,元素个数,元素大小)
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);

// 添加n个元素
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
     */

    // nelts,数组中的元素数量
    array->nelts = 0;
    // size,单个数组元素的所占内存大小
    array->size = size;
    // nalloc,数组中的元素数量的最大值
    array->nalloc = n;
    // pool指针,指向创建数组时使用的内存池
    array->pool = pool;
    // elts指针,指向存储数组元素的内存块
    array->elts = ngx_palloc(pool, n * size);
    if (array->elts == NULL) {
        return NGX_ERROR;
    }

    return NGX_OK;
}


#endif /* _NGX_ARRAY_H_INCLUDED_ */

 

ngx_array.c

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) Nginx, Inc.
 */


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


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

    // 创建数组
    a = ngx_palloc(p, sizeof(ngx_array_t));
    if (a == NULL) {
        return NULL;
    }

    // 创建数组元素空间
    if (ngx_array_init(a, p, n, size) != NGX_OK) {
        return NULL;
    }

    return a;
}


// 销毁数组
void
ngx_array_destroy(ngx_array_t *a)
{
    ngx_pool_t  *p;

    // pool指针,指向数组创建时使用的内存池
    p = a->pool;

    // 判断数组元素是否位于内存块的最后位置,如果是直接调整内存块的参数进行删除
    if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) {
        p->d.last -= a->size * a->nalloc;
    }

    // 判断数组是否位于内存块的最后位置,如果是直接调整内存的参数进行删除
    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;

    // 判断元素数量是否达到最大值
    if (a->nelts == a->nalloc) {

        /* the array is full(满了) */
    
        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
             */
            p->d.last += a->size;
            a->nalloc++;

        } else {
            /* allocate a new array */
            // 创建于原数组两倍大的数组元素空间
            new = ngx_palloc(p, 2 * size);
            if (new == NULL) {
                return NULL;
            }
            ngx_memcpy(new, a->elts, size);
            a->elts = new;
            a->nalloc *= 2;
        }
    }

    // elt指针,指向新元素的内存地址
    elt = (u_char *) a->elts + a->size * a->nelts;

    // 累加元素数量
    a->nelts++;

    // 返回新元素
    return elt;
}


// 添加n个数组元素
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;

    size = n * a->size;

    // 判断元素数量是否达到最大值
    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
             */
            p->d.last += size;
            a->nalloc += n;

        } else {
            /* allocate a new array */
            // 创建于原数组两倍大的数组元素空间
            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);
            a->elts = new;
            a->nalloc = nalloc;
        }
    }

    // elt指针,指向第一个新元素的内存地址
    elt = (u_char *) a->elts + a->size * a->nelts;
    
    // 累加元素数量
    a->nelts += n;

    // 返回第一个新元素
    return elt;
}

 

nginx源码分析——数组

标签:oid   null   body   sizeof   efi   c++   使用   before   分析   

原文地址:https://www.cnblogs.com/hvicen/p/8450280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!