isc_result_t isc_pool_create(isc_mem_t *mctx, unsigned int count, isc_pooldeallocator_t free, isc_poolinitializer_t init, void *initarg, isc_pool_t **poolp) { isc_pool_t *pool = NULL; isc_result_t result; unsigned int i; INSIST(count > 0); /* Allocate the pool structure */ result = alloc_pool(mctx, count, &pool); if (result != ISC_R_SUCCESS) return (result); pool->free = free; pool->init = init; pool->initarg = initarg; /* Populate the pool */ for (i = 0; i < count; i++) { result = init(&pool->pool[i], initarg); if (result != ISC_R_SUCCESS) { isc_pool_destroy(&pool); return (result); } } *poolp = pool; return (ISC_R_SUCCESS); }
isc_result_t isc_pool_expand(isc_pool_t **sourcep, unsigned int count, isc_pool_t **targetp) { isc_result_t result; isc_pool_t *pool; REQUIRE(sourcep != NULL && *sourcep != NULL); REQUIRE(targetp != NULL && *targetp == NULL); pool = *sourcep; if (count > pool->count) { isc_pool_t *newpool = NULL; unsigned int i; /* Allocate a new pool structure */ result = alloc_pool(pool->mctx, count, &newpool); if (result != ISC_R_SUCCESS) return (result); newpool->free = pool->free; newpool->init = pool->init; newpool->initarg = pool->initarg; /* Copy over the objects from the old pool */ for (i = 0; i < pool->count; i++) { newpool->pool[i] = pool->pool[i]; pool->pool[i] = NULL; } /* Populate the new entries */ for (i = pool->count; i < count; i++) { result = pool->init(&newpool->pool[i], pool->initarg); if (result != ISC_R_SUCCESS) { isc_pool_destroy(&pool); return (result); } } isc_pool_destroy(&pool); pool = newpool; } *sourcep = NULL; *targetp = pool; return (ISC_R_SUCCESS); }
ATF_TC_BODY(expand_pool, tc) { isc_result_t result; isc_pool_t *pool1 = NULL, *pool2 = NULL, *hold = NULL; UNUSED(tc); result = isc_test_begin(NULL, ISC_TRUE); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); result = isc_pool_create(mctx, 10, poolfree, poolinit, taskmgr, &pool1); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); ATF_REQUIRE_EQ(isc_pool_count(pool1), 10); /* resizing to a smaller size should have no effect */ hold = pool1; result = isc_pool_expand(&pool1, 5, &pool2); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); ATF_REQUIRE_EQ(isc_pool_count(pool2), 10); ATF_REQUIRE_EQ(pool2, hold); ATF_REQUIRE_EQ(pool1, NULL); pool1 = pool2; pool2 = NULL; /* resizing to the same size should have no effect */ hold = pool1; result = isc_pool_expand(&pool1, 10, &pool2); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); ATF_REQUIRE_EQ(isc_pool_count(pool2), 10); ATF_REQUIRE_EQ(pool2, hold); ATF_REQUIRE_EQ(pool1, NULL); pool1 = pool2; pool2 = NULL; /* resizing to larger size should make a new pool */ hold = pool1; result = isc_pool_expand(&pool1, 20, &pool2); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); ATF_REQUIRE_EQ(isc_pool_count(pool2), 20); ATF_REQUIRE(pool2 != hold); ATF_REQUIRE_EQ(pool1, NULL); isc_pool_destroy(&pool2); ATF_REQUIRE_EQ(pool2, NULL); isc_test_end(); }
ATF_TC_BODY(create_pool, tc) { isc_result_t result; isc_pool_t *pool = NULL; UNUSED(tc); result = isc_test_begin(NULL, ISC_TRUE); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); result = isc_pool_create(mctx, 8, poolfree, poolinit, taskmgr, &pool); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); ATF_REQUIRE_EQ(isc_pool_count(pool), 8); isc_pool_destroy(&pool); ATF_REQUIRE_EQ(pool, NULL); isc_test_end(); }
ATF_TC_BODY(get_objects, tc) { isc_result_t result; isc_pool_t *pool = NULL; void *item; isc_task_t *task1 = NULL, *task2 = NULL, *task3 = NULL; UNUSED(tc); result = isc_test_begin(NULL, ISC_TRUE); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); result = isc_pool_create(mctx, 2, poolfree, poolinit, taskmgr, &pool); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); ATF_REQUIRE_EQ(isc_pool_count(pool), 2); item = isc_pool_get(pool); ATF_REQUIRE(item != NULL); isc_task_attach((isc_task_t *) item, &task1); item = isc_pool_get(pool); ATF_REQUIRE(item != NULL); isc_task_attach((isc_task_t *) item, &task2); item = isc_pool_get(pool); ATF_REQUIRE(item != NULL); isc_task_attach((isc_task_t *) item, &task3); isc_task_detach(&task1); isc_task_detach(&task2); isc_task_detach(&task3); isc_pool_destroy(&pool); ATF_REQUIRE_EQ(pool, NULL); isc_test_end(); }