typedef struct ngx_module_s ngx_module_t; typedef struct ngx_conf_s ngx_conf_t; typedef struct ngx_cycle_s ngx_cycle_t; typedef struct ngx_pool_s ngx_pool_t; typedef struct ngx_chain_s ngx_chain_t; typedef struct ngx_log_s ngx_log_t; typedef struct ngx_open_file_s ngx_open_file_t; typedef struct ngx_command_s ngx_command_t; typedef struct ngx_file_s ngx_file_t; typedef struct ngx_event_s ngx_event_t; typedef struct ngx_event_aio_s ngx_event_aio_t; typedef struct ngx_connection_s ngx_connection_t; typedef struct ngx_thread_task_s ngx_thread_task_t; typedef struct ngx_ssl_s ngx_ssl_t; typedef struct ngx_ssl_connection_s ngx_ssl_connection_t;
typedef struct { void *elts; '實際的數據存儲區起始地址' ngx_uint_t nelts; '數組實際元素個數' size_t size; '數組單個元素的大小,單位是字節' ngx_uint_t nalloc; '數組的容量:表示該數組在不引起擴容的前提下,能夠最多存儲的元素的個數' ngx_pool_t *pool; '該數組結構,分配的的內存在內存池中的位置' } ngx_array_t;
特色:nginx
/** */ 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); 'n個數組數據地址,在內存池中的位置' if (array->elts NULL) { return NGX_ERROR; } return NGX_OK; }
/** 先在內存池中分配一個數組頭,而後在分配 n*size 大小的內存 ngx_pool_t:內存池 ngx_uint_t:數組大小 */ 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) { '初始化n個數組內存空間,存儲數組' return NULL; } return a; }
void ngx_array_destroy(ngx_array_t *a) { ngx_pool_t *p; 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; } }
特色:c++
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; // 數組才個數=1 // 未分配空間>已使用空間+size 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 */ //容量+1 p->d.last += a->size; a->nalloc++; } else { /* allocate a new array */ // 容量*2 new = ngx_palloc(p, 2 * size); if (new NULL) { return NULL; } ngx_memcpy(new, a->elts, size); a->elts = new; a->nalloc *= 2; } } //數字其實地址+個數*size elt = (u_char *) a->elts + a->size * a->nelts; //下一個數的起始地址 a->nelts++; //個數+1 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; 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 = (u_char *) a->elts + a->size * a->nelts; a->nelts += n; return elt; }
//鏈接頭 typedef struct { ngx_list_part_t *last; '指向鏈表中最後一個ngx_list_part_t,用於管理整個鏈表,含義很明確' ngx_list_part_t part; '鏈表第一個節點,表示一塊連續的內存空間。' size_t size; '鏈表中每一個節點中存放元素大小' ngx_uint_t nalloc; '鏈表中每一個節點能夠存放的元素個數。' ngx_pool_t *pool; '鏈表使用的內存池。' } ngx_list_t; //鏈接表 struct ngx_list_part_s { void *elts; '鏈表節點使用的內存塊地址' ngx_uint_t nelts; '當前鏈表節點已經存放的元素個數' ngx_list_part_t *next; '指向鏈表的下一個節點' };
//建立鏈表引用 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; } if (ngx_list_init(list, pool, n, size) != NGX_OK) { return NULL; } return list; } //初始化 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->part.elts = ngx_palloc(pool, n * size); if (list->part.elts NULL) { return NGX_ERROR; } list->part.nelts = 0; list->part.next = NULL; list->last = &list->part; list->size = size; list->nalloc = n; list->pool = pool; return NGX_OK; }