菜鳥nginx源碼剖析數據結構篇(一)動態數組ngx_array_tnginx
Author:Echo Chen(陳斌)c++
Email:chenb19870707@gmail.com數組
Blog:Blog.csdn.net/chen19870707數據結構
Date:October 20h, 2014app
ngx_array _t是一個順序容器,支持達到數組容量上限時動態改變數組的大小,相似於STL中vector,具備如下特性:學習
頭文件:http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_array.h測試
源文件:http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_array.cui
typedef struct { void *elts; //elts指向數組的首地址 ngx_uint_t nelts; //nelts是數組中已經使用的元素個數 size_t size; //每一個數組元素佔用內存大小 ngx_uint_t nalloc; //當前數組中能容納袁術個數的總大小 ngx_pool_t *pool; //內存池對象 } ngx_array_t;
//p爲內存池,n爲初始分配的元素的個數,size爲每一個元素佔的內存大小 spa
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; } 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; //分配n個大小爲size的內存,elts指向首地址 array->elts = ngx_palloc(pool, n * size); if (array->elts == NULL) { return NGX_ERROR; } return NGX_OK; } .net
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; } }
//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 */ //再分配預分配nalloc個,如今就有2*nalloc個了 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 */ //內存池尾指針後移一個元素大小,分配內存一個元素,並把nalloc+1 p->d.last += a->size; a->nalloc++; //若是內存池內存不夠了,分配一個新的數組,大小爲兩倍的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; } } //已分配個數+1 ,並返回之 elt = (u_char *) a->elts + a->size * a->nelts; a->nelts++; return elt; }
//a爲要放入的數組,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; //若是內存池中剩餘的夠分配n個元素,從內存池中分配 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; //若是內存池中剩餘的不夠分配n個元素 } else { /* allocate a new array */ //當n比數組預分配個數nalloc大時,分配2n個,不然分配2*nalloc個 nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); new = ngx_palloc(p, nalloc * a->size); if (new == NULL) { return NULL; } //拷貝之前元素,設置nalloc 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; }
研究開源代碼的主要意義就在於理解設計的原理和適用的場合,並在合適的場合使用代碼,若是單純的分析代碼,但都不能使用,確定達不到學習的目的,這裏就給出ngx_array的測試代碼,但願多動手。
1: typedef struct
2: {
3: u_char *name;
4: int age;
5: }Student;
6:
7: ngx_array_t* pArray = ngx_array_create(cf->pool,1,sizeof(Student));
8:
9: Student *pStudent = ngx_array_push(pArray);
10: pStudent->age = 10;
11:
12: Students *pStudents = ngx_array_push_n(pArray,3);
13: pStudents->age = 1;
14: (pStudents + 1 )->age =2;
15: (pStudents + 2 )->age = 3;
16:
17: //遍歷
18: Student *pStudent = pArray->elts;
19: ngx_uint_i = 0;
20: for(; i < pArray->nelts;i++)
21: {
22: a = pStudent + i;
23: //....
24: }