nginx源碼分析——數組
阿新 • • 發佈:2018-02-16
空間 svc oca n個元素 使用 copy style 返回 out
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源碼分析——數組