int main(/* int argc, char **argv */) { ngx_pool_t *pool = NULL; ngx_array_t *array = NULL; ngx_hash_t *hash; printf("--------------------------------\n"); printf("create a new pool:\n"); printf("--------------------------------\n"); pool = ngx_create_pool(1024, &ngx_log); dump_pool(pool); printf("--------------------------------\n"); printf("create and add urls to it:\n"); printf("--------------------------------\n"); array = add_urls_to_array(pool); //in fact, here should validate array dump_hash_array(array); printf("--------------------------------\n"); printf("the pool:\n"); printf("--------------------------------\n"); dump_pool(pool); hash = init_hash(pool, array); if (hash == NULL) { printf("Failed to initialize hash!\n"); return -1; } printf("--------------------------------\n"); printf("the hash:\n"); printf("--------------------------------\n"); dump_hash(hash, array); printf("\n"); printf("--------------------------------\n"); printf("the pool:\n"); printf("--------------------------------\n"); dump_pool(pool); //find test printf("--------------------------------\n"); printf("find test:\n"); printf("--------------------------------\n"); find_test(hash, urls, Max_Num); printf("\n"); find_test(hash, urls2, Max_Num2); //release ngx_array_destroy(array); ngx_destroy_pool(pool); return 0; }
void ngx_array_t_test() { ngx_pool_t* pool; int i = 0; printf("--------------------------------\n"); printf("the size of ngx_array_t: \n"); printf("--------------------------------\n"); printf("%lu\n\n", sizeof(ngx_array_t)); printf("--------------------------------\n"); printf("create a new pool: \n"); printf("--------------------------------\n"); pool = ngx_create_pool(1024, NULL); dump_pool(pool); printf("--------------------------------\n"); printf("alloc an array from the pool: \n"); printf("--------------------------------\n"); ngx_array_t* array = ngx_array_create(pool, 10, sizeof(int)); dump_pool(pool); for(i = 0; i < 100; ++i) { int* ptr = ngx_array_push(array); *ptr = i + 1; } dump_array(array); ngx_array_destroy(array); ngx_destroy_pool(pool); }
void ngx_queue_t_test() { int i = 0; ngx_pool_t* pool; ngx_queue_t* myque; my_point_queue_t* point; my_point_t points[POINTS_LEN] = { {10, 1}, {20, 9}, {9, 9}, {90, 80}, {5, 3}, {50, 20} }; printf("----------------------------------------\n"); printf("the size of ngx_queue_t: \n"); printf("----------------------------------------\n"); printf("%lu\n\n", sizeof(ngx_queue_t)); printf("----------------------------------------\n"); printf("create a new pool: \n"); printf("----------------------------------------\n"); pool = ngx_create_pool(1024, NULL); dump_pool(pool); printf("----------------------------------------\n"); printf("alloc a queue head and nodes: \n"); printf("----------------------------------------\n"); myque = ngx_palloc(pool, sizeof(ngx_queue_t)); ngx_queue_init(myque); for(i = 0; i < POINTS_LEN; ++i) { point = (my_point_queue_t*)ngx_palloc(pool, sizeof(my_point_queue_t)); point->point.x = points[i].x; point->point.y = points[i].y; ngx_queue_init(&point->queue); ngx_queue_insert_head(myque, &point->queue); } dump_queue_from_tail(myque); printf("\n"); printf("----------------------------------------\n"); printf("sort the queue: \n"); printf("----------------------------------------\n"); ngx_queue_sort(myque, my_point_cmp); dump_queue_from_head(myque); printf("\n"); printf("----------------------------------------\n"); printf("the pool at the end: \n"); printf("----------------------------------------\n"); dump_pool(pool); ngx_destroy_pool(pool); }
int main() { ngx_pool_t *pool; ngx_queue_t *myque; my_point_queue_t *point; my_point_t points[Max_Num] = { {10, 1}, {20, 9}, {9, 9}, {90, 80}, {5, 3}, {50, 20} }; int i; printf("--------------------------------\n"); printf("create a new pool:\n"); printf("--------------------------------\n"); pool = ngx_create_pool(1024, NULL); dump_pool(pool); printf("--------------------------------\n"); printf("alloc a queue head and nodes :\n"); printf("--------------------------------\n"); myque =ngx_palloc(pool, sizeof(ngx_queue_t)); //alloc a queue head ngx_queue_init(myque); //init the queue //insert some points into the queue for (i = 0; i < Max_Num; i++) { point = (my_point_queue_t*)ngx_palloc(pool, sizeof(my_point_queue_t)); point->point.x = points[i].x; point->point.y = points[i].y; ngx_queue_init(&point->queue); //insert this point into the points queue ngx_queue_insert_head(myque, &point->queue); } dump_queue_from_tail(myque); printf("\n"); printf("--------------------------------\n"); printf("sort the queue:\n"); printf("--------------------------------\n"); ngx_queue_sort(myque, my_point_cmp); dump_queue_from_head(myque); printf("\n"); printf("--------------------------------\n"); printf("the pool at the end:\n"); printf("--------------------------------\n"); dump_pool(pool); ngx_destroy_pool(pool); return 0; }
int main(int argc, char *argv[]) { ngx_uint_t pool_size = 1024; ngx_pool_t *pool; printf("\ncreate a new pool of %u bytes: \n", pool_size); pool = ngx_create_pool(pool_size, NULL); dump_pool(pool); ngx_destroy_pool(pool); return 0; }
int main() { ngx_pool_t *pool; int i; printf("--------------------------------\n"); printf("create a new pool:\n"); printf("--------------------------------\n"); pool = ngx_create_pool(1024, NULL); dump_pool(pool); printf("--------------------------------\n"); printf("alloc an list from the pool:\n"); printf("--------------------------------\n"); ngx_list_t *list = ngx_list_create(pool, 5, sizeof(int)); dump_pool(pool); for (i = 0; i < 15; i++) { int *ptr = ngx_list_push(list); *ptr = i + 1; } printf("--------------------------------\n"); printf("the list information:\n"); printf("--------------------------------\n"); dump_list(list); printf("--------------------------------\n"); printf("the pool at the end:\n"); printf("--------------------------------\n"); dump_pool(pool); ngx_destroy_pool(pool); return 0; }
int main(int argc, char *argv[]) { ngx_pool_t *pool; ngx_queue_t *myQueue; printf("--------------------------------\n"); printf("create a new pool:\n"); printf("--------------------------------\n"); pool = ngx_create_pool(1024); dump_pool(pool); printf("--------------------------------\n"); printf("alloc a queue head and nodes :\n"); printf("--------------------------------\n"); myQueue = ngx_palloc(pool, sizeof(ngx_queue_t)); //alloc a queue head ngx_queue_init(myQueue); //init the queue return 0; }
void ngx_pool_t_test() { int i = 0; ngx_pool_t* pool; printf("------------------------------\n"); printf("the size of the ngx_pool_data_t: \n"); printf("------------------------------\n"); printf("%lu\n\n", sizeof(ngx_pool_data_t)); printf("------------------------------\n"); printf("the size of the ngx_pool_t: \n"); printf("------------------------------\n"); printf("%lu\n\n", sizeof(ngx_pool_t)); printf("------------------------------\n"); printf("the size of the ngx_pool_large_t: \n"); printf("------------------------------\n"); printf("%lu\n\n", sizeof(ngx_pool_large_t)); printf("------------------------------\n"); printf("create a new pool:\n"); printf("------------------------------\n"); pool = ngx_create_pool(1024, NULL); dump_pool(pool); printf("------------------------------\n"); printf("alloc block 1 from the poll:\n"); printf("------------------------------\n"); ngx_palloc(pool, 512); dump_pool(pool); printf("------------------------------\n"); printf("alloc block 2 from the pool:\n"); printf("------------------------------\n"); ngx_palloc(pool, 512); dump_pool(pool); printf("------------------------------\n"); printf("alloc block 3 from the pool:\n"); printf("------------------------------\n"); ngx_palloc(pool, 512); dump_pool(pool); printf("------------------------------\n"); printf("alloc 1 large memory from the pool: \n"); printf("------------------------------\n"); ngx_palloc(pool, 40960); dump_pool(pool); printf("------------------------------\n"); printf("alloc 2 large memory from the pool: \n"); printf("------------------------------\n"); ngx_palloc(pool, 40960); dump_pool(pool); printf("------------------------------\n"); printf("alloc 3 large memory from the pool:\n"); printf("------------------------------\n"); ngx_palloc(pool, 40960); dump_pool(pool); printf("------------------------------\n"); printf("alloc n large memory from the pool:\n"); printf("------------------------------\n"); for (i = 3; i < 30; ++i) { ngx_palloc(pool, 40960); } dump_pool(pool); ngx_destroy_pool(pool); }
int main(int argc, char *const *argv) { void *ret; void *freeptr; ngx_pool_t *curptr; ngx_int_t rlt; printf("--------pool Test---------\n"); ngx_pool_t *pool; int i=0; unit_create_log(); /*The the value of the ngx_pool_s.max be wrong if missing this call. *Because the ngx_pagesize = getpagesize() should be call for getting real page size. *In function ngx_create_pool: * #define NGX_MAX_ALLOC_FROM_POOL (ngx_pagesize - 1) * size = size - sizeof(ngx_pool_t); * p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; *The max will be size - sizeof(ngx_pool_t) */ ngx_os_init(mylog); ngx_log_error_core(NGX_LOG_NOTICE, mylog, 0,"ngx_create_pool size:%d", NGX_CYCLE_POOL_SIZE); printf("--System memory setting---------\n"); printf("Value of NGX_DEFAULT_POOL_SIZE size = %d\n", NGX_CYCLE_POOL_SIZE); printf("Value of ngx_pagesize:getpagesize() = %d\n", getpagesize()); printf("Value of NGX_MAX_ALLOC_FROM_POOL£ºngx_pagesize-1= %d\n", NGX_MAX_ALLOC_FROM_POOL); printf("size of ngx_pool_data_t = %d\n", sizeof(ngx_pool_data_t)); printf("size of ngx_pool_t = %d\n", sizeof(ngx_pool_t)); printf("size of ngx_pool_large_t= %d\n", sizeof(ngx_pool_large_t)); printf("\n\n--------ngx_create_pool--------\n"); //#define NGX_DEFAULT_POOL_SIZE (16 * 1024) printf("ngx_create_pool using NGX_DEFAULT_POOL_SIZE size = %d\n", NGX_CYCLE_POOL_SIZE); printf("--------------------------------------\n"); pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, mylog); if (pool == NULL) { return NULL; } dump_pool(pool); printf("\n--------size of ngx_align_ptr(p, a):--------\n"); printf("pool->d.last=%p,size of ngx_align_ptr(p, a) = %p\n",pool->d.last,ngx_align_ptr(pool->d.last, NGX_ALIGNMENT)); printf("NGX_ALIGNMENT = %d\n", NGX_ALIGNMENT); printf("\n--------alloc block 1024 from the pool:--------\n"); printf("pool->d.last=%p,size of ngx_align_ptr(p, a) = %p\n",pool->d.last,ngx_align_ptr(pool->d.last, NGX_ALIGNMENT)); ret = ngx_palloc(pool, 1024); printf("--------return address:0x%p--------\n",ret); dump_pool(pool); printf("--------alloc block 1000 from the pool:--------\n"); printf("pool->d.last=%p,size of ngx_align_ptr(p, a) = %p\n",pool->d.last,ngx_align_ptr(pool->d.last, NGX_ALIGNMENT)); ret = ngx_palloc(pool, 1000); printf("--------return address:0x%p--------\n",ret); dump_pool(pool); #if (TEST_ngx_palloc_large) printf("--------alloc large block 3 from the pool:--------\n"); printf("pool->d.last=%p,size of ngx_align_ptr(p, a) = %p\n",pool->d.last,ngx_align_ptr(pool->d.last, NGX_ALIGNMENT)); ret = ngx_palloc(pool, 3*1024); printf("--------return address:0x%p--------\n",ret); dump_pool(pool); printf("--------alloc large block 4 from the pool:--------\n"); printf("pool->d.last=%p,size of ngx_align_ptr(p, a) = %p\n",pool->d.last,ngx_align_ptr(pool->d.last, NGX_ALIGNMENT)); ret = ngx_palloc(pool, 4*1024); freeptr = ret; printf("--------return address:0x%p--------\n",ret); dump_pool(pool); printf("--------alloc large block 5 from the pool:--------\n"); printf("pool->d.last=%p,size of ngx_align_ptr(p, a) = %p\n",pool->d.last,ngx_align_ptr(pool->d.last, NGX_ALIGNMENT)); ret = ngx_palloc(pool, 5*1024); printf("--------return address:0x%p--------\n",ret); dump_pool(pool); printf("--------free large block 4 from the pool:--------\n"); printf("block 4 address = p\n", freeptr); rlt = ngx_pfree(pool,freeptr); if (rlt == NGX_OK){ printf("block 4 address deleted\n"); dump_pool(pool); } else{ printf("ngx_pfree return error=%d\n",rlt); } printf("--------alloc large block 6 from the pool:--------\n"); printf("pool->d.last=%p,size of ngx_align_ptr(p, a) = %p\n",pool->d.last,ngx_align_ptr(pool->d.last, NGX_ALIGNMENT)); ret = ngx_palloc(pool, 6*1024); printf("--------return address:0x%p--------\n",ret); dump_pool(pool); #endif #if (TEST_ngx_palloc_block) for(i=0;i<30;i++){ printf("--------Loop alloc block 1024 from the pool:[%d]--------\n",i); printf("pool->d.last=%p,size of ngx_align_ptr(p, a) = %p\n",pool->d.last,ngx_align_ptr(pool->d.last, NGX_ALIGNMENT)); ret = ngx_palloc(pool, 4095); printf("--------return address:0x%p--------\n",ret); dump_pool(pool); } #endif #if (TEST_ngx_pool_cleanup_add) printf("\n--------test for ngx_pool_cleanup_add : --------\n"); ngx_pool_cleanup_t *cln; void *buf; buf = ngx_pcalloc(pool, 100); cln = ngx_pool_cleanup_add(pool, 0); if (cln == NULL) { printf("\n--------ngx_pool_cleanup_add failure --------\n"); return NGX_ERROR; } cln->handler = ngx_cleanup_callback; cln->data = buf; #endif printf("\n--------call ngx_destroy_pool--------\n"); dump_pool(pool); ngx_destroy_pool(pool); return 0; }
int main(/* int argc, char **argv */) { ngx_pool_t *pool = NULL; ngx_hash_keys_arrays_t array; ngx_hash_combined_t hash; ngx_int_t loop; ngx_array_t *url, *value; ngx_str_t *temp; printf("--------------------------------\n"); printf("create a new pool:\n"); printf("--------------------------------\n"); pool = ngx_create_pool(1024, &ngx_log); dump_pool(pool); printf("--------------------------------\n"); printf("create and add urls to it:\n"); printf("--------------------------------\n"); if ((url = ngx_array_create(pool, Max_Num, sizeof(ngx_str_t))) == NULL) { printf("Failed to initialize url!\n"); return -1; } if ((value = ngx_array_create(pool, Max_Num, sizeof(ngx_str_t))) == NULL) { printf("Failed to initialize value!\n"); return -1; } //常量字符串是不可修改的,而后面需要修改,所以重新拷贝一份 for (loop = 0; loop < Max_Num; loop++) { temp = ngx_array_push(url); temp->len = urls[loop].len; temp->data = ngx_palloc(pool, urls[loop].len); ngx_memcpy(temp->data, urls[loop].data, temp->len); } //由于key-value中的value的地址必须是4对齐的,所以需要重新拷贝一份vaule for (loop = 0; loop < Max_Num; loop++) { temp = ngx_array_push(value); temp->len = values[loop].len; temp->data = ngx_palloc(pool, values[loop].len); ngx_memcpy(temp->data, values[loop].data, temp->len); } if (add_urls_to_array(pool, &array, url, value) == NULL) { printf("Failed to initialize array!\n"); return -1; } dump_hash_array(&array.keys); dump_hash_array(&array.dns_wc_head); dump_hash_array(&array.dns_wc_tail); printf("--------------------------------\n"); printf("the pool:\n"); printf("--------------------------------\n"); dump_pool(pool); if (init_hash(pool, &array, &hash) == NULL) { printf("Failed to initialize hash!\n"); return -1; } printf("buckets = %p\n", hash.hash.buckets); printf("--------------------------------\n"); printf("the hash:\n"); printf("--------------------------------\n"); dump_combined_hash(&hash, &array); printf("\n"); printf("--------------------------------\n"); printf("the pool:\n"); printf("--------------------------------\n"); dump_pool(pool); //find test printf("--------------------------------\n"); printf("find test:\n"); printf("--------------------------------\n"); find_test(&hash, urls, Max_Num); printf("\n"); find_test(&hash, urls2, Max_Num2); //release return 0; }