/* ////////////////////////////////////////////////////////////////////////////////////// * 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; }
/* ////////////////////////////////////////////////////////////////////////////////////// * main */ tb_int_t main(tb_int_t argc, tb_char_t** argv) { // init tbox #if 0 if (!tb_init(tb_null, tb_default_allocator((tb_byte_t*)malloc(300 * 1024 * 1024), 300 * 1024 * 1024))) return -1; #elif 0 if (!tb_init(tb_null, tb_static_allocator((tb_byte_t*)malloc(300 * 1024 * 1024), 300 * 1024 * 1024))) return -1; #elif (defined(__tb_valgrind__) && defined(TB_CONFIG_VALGRIND_HAVE_VALGRIND_STACK_REGISTER)) \ || defined(__tb_sanitize_address__) || defined(__tb_sanitize_thread__) if (!tb_init(tb_null, tb_native_allocator())) return -1; #else if (!tb_init(tb_null, tb_null)) return -1; #endif // find the main func from the first argument tb_int_t ok = 0; tb_char_t const* name = tb_null; if (argc > 1 && argv[1]) { tb_size_t i = 0; tb_size_t n = tb_arrayn(g_demo); for (i = 0; i < n; i++) { // find it? if (g_demo[i].name && !tb_stricmp(g_demo[i].name, argv[1])) { // save name name = g_demo[i].name; // done main ok = g_demo[i].main(argc - 1, argv + 1); break; } } } // no this demo? help it if (!name) { tb_trace_i("======================================================================"); tb_trace_i("Usages: xmake r demo [testname] arguments ..."); tb_trace_i(""); tb_trace_i(".e.g"); tb_trace_i(" xmake r demo stream http://www.xxxxx.com /tmp/a"); tb_trace_i(""); // walk name tb_size_t i = 0; tb_size_t n = tb_arrayn(g_demo); for (i = 0; i < n; i++) tb_trace_i("testname: %s", g_demo[i].name); } // exit tbox tb_exit(); // ok? return ok; }