Ejemplo n.º 1
0
__tb_no_sanitize_address__ tb_size_t tb_pool_data_size(tb_cpointer_t data)
{
    // check
    tb_check_return_val(data, 0);

    // done
    tb_size_t               size = 0;
    tb_pool_data_head_t*    data_head = tb_null;
    do
    {
        // tbox must be running normally
        tb_check_break(tb_state() == TB_STATE_OK);

        // get global allocator
        tb_allocator_ref_t allocator = tb_allocator();
        tb_check_break(allocator);

        // have this data address?
        tb_check_break(tb_allocator_have(allocator, data));

        // the data head
        data_head = &(((tb_pool_data_head_t*)data)[-1]);
        tb_check_break(data_head->debug.magic == TB_POOL_DATA_MAGIC);

        // ok
        size = data_head->size;

    } while (0);

    // ok?
    return size;
}
Ejemplo n.º 2
0
/* //////////////////////////////////////////////////////////////////////////////////////
 * 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;
}