PJ_DEF(void) pj_caching_pool_init( pj_caching_pool *cp, const pj_pool_factory_policy *policy, pj_size_t max_capacity) { int i; pj_pool_t *pool; PJ_CHECK_STACK(); pj_bzero(cp, sizeof(*cp)); cp->max_capacity = max_capacity; pj_list_init(&cp->used_list); for (i=0; i<PJ_CACHING_POOL_ARRAY_SIZE; ++i) pj_list_init(&cp->free_list[i]); if (policy == NULL) { policy = &pj_pool_factory_default_policy; } pj_memcpy(&cp->factory.policy, policy, sizeof(pj_pool_factory_policy)); cp->factory.create_pool = &cpool_create_pool; cp->factory.release_pool = &cpool_release_pool; cp->factory.dump_status = &cpool_dump_status; cp->factory.on_block_alloc = &cpool_on_block_alloc; cp->factory.on_block_free = &cpool_on_block_free; pool = pj_pool_create_on_buf("cachingpool", cp->pool_buf, sizeof(cp->pool_buf)); pj_lock_create_simple_mutex(pool, "cachingpool", &cp->lock); }
/* Test the buffer based pool */ static int pool_buf_test(void) { enum { STATIC_BUF_SIZE = 40 }; /* 16 is the internal struct in pool_buf */ static char buf[ STATIC_BUF_SIZE + sizeof(pj_pool_t) + sizeof(pj_pool_block) + 16]; pj_pool_t *pool; void *p; PJ_USE_EXCEPTION; PJ_LOG(3,("test", "...pool_buf test")); pool = pj_pool_create_on_buf("no name", buf, sizeof(buf)); if (!pool) return -70; /* Drain the pool */ PJ_TRY { if ((p=pj_pool_alloc(pool, STATIC_BUF_SIZE/2)) == NULL) return -75; if ((p=pj_pool_alloc(pool, STATIC_BUF_SIZE/2)) == NULL) return -76; } PJ_CATCH_ANY { return -77; } PJ_END; /* On the next alloc, exception should be thrown */ PJ_TRY { p = pj_pool_alloc(pool, STATIC_BUF_SIZE); if (p != NULL) { /* This is unexpected, the alloc should fail */ return -78; } } PJ_CATCH_ANY { /* This is the expected result */ } PJ_END; /* Done */ return 0; }
/* Test that the alignment works for pool on buf. */ static int pool_buf_alignment_test(void) { pj_pool_t *pool; char buf[512]; void *ptr; enum { LOOP = 100 }; unsigned i; PJ_LOG(3,("test", "...pool_buf alignment test")); pool = pj_pool_create_on_buf(NULL, buf, sizeof(buf)); if (!pool) return -400; for (i=0; i<LOOP; ++i) { /* Test first allocation */ ptr = pj_pool_alloc(pool, 1); if (!IS_ALIGNED(ptr)) { pj_pool_release(pool); return -410; } /* Test subsequent allocation */ ptr = pj_pool_alloc(pool, 1); if (!IS_ALIGNED(ptr)) { pj_pool_release(pool); return -420; } /* Reset the pool */ pj_pool_reset(pool); } /* Done */ return 0; }