void *test_pool(void *arg) { jv_pool_t *pool; jv_uint_t i; pool=jv_pool_create(1024); srand(time(NULL)); for(i=0;i<100;i++){ jv_uint_t j=rand()%100; jv_lump_t *lump=jv_pool_alloc(pool,j); if(lump==NULL){ printf("free is error.\n"); continue; } jv_pool_free(pool,lump); } each(pool); jv_pool_destroy(pool); return NULL; }
void test3(void) { jv_pool_t *pool; jv_uint_t i; pool=jv_pool_create(4096); each(pool); srand(time(NULL)); for(i=0;i<100;i++) { jv_uint_t j = rand()%100 + 500; jv_lump_t* lump = (jv_lump_t*)jv_pool_alloc(pool,j); printf("apply memory size:%u\n",j); if(lump==NULL) { printf("free is error.\n"); continue; } jv_pool_free(pool,lump); } each(pool); jv_pool_destroy(pool); }
int main() { jv_pool_t* pool; yahoo_guy_t* guy; jv_queue_t* q; yahoo_t* yahoo; pool = jv_pool_create(1024); //初始化内存池 int i; // 构建队列 const jv_str_t names[] = { jv_string("rainx"), jv_string("xiaozhe"), jv_string("zhoujian") }; const int ids[] = { 4611, 8322, 6111 }; yahoo = jv_pool_alloc(pool, sizeof(yahoo_t)); jv_queue_init(&yahoo->queue); //初始化queue for (i = 0; i < 3; i++) { guy = (yahoo_guy_t*) jv_pool_alloc(pool, sizeof(yahoo_guy_t)); guy->id = ids[i]; //guy->name = (char*) jv_palloc(pool, (size_t) (strlen(names[i]) + 1) ); guy->name = (u_char*) jv_strdup(pool, names[i].data); jv_queue_init(&guy->queue); // 从头部进入队列 jv_queue_insert_head(&yahoo->queue, &guy->queue); } // 从尾部遍历输出 for (q = jv_queue_last(&yahoo->queue); q != jv_queue_sentinel(&yahoo->queue); q = jv_queue_prev(q)) { guy = jv_queue_data(q, yahoo_guy_t, queue); printf("No. %d guy in yahoo is %s \n", guy->id, guy->name); } // 找到位于队列中间(若队列有奇数个元素就是正中间,若有偶数个元素则为后半部分的首个元素)的元素输出 { jv_queue_t *m = jv_queue_middle(&yahoo->queue); guy = jv_queue_data(m, yahoo_guy_t, queue); printf("Middle man: ID: %d, Name: %s\n", guy->id, guy->name); } // 排序从头部输出 jv_queue_sort(&yahoo->queue, yahoo_no_cmp); printf("sorting....\n"); for (q = jv_queue_prev(&yahoo->queue); q != jv_queue_sentinel(&yahoo->queue); q = jv_queue_last(q)) { guy = jv_queue_data(q, yahoo_guy_t, queue); printf("No. %d guy in yahoo is %s \n", guy->id, guy->name); } // 找到位于队列中间(若队列有奇数个元素就是正中间,若有偶数个元素则为后半部分的首个元素)的元素输出 { jv_queue_t *m = jv_queue_middle(&yahoo->queue); guy = jv_queue_data(m, yahoo_guy_t, queue); printf("Middle man: ID: %d, Name: %s\n", guy->id, guy->name); } jv_pool_destroy(pool); return 0; }
void test2(void) { jv_pool_t *pool; void *a,*b,*c,*d,*e,*f,*g,*h; jv_lump_t *lump; /* printf("sizeof(jv_pool_t):%u\n",sizeof(jv_pool_t)); */ //pool=jv_pool_create(128); pool=jv_pool_create(5000 * (4096 + 16 * 3)); // printf("pool:%u,pos:%u,block:%u,block->next:%u,lump:%u,lump->next:%u\n",(jv_uint_t)pool,(jv_uint_t)pool->pos,(jv_uint_t)pool->first,(jv_uint_t)pool->first->next,(jv_uint_t)pool->lump,(jv_uint_t)pool->lump->next); // printf("\n"); // assert((u_char *)pool->first+sizeof(jv_block_t)==(u_char *)pool); // assert((u_char *)pool->first+sizeof(jv_block_t)+sizeof(jv_pool_t)==(u_char *)pool->lump); // assert((u_char *)pool+sizeof(jv_pool_t)==(u_char *)(pool->lump)); // a=jv_pool_alloc(pool,40); // printf("pool:%u,pos:%u,block:%u,block->next:%u,lump:%u,lump->next:%u\n",(jv_uint_t)pool,(jv_uint_t)pool->pos,(jv_uint_t)pool->first,(jv_uint_t)pool->first->next,(jv_uint_t)pool->lump,(jv_uint_t)pool->lump->next); // printf("a address:%u,pos:%u\n",(jv_uint_t)a,(jv_uint_t)pool->pos); // assert((u_char *)a==(u_char *)pool+sizeof(jv_pool_t)+sizeof(jv_lump_t)+32+sizeof(jv_lump_t)); // printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n\n"); // b=jv_pool_alloc(pool,56); // printf("pool:%u,pos:%u,block:%u,block->next:%u,lump:%u,lump->next:%u\n",(jv_uint_t)pool,(jv_uint_t)pool->pos,(jv_uint_t)pool->first,(jv_uint_t)pool->first->next,(jv_uint_t)pool->lump,(jv_uint_t)pool->lump->next); // printf("b address:%u,pos:%u\n",(jv_uint_t)b,(jv_uint_t)pool->pos); // assert((u_char *)b==(u_char *)pool->first->next+sizeof(jv_block_t)+sizeof(jv_lump_t)+52); // //each(pool); // printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n\n"); // c=jv_pool_alloc(pool,32); /* (32,36) */ // printf("pool:%u,pos:%u,block:%u,block->next:%u,lump:%u,lump->next:%u\n",(jv_uint_t)pool,(jv_uint_t)pool->pos,(jv_uint_t)pool->first,(jv_uint_t)pool->first->next,(jv_uint_t)pool->lump,(jv_uint_t)pool->lump->next); // printf("c address:%u,pos:%u\n",(jv_uint_t)c,(jv_uint_t)pool->pos); // assert((u_char *)c==(u_char *)pool->first->next+sizeof(jv_block_t)+sizeof(jv_lump_t)); //// each(pool); // printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n\n"); // d=jv_pool_alloc(pool,8); // printf("pool:%u,pos:%u,block:%u,block->next:%u,lump:%u,lump->next:%u\n",(jv_uint_t)pool,(jv_uint_t)pool->pos,(jv_uint_t)pool->first,(jv_uint_t)pool->first->next,(jv_uint_t)pool->lump,(jv_uint_t)pool->lump->next); // printf("d address:%u,pos:%u\n",(jv_uint_t)d,(jv_uint_t)pool->pos); // assert((u_char *)d==(u_char *)pool+sizeof(jv_pool_t)+sizeof(jv_lump_t)+24); // printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n\n"); // e=jv_pool_alloc(pool,4); /* (4,8) */ // printf("pool:%u,pos:%u,block:%u,block->next:%u,lump:%u,lump->next:%u\n",(jv_uint_t)pool,(jv_uint_t)pool->pos,(jv_uint_t)pool->first,(jv_uint_t)pool->first->next,(jv_uint_t)pool->lump,(jv_uint_t)pool->lump->next); // printf("e address:%u,pos:%u\n",(jv_uint_t)e,(jv_uint_t)pool->pos); // assert((u_char *)e==(u_char *)pool+sizeof(jv_pool_t)+sizeof(jv_lump_t)); // printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n\n"); // f=jv_pool_alloc(pool,24); // printf("pool:%u,pos:%u,block:%u,block->next->next:%u,lump:%u,lump->next:%u\n",(jv_uint_t)pool,(jv_uint_t)pool->pos,(jv_uint_t)pool->first,(jv_uint_t)pool->first->next->next,(jv_uint_t)pool->lump,(jv_uint_t)pool->lump->next); // printf("f address:%u,pos:%u\n",(jv_uint_t)f,(jv_uint_t)pool->pos); // assert((u_char *)f==(u_char *)pool->first->next->next+sizeof(jv_block_t)+sizeof(jv_lump_t)+68+sizeof(jv_lump_t)); // printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n\n"); // g=jv_pool_alloc(pool,104); // printf("pool:%u,pos:%u,block:%u,block->next->next:%u,lump:%u,lump->next:%u\n",(jv_uint_t)pool,(jv_uint_t)pool->pos,(jv_uint_t)pool->first,(jv_uint_t)pool->first->next->next,(jv_uint_t)pool->lump,(jv_uint_t)pool->lump->next); // printf("g address:%u,pos:%u\n",(jv_uint_t)g,(jv_uint_t)pool->pos); // assert((u_char *)g==(u_char *)pool->first->next->next->next+sizeof(jv_block_t)+sizeof(jv_lump_t)); /*jeach(pool); v_pool_free(pool,c); lump=((jv_lump_t *)((u_char *)pool->first->next+sizeof(jv_block_t))); assert(lump->used==0); assert(lump->size==36); jv_pool_free(pool,f); lump=((jv_lump_t *)((u_char *)pool->first->next->next+sizeof(jv_block_t)+sizeof(jv_lump_t)+68)); assert(lump->used==0); assert(lump->size==24); jv_pool_free(pool,a); lump=((jv_lump_t *)((u_char *)pool->first+sizeof(jv_block_t)+sizeof(jv_pool_t)+sizeof(jv_lump_t)+32)); assert(lump->used==0); assert(lump->size==40); jv_pool_free(pool,e); lump=((jv_lump_t *)((u_char *)pool->first+sizeof(jv_block_t)+sizeof(jv_pool_t))); assert(lump->used==0); assert(lump->size==8); jv_pool_free(pool,d); lump=((jv_lump_t *)((u_char *)pool->first+sizeof(jv_block_t)+sizeof(jv_pool_t))); assert(lump->used==0); assert(lump->size==88); jv_pool_free(pool,b); lump=((jv_lump_t *)((u_char *)pool->first->next+sizeof(jv_block_t))); assert(lump->used==0); assert(lump->size==108); jv_pool_free(pool,g); lump=((jv_lump_t *)((u_char *)pool->first->next->next+sizeof(jv_block_t))); assert(lump->used==0); assert(lump->size==108);*/ each(pool); h=jv_pool_alloc(pool,1000); each(pool); jv_pool_free(pool,h); each(pool); /* char* sss = (char*)malloc(4); assert(jv_pool_free(pool, sss)==JV_ERROR); free(sss);*/ jv_pool_destroy(pool); }