示例#1
0
文件: list.c 项目: siwuxian/xmake
/* //////////////////////////////////////////////////////////////////////////////////////
 * implementation
 */
tb_list_ref_t tb_list_init(tb_size_t grow, tb_element_t element)
{
    // check
    tb_assert_and_check_return_val(element.size && element.data && element.dupl && element.repl, tb_null);

    // done
    tb_bool_t   ok = tb_false;
    tb_list_t*  list = tb_null;
    do
    {
        // using the default grow
        if (!grow) grow = TB_LIST_GROW;

        // make self
        list = tb_malloc0_type(tb_list_t);
        tb_assert_and_check_break(list);

        // init element
        list->element = element;

        // init iterator
        list->itor.mode         = TB_ITERATOR_MODE_FORWARD | TB_ITERATOR_MODE_REVERSE;
        list->itor.priv         = tb_null;
        list->itor.step         = element.size;
        list->itor.size         = tb_list_itor_size;
        list->itor.head         = tb_list_itor_head;
        list->itor.last         = tb_list_itor_last;
        list->itor.tail         = tb_list_itor_tail;
        list->itor.prev         = tb_list_itor_prev;
        list->itor.next         = tb_list_itor_next;
        list->itor.item         = tb_list_itor_item;
        list->itor.copy         = tb_list_itor_copy;
        list->itor.comp         = tb_list_itor_comp;
        list->itor.remove       = tb_list_itor_remove;
        list->itor.remove_range = tb_list_itor_remove_range;

        // init pool, item = entry + data
        list->pool = tb_fixed_pool_init(tb_null, grow, sizeof(tb_list_entry_t) + element.size, tb_null, tb_list_item_exit, (tb_cpointer_t)list);
        tb_assert_and_check_break(list->pool);

        // init head
        tb_list_entry_init_(&list->head, 0, sizeof(tb_list_entry_t) + element.size, tb_null);

        // ok
        ok = tb_true;

    } while (0);

    // failed?
    if (!ok)
    {
        // exit it
        if (list) tb_list_exit((tb_list_ref_t)list);
        list = tb_null;
    }

    // ok?
    return (tb_list_ref_t)list;
}
示例#2
0
文件: face_list.c 项目: cdrr/gbox
/* //////////////////////////////////////////////////////////////////////////////////////
 * implementation
 */
gb_mesh_face_list_ref_t gb_mesh_face_list_init(tb_element_t element)
{
    // check
    tb_assert_and_check_return_val(element.data && element.dupl && element.repl, tb_null);

    // done
    tb_bool_t                   ok = tb_false;
    gb_mesh_face_list_impl_t*   impl = tb_null;
    do
    {
        // make list
        impl = tb_malloc0_type(gb_mesh_face_list_impl_t);
        tb_assert_and_check_break(impl);

        // init element
        impl->element = element;

        // init pool, item = face + data
        impl->pool = tb_fixed_pool_init(tb_null, GB_MESH_FACE_LIST_GROW, sizeof(gb_mesh_face_t) + element.size, tb_null, gb_mesh_face_exit, (tb_cpointer_t)impl);
        tb_assert_and_check_break(impl->pool);

        // init head
        tb_list_entry_init_(&impl->head, 0, sizeof(gb_mesh_face_t) + element.size, tb_null);

        // init order
        impl->order = GB_MESH_ORDER_INSERT_TAIL;

        // ok
        ok = tb_true;

    } while (0);

    // failed?
    if (!ok)
    {
        // exit it
        if (impl) gb_mesh_face_list_exit((gb_mesh_face_list_ref_t)impl);
        impl = tb_null;
    }

    // ok?
    return (gb_mesh_face_list_ref_t)impl;
}
示例#3
0
/* //////////////////////////////////////////////////////////////////////////////////////
 * implementation
 */
gb_mesh_edge_list_ref_t gb_mesh_edge_list_init(tb_element_t element)
{
    // check
    tb_assert_and_check_return_val(element.data && element.dupl && element.repl, tb_null);

    // done
    tb_bool_t                   ok = tb_false;
    gb_mesh_edge_list_impl_t*   impl = tb_null;
    do
    {
        // make list
        impl = tb_malloc0_type(gb_mesh_edge_list_impl_t);
        tb_assert_and_check_break(impl);

        // init element
        impl->element = element;

        // init order
        impl->order = GB_MESH_ORDER_INSERT_TAIL;

        // init edge size
        impl->edge_size = tb_align_cpu(sizeof(gb_mesh_edge_t) + element.size);

        // init iterator
        impl->itor.mode = TB_ITERATOR_MODE_FORWARD | TB_ITERATOR_MODE_REVERSE | TB_ITERATOR_MODE_READONLY;
        impl->itor.priv = tb_null;
        impl->itor.step = impl->edge_size << 1;
        impl->itor.size = gb_mesh_edge_itor_size;
        impl->itor.head = gb_mesh_edge_itor_head;
        impl->itor.last = gb_mesh_edge_itor_last;
        impl->itor.tail = gb_mesh_edge_itor_tail;
        impl->itor.prev = gb_mesh_edge_itor_prev;
        impl->itor.next = gb_mesh_edge_itor_next;
        impl->itor.item = gb_mesh_edge_itor_item;

        // init pool, item = (edge + data) + (edge->sym + data)
        impl->pool = tb_fixed_pool_init(tb_null, GB_MESH_EDGE_LIST_GROW, impl->edge_size << 1, tb_null, gb_mesh_edge_exit, (tb_cpointer_t)impl);
        tb_assert_and_check_break(impl->pool);

        // init head edge
        impl->head[0].sym = &impl->head[1];
        impl->head[1].sym = &impl->head[0];

        // init edge list
        gb_mesh_edge_init(impl->head);

        // ok
        ok = tb_true;

    } while (0);

    // failed?
    if (!ok)
    {
        // exit it
        if (impl) gb_mesh_edge_list_exit((gb_mesh_edge_list_ref_t)impl);
        impl = tb_null;
    }

    // ok?
    return (gb_mesh_edge_list_ref_t)impl;
}
示例#4
0
文件: thread_pool.c 项目: luxuan/tbox
tb_thread_pool_ref_t tb_thread_pool_init(tb_size_t worker_maxn, tb_size_t stack)
{
    // done
    tb_bool_t               ok = tb_false;
    tb_thread_pool_impl_t*  impl = tb_null;
    do
    {
        // make pool
        impl = tb_malloc0_type(tb_thread_pool_impl_t);
        tb_assert_and_check_break(impl);

        // init lock
        if (!tb_spinlock_init(&impl->lock)) break;

        // computate the default worker maxn if be zero
        if (!worker_maxn) worker_maxn = tb_processor_count() << 2;
        tb_assert_and_check_break(worker_maxn);

        // init thread stack
        impl->stack         = stack;

        // init workers
        impl->worker_size   = 0;
        impl->worker_maxn   = worker_maxn;

        // init jobs pool
        impl->jobs_pool     = tb_fixed_pool_init(tb_null, TB_THREAD_POOL_JOBS_POOL_GROW, sizeof(tb_thread_pool_job_t), tb_null, tb_null, tb_null);
        tb_assert_and_check_break(impl->jobs_pool);

        // init jobs urgent
        tb_list_entry_init(&impl->jobs_urgent, tb_thread_pool_job_t, entry, tb_null);

        // init jobs waiting
        tb_list_entry_init(&impl->jobs_waiting, tb_thread_pool_job_t, entry, tb_null);

        // init jobs pending
        tb_list_entry_init(&impl->jobs_pending, tb_thread_pool_job_t, entry, tb_null);

        // init semaphore
        impl->semaphore = tb_semaphore_init(0);
        tb_assert_and_check_break(impl->semaphore);

        // register lock profiler
#ifdef TB_LOCK_PROFILER_ENABLE
        tb_lock_profiler_register(tb_lock_profiler(), (tb_pointer_t)&impl->lock, TB_TRACE_MODULE_NAME);
#endif

        // ok
        ok = tb_true;

    } while (0);

    // failed?
    if (!ok)
    {
        // exit it
        if (impl) tb_thread_pool_exit((tb_thread_pool_ref_t)impl);
        impl = tb_null;
    }

    // ok?
    return (tb_thread_pool_ref_t)impl;
}