Exemplo n.º 1
0
/* //////////////////////////////////////////////////////////////////////////////////////
 * implementation
 */
tb_bool_t tb_memory_init_env(tb_allocator_ref_t allocator)
{
    // done
    tb_bool_t ok = tb_false;
    do
    {   
        // init page
        if (!tb_page_init()) break;

        // init the native memory
        if (!tb_native_memory_init()) break;

        // init the allocator
#if defined(TB_CONFIG_MICRO_ENABLE) || \
        (defined(__tb_small__) && !defined(__tb_debug__))
        g_allocator = allocator? allocator : tb_native_allocator();
#else
        g_allocator = allocator? allocator : tb_default_allocator(tb_null, 0);
#endif
        tb_assert_and_check_break(g_allocator);

        // ok
        ok = tb_true;

    } while (0);

    // failed? exit it
    if (!ok) tb_memory_exit_env();
    
    // ok?
    return ok;
}
Exemplo n.º 2
0
/* //////////////////////////////////////////////////////////////////////////////////////
 * implementation
 */
tb_bool_t tb_memory_init(tb_allocator_ref_t allocator, tb_byte_t* data, tb_size_t size)
{
    // done
    tb_bool_t ok = tb_false;
    do
    {   
        // init page
        if (!tb_page_init()) break;

        // init the native memory
        if (!tb_native_memory_init()) break;

        // init the allocator
        g_allocator = allocator;

        // init the large pool data
        g_large_pool_data = data;
        g_large_pool_size = size;

        // init the pool
        tb_assert_and_check_break(tb_pool());

        // ok
        ok = tb_true;

    } while (0);

    // failed? exit it
    if (!ok) tb_memory_exit();
    
    // ok?
    return ok;
}
Exemplo n.º 3
0
static tb_handle_t tb_default_allocator_instance_init(tb_cpointer_t* ppriv)
{
    // check
    tb_check_return_val(ppriv, tb_null);

    // the data and size
    tb_value_ref_t  tuple = (tb_value_ref_t)*ppriv;
    tb_byte_t*      data = (tb_byte_t*)tuple[0].ptr;
    tb_size_t       size = tuple[1].ul;

    // clear the private data first
    *ppriv = tb_null;

    // done
    tb_bool_t               ok = tb_false;
    tb_allocator_ref_t      allocator = tb_null;
    tb_allocator_ref_t      large_allocator = tb_null;
    do
    {
        /* init the page first
         *
         * because this allocator may be called before tb_init()
         */
        if (!tb_page_init()) break ;

        /* init the native memory first
         *
         * because this allocator may be called before tb_init()
         */
        if (!tb_native_memory_init()) break ;

        // init large allocator
        large_allocator = tb_large_allocator_init(data, size);
        tb_assert_and_check_break(large_allocator);

        // init allocator
        allocator = tb_default_allocator_init(large_allocator);
        tb_assert_and_check_break(allocator);

        // ok
        ok = tb_true;

    } while (0);

    // failed?
    if (!ok)
    {
        // exit large allocator
        if (large_allocator) tb_allocator_exit(large_allocator);
        large_allocator = tb_null;
    }

    // ok?
    return (tb_handle_t)allocator;
}