/* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ tb_fixed_pool_ref_t tb_fixed_pool_init_(tb_large_pool_ref_t large_pool, tb_size_t slot_size, tb_size_t item_size, tb_bool_t for_small_pool, tb_fixed_pool_item_init_func_t item_init, tb_fixed_pool_item_exit_func_t item_exit, tb_cpointer_t priv) { // check tb_assert_and_check_return_val(item_size, tb_null); // done tb_bool_t ok = tb_false; tb_fixed_pool_impl_t* impl = tb_null; do { // using the default large pool if (!large_pool) large_pool = tb_large_pool(); tb_assert_and_check_break(large_pool); // make pool impl = (tb_fixed_pool_impl_t*)tb_large_pool_malloc0(large_pool, sizeof(tb_fixed_pool_impl_t), tb_null); tb_assert_and_check_break(impl); // init pool impl->large_pool = large_pool; impl->slot_size = slot_size? slot_size : (tb_page_size() >> 4); impl->item_size = item_size; impl->func_init = item_init; impl->func_exit = item_exit; impl->func_priv = priv; impl->for_small_pool = for_small_pool; tb_assert_and_check_break(impl->slot_size); // init partial slots tb_list_entry_init(&impl->partial_slots, tb_fixed_pool_slot_t, entry, tb_null); // init full slots tb_list_entry_init(&impl->full_slots, tb_fixed_pool_slot_t, entry, tb_null); // ok ok = tb_true; } while (0); // failed? if (!ok) { // exit it if (impl) tb_fixed_pool_exit((tb_fixed_pool_ref_t)impl); impl = tb_null; } // ok? return (tb_fixed_pool_ref_t)impl; }
/* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ tb_fixed_pool_ref_t tb_fixed_pool_init_(tb_allocator_ref_t large_allocator, tb_size_t slot_size, tb_size_t item_size, tb_bool_t for_small, tb_fixed_pool_item_init_func_t item_init, tb_fixed_pool_item_exit_func_t item_exit, tb_cpointer_t priv) { // check tb_assert_and_check_return_val(item_size, tb_null); // done tb_bool_t ok = tb_false; tb_fixed_pool_t* pool = tb_null; do { // no allocator? uses the global allocator if (!large_allocator) large_allocator = tb_allocator(); tb_assert_and_check_break(large_allocator); // make pool pool = (tb_fixed_pool_t*)tb_allocator_large_malloc0(large_allocator, sizeof(tb_fixed_pool_t), tb_null); tb_assert_and_check_break(pool); // init pool pool->large_allocator = large_allocator; pool->slot_size = slot_size? slot_size : (tb_page_size() >> 4); pool->item_size = item_size; pool->func_init = item_init; pool->func_exit = item_exit; pool->func_priv = priv; pool->for_small = for_small; tb_assert_and_check_break(pool->slot_size); // init partial slots tb_list_entry_init(&pool->partial_slots, tb_fixed_pool_slot_t, entry, tb_null); // init full slots tb_list_entry_init(&pool->full_slots, tb_fixed_pool_slot_t, entry, tb_null); // ok ok = tb_true; } while (0); // failed? if (!ok) { // exit it if (pool) tb_fixed_pool_exit((tb_fixed_pool_ref_t)pool); pool = tb_null; } // ok? return (tb_fixed_pool_ref_t)pool; }
tb_large_pool_ref_t tb_large_pool_init(tb_byte_t* data, tb_size_t size) { // init pool return (data && size)? tb_static_large_pool_init(data, size, tb_page_size()) : tb_native_large_pool_init(); }