Toggle tbox global memory allocator

2015/12/21 10:29
Reading 433

The default memory allocation of tbox is completely based on its own memory pool architecture. It supports fast memory allocation, optimization of fragments, and various memory leak and overflow detection.

If you don't want to use the built-in default memory allocation management of tbox, you can also flexibly switch to other allocation modes, because tbox now fully supports the allocator architecture. As long as different allocator models are imported in the init phase, you can quickly switch the allocation mode, for example:

 --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());

If you think these allocators are not enough, you can customize your own memory allocator to let tbox use it. The customization method is also very simple. Here we take the implementation code of tb_native_allocator as an example:

 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;

Is it very simple? It should be noted that the above __tb_debug_decl__ Some debug information is declared in the macro, such as _file, _func, _line The information recorded during memory allocation can be printed out during debugging, or you can use this information to process some advanced memory detection operations, but these are not available under release

So you need to use __tb_debug__ Macro to process separately..

After the allocator is passed into the tb_init interface tb_malloc/tb_ralloc/tb_free/... Wait until all tbox memory allocation interfaces are switched to the new allocator for allocation..

Of course, if you want to directly allocate from a specific allocator, you can also directly call the allocator's allocation interface:

 tb_allocator_malloc(allocator, 10); tb_allocator_ralloc(allocator, data, 100); tb_allocator_free(allocator, data);

wait.


Expand to read the full text
Loading
Click to lead the topic 📣 Post and join the discussion 🔥
Reward
zero comment
zero Collection
zero fabulous
 Back to top
Top