--The default tbox memory management is adopted, and all features such as memory pool maintenance, fragment optimization, memory leak and overflow detection are enabled --Equivalent to using: tb_default_allocator (tb_null, 0) tb_init(tb_null, tb_null); --The default tbox memory management is adopted, and all features such as memory pool maintenance, fragment optimization, memory leak and overflow detection are enabled --It also uses a whole buffer imported from outside for maintenance, and does not use other native memory tb_init(tb_null, tb_default_allocator((tb_byte_t*)malloc(300 * 1024 * 1024), 300 * 1024 * 1024)); --Use a whole static buffer for maintenance and enable all features such as memory leak and overflow detection --The difference between this and tb_default_allocator is that this allocator is relatively lightweight, with simple internal data structure, less memory consumption, and suitable for low resource environments --However, this allocator does not support fragment optimization, which is easy to generate fragments tb_init(tb_null, tb_static_allocator((tb_byte_t*)malloc(300 * 1024 * 1024), 300 * 1024 * 1024)); --Full use of system native memory allocation, no internal processing and data maintenance, all features depend on the system environment, and tbox no longer supports features such as memory pool and memory detection tb_init(tb_null, tb_native_allocator());
static tb_pointer_t tb_native_allocator_malloc(tb_allocator_ref_t allocator, tb_size_t size __tb_debug_decl__) { // trace tb_trace_d("malloc(%lu) at %s(): %lu, %s", size, func_, line_, file_); // malloc it return malloc(size); } static tb_pointer_t tb_native_allocator_ralloc(tb_allocator_ref_t allocator, tb_pointer_t data, tb_size_t size __tb_debug_decl__) { // trace tb_trace_d("realloc(%p, %lu) at %s(): %lu, %s", data, size, func_, line_, file_); // realloc it return realloc(data, size); } static tb_bool_t tb_native_allocator_free(tb_allocator_ref_t allocator, tb_pointer_t data __tb_debug_decl__) { // trace tb_trace_d("free(%p) at %s(): %lu, %s", data, func_, line_, file_); // free it return free(data); } //Initialize a native allocator tb_allocator_t allocator = {0}; allocator.type = TB_ALLOCATOR_NATIVE; allocator.malloc = tb_native_allocator_malloc; allocator.ralloc = tb_native_allocator_ralloc; allocator.free = tb_native_allocator_free;
tb_allocator_malloc(allocator, 10); tb_allocator_ralloc(allocator, data, 100); tb_allocator_free(allocator, data);