|
Blender
V2.93
|
#include <stdlib.h>#include <string.h>#include "atomic_ops.h"#include "BLI_utildefines.h"#include "BLI_mempool.h"#include "MEM_guardedalloc.h"#include "BLI_strict_flags.h"Go to the source code of this file.
Classes | |
| struct | BLI_freenode |
| struct | BLI_mempool_chunk |
| struct | BLI_mempool |
Macros | |
| #define | MAKE_ID(a, b, c, d) ((int)(d) << 24 | (int)(c) << 16 | (b) << 8 | (a)) |
| #define | MAKE_ID_8(a, b, c, d, e, f, g, h) |
| #define | FREEWORD |
| #define | USEDWORD MAKE_ID('u', 's', 'e', 'd') |
| #define | USE_CHUNK_POW2 |
| #define | MEMPOOL_ELEM_SIZE_MIN (sizeof(void *) * 2) |
| #define | CHUNK_DATA(chunk) (CHECK_TYPE_INLINE(chunk, BLI_mempool_chunk *), (void *)((chunk) + 1)) |
| #define | NODE_STEP_NEXT(node) ((void *)((char *)(node) + esize)) |
| #define | NODE_STEP_PREV(node) ((void *)((char *)(node)-esize)) |
| #define | CHUNK_OVERHEAD (uint)(MEM_SIZE_OVERHEAD + sizeof(BLI_mempool_chunk)) |
Typedefs | |
| typedef struct BLI_freenode | BLI_freenode |
| typedef struct BLI_mempool_chunk | BLI_mempool_chunk |
Variables | |
| static bool | mempool_debug_memset = false |
Simple, fast memory allocator for allocating many elements of the same size.
Supports:
Definition in file BLI_mempool.c.
| #define CHUNK_DATA | ( | chunk | ) | (CHECK_TYPE_INLINE(chunk, BLI_mempool_chunk *), (void *)((chunk) + 1)) |
Definition at line 142 of file BLI_mempool.c.
| #define CHUNK_OVERHEAD (uint)(MEM_SIZE_OVERHEAD + sizeof(BLI_mempool_chunk)) |
Extra bytes implicitly used for every chunk alloc.
Definition at line 148 of file BLI_mempool.c.
| #define FREEWORD |
Important that this value is an is not aligned with sizeof(void *). So having a pointer to 2/4/8... aligned memory is enough to ensure the freeword will never be used. To be safe, use a word that's the same in both directions.
Definition at line 70 of file BLI_mempool.c.
| #define MAKE_ID | ( | a, | |
| b, | |||
| c, | |||
| d | |||
| ) | ((int)(d) << 24 | (int)(c) << 16 | (b) << 8 | (a)) |
Definition at line 58 of file BLI_mempool.c.
| #define MAKE_ID_8 | ( | a, | |
| b, | |||
| c, | |||
| d, | |||
| e, | |||
| f, | |||
| g, | |||
| h | |||
| ) |
Definition at line 59 of file BLI_mempool.c.
| #define MEMPOOL_ELEM_SIZE_MIN (sizeof(void *) * 2) |
Definition at line 140 of file BLI_mempool.c.
Definition at line 144 of file BLI_mempool.c.
Definition at line 145 of file BLI_mempool.c.
| #define USE_CHUNK_POW2 |
Definition at line 83 of file BLI_mempool.c.
The 'used' word just needs to be set to something besides FREEWORD.
Definition at line 77 of file BLI_mempool.c.
| typedef struct BLI_freenode BLI_freenode |
A free element from BLI_mempool_chunk. Data is cast to this type and stored in BLI_mempool.free as a single linked list, each item BLI_mempool.esize large.
Each element represents a block which BLI_mempool_alloc may return.
| typedef struct BLI_mempool_chunk BLI_mempool_chunk |
A chunk of memory in the mempool stored in BLI_mempool.chunks as a double linked list.
| void* BLI_mempool_alloc | ( | BLI_mempool * | pool | ) |
Definition at line 334 of file BLI_mempool.c.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_freenode::freeword, mempool_chunk_add(), mempool_chunk_alloc(), BLI_freenode::next, NULL, blender::compositor::pool, UNLIKELY, USEDWORD, and VALGRIND_MEMPOOL_ALLOC.
Referenced by bchunk_list_append_only(), bchunk_list_new(), bchunk_new(), BKE_image_pool_acquire_ibuf(), BLI_ghash_ensure_p(), BLI_ghash_ensure_p_ex(), BLI_gset_ensure_p_ex(), BLI_linklist_append_pool(), BLI_linklist_prepend_pool(), BLI_mempool_calloc(), BM_edge_create(), bm_face_create__internal(), bm_log_face_alloc(), bm_log_vert_alloc(), bm_loop_create(), BM_mesh_beautify_fill(), BM_mesh_rebuild(), bm_uuidwalk_facestep_begin(), bm_uuidwalk_pass_add(), BM_vert_create(), bmo_flag_layer_free(), BMW_state_add(), check_persistent(), CustomData_bmesh_alloc_block(), direct_link_area(), do_moviecache_put(), DRW_instance_data_next(), edge_queue_insert(), ghash_copy(), ghash_insert_ex(), ghash_insert_ex_keyonly(), main_relations_create_idlink_cb(), outliner_storage_cleanup(), seq_cache_allocate_key(), seq_cache_put_ex(), state_link_add(), tbb_task_pool_run(), TEST(), and vs_add().
| void BLI_mempool_as_array | ( | BLI_mempool * | pool, |
| void * | data | ||
| ) |
Fill in data with the contents of the mempool.
Definition at line 510 of file BLI_mempool.c.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_mempool_iternew(), BLI_mempool_iterstep(), data, NODE_STEP_NEXT, and blender::compositor::pool.
Referenced by BLI_mempool_as_arrayN().
| void* BLI_mempool_as_arrayN | ( | BLI_mempool * | pool, |
| const char * | allocstr | ||
| ) |
A version of BLI_mempool_as_array that allocates and returns the data.
Definition at line 527 of file BLI_mempool.c.
References BLI_mempool_as_array(), data, MEM_malloc_arrayN, and blender::compositor::pool.
| void BLI_mempool_as_table | ( | BLI_mempool * | pool, |
| void ** | data | ||
| ) |
Fill in data with pointers to each element of the mempool, to create lookup table.
| pool | Pool to create a table from. |
| data | array of pointers at least the size of 'pool->totused' |
Definition at line 484 of file BLI_mempool.c.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_mempool_iternew(), BLI_mempool_iterstep(), data, and blender::compositor::pool.
Referenced by BLI_mempool_as_tableN().
| void** BLI_mempool_as_tableN | ( | BLI_mempool * | pool, |
| const char * | allocstr | ||
| ) |
A version of BLI_mempool_as_table that allocates and returns the data.
Definition at line 500 of file BLI_mempool.c.
References BLI_mempool_as_table(), data, MEM_mallocN, and blender::compositor::pool.
| void* BLI_mempool_calloc | ( | BLI_mempool * | pool | ) |
Definition at line 362 of file BLI_mempool.c.
References BLI_mempool_alloc(), and blender::compositor::pool.
Referenced by BM_edge_create(), bm_face_boundary_add(), bm_face_create__internal(), bm_face_create__sfme(), BM_mesh_elem_toolflags_ensure(), BM_mesh_rebuild(), BM_vert_create(), bmo_flag_layer_alloc(), bmo_planar_faces_exec(), curve_draw_event_add(), curve_draw_stroke_from_operator_elem(), do_versions_after_linking_280(), knife_append_list(), new_knife_edge(), and new_knife_vert().
| void BLI_mempool_clear | ( | BLI_mempool * | pool | ) |
Wrap BLI_mempool_clear_ex with no reserve set.
Definition at line 749 of file BLI_mempool.c.
References BLI_mempool_clear_ex(), and blender::compositor::pool.
Referenced by BLI_array_store_clear(), bm_uuidwalk_clear(), bmo_planar_faces_exec(), and tbb_task_pool_work_and_wait().
| void BLI_mempool_clear_ex | ( | BLI_mempool * | pool, |
| const int | totelem_reserve | ||
| ) |
Empty the pool, as if it were just created.
| pool | The pool to clear. |
| totelem_reserve | Optionally reserve how many items should be kept from clearing. |
Definition at line 694 of file BLI_mempool.c.
References mempool_chunk_add(), mempool_chunk_find(), mempool_chunk_free(), mempool_maxchunks(), BLI_mempool_chunk::next, NULL, blender::compositor::pool, VALGRIND_CREATE_MEMPOOL, and VALGRIND_DESTROY_MEMPOOL.
Referenced by BLI_ghash_clear_ex(), BLI_mempool_clear(), and DRW_instance_data_list_resize().
| BLI_mempool* BLI_mempool_create | ( | uint | esize, |
| uint | totelem, | ||
| uint | pchunk, | ||
| uint | flag | ||
| ) |
Definition at line 268 of file BLI_mempool.c.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, CHUNK_OVERHEAD, MAX2, MEM_mallocN, MEM_SIZE_OVERHEAD, mempool_chunk_add(), mempool_chunk_alloc(), MEMPOOL_ELEM_SIZE_MIN, mempool_maxchunks(), NULL, blender::compositor::pool, power_of_2_max_u(), and VALGRIND_CREATE_MEMPOOL.
Referenced by BKE_image_pool_new(), BKE_main_relations_create(), BKE_pbvh_bmesh_update_topology(), BLI_array_store_create(), bm_log_entry_create(), bm_mempool_init(), bm_mempool_init_ex(), BM_mesh_beautify_fill(), BM_mesh_edgeloops_find_path(), BM_mesh_edgenet(), BM_mesh_elem_toolflags_ensure(), bm_uuidwalk_init(), bmo_connect_vert_pair_exec(), bmo_flag_layer_alloc(), bmo_flag_layer_free(), bmo_planar_faces_exec(), BMW_init(), check_persistent(), curve_draw_init(), CustomData_bmesh_init_pool(), direct_link_area(), do_versions_after_linking_280(), drw_instance_data_create(), ghash_new(), IMB_moviecache_create(), knifetool_init(), outliner_storage_cleanup(), seq_cache_create(), tbb_task_pool_create(), and TEST().
| void BLI_mempool_destroy | ( | BLI_mempool * | pool | ) |
Free the mempool its self (and all elements).
Definition at line 757 of file BLI_mempool.c.
References MEM_freeN, mempool_chunk_free_all(), blender::compositor::pool, and VALGRIND_DESTROY_MEMPOOL.
Referenced by BKE_image_pool_free(), BKE_main_relations_free(), BKE_pbvh_bmesh_update_topology(), BLI_array_store_destroy(), BLI_ghash_free(), bm_log_entry_free(), BM_mesh_beautify_fill(), BM_mesh_data_free(), BM_mesh_edgeloops_find_path(), BM_mesh_edgenet(), BM_mesh_elem_toolflags_clear(), BM_mesh_rebuild(), BM_mesh_toolflags_set(), bm_uuidwalk_free(), bmo_connect_vert_pair_exec(), bmo_flag_layer_alloc(), bmo_flag_layer_free(), bmo_planar_faces_exec(), BMW_end(), curve_draw_exit(), CustomData_bmesh_merge(), DRW_instance_data_free(), IMB_moviecache_free(), knifetool_exit_ex(), outliner_free(), outliner_storage_cleanup(), seq_cache_destruct(), tbb_task_pool_free(), TEST(), and update_data_blocks().
| void* BLI_mempool_findelem | ( | BLI_mempool * | pool, |
| uint | index | ||
| ) |
Definition at line 459 of file BLI_mempool.c.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_mempool_iternew(), BLI_mempool_iterstep(), NULL, and blender::compositor::pool.
Referenced by BM_edge_at_index_find(), BM_face_at_index_find(), and BM_vert_at_index_find().
| void BLI_mempool_free | ( | BLI_mempool * | pool, |
| void * | addr | ||
| ) |
Free an element from the mempool.
Definition at line 374 of file BLI_mempool.c.
References ARRAY_HAS_ITEM, BLI_assert, BLI_MEMPOOL_ALLOW_ITER, CHUNK_DATA, FREEWORD, BLI_freenode::freeword, mempool_chunk_free_all(), mempool_debug_memset, BLI_freenode::next, BLI_mempool_chunk::next, NODE_STEP_NEXT, NODE_STEP_PREV, NULL, blender::compositor::pool, UNLIKELY, and VALGRIND_MEMPOOL_ALLOC.
| BLI_mempool_iter* BLI_mempool_iter_threadsafe_create | ( | BLI_mempool * | pool, |
| const size_t | num_iter | ||
| ) |
Initialize an array of mempool iterators, BLI_MEMPOOL_ALLOW_ITER flag must be set.
This is used in threaded code, to generate as much iterators as needed (each task should have its own), such that each iterator goes over its own single chunk, and only getting the next chunk to iterate over has to be protected against concurrency (which can be done in a lockless way).
To be used when creating a task for each single item in the pool is totally overkill.
See BLI_task_parallel_mempool implementation for detailed usage example.
Definition at line 561 of file BLI_mempool.c.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_mempool_iternew(), BLI_mempool_iter::curchunk, BLI_mempool_iter::curchunk_threaded_shared, MEM_mallocN, NULL, and blender::compositor::pool.
Referenced by BLI_task_parallel_mempool().
| void BLI_mempool_iter_threadsafe_free | ( | BLI_mempool_iter * | iter_arr | ) |
Definition at line 583 of file BLI_mempool.c.
References BLI_assert, BLI_mempool_iter::curchunk_threaded_shared, MEM_freeN, and NULL.
Referenced by BLI_task_parallel_mempool().
| void BLI_mempool_iternew | ( | BLI_mempool * | pool, |
| BLI_mempool_iter * | iter | ||
| ) |
Initialize a new mempool iterator, BLI_MEMPOOL_ALLOW_ITER flag must be set.
Definition at line 537 of file BLI_mempool.c.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_mempool_iter::curchunk, BLI_mempool_iter::curchunk_threaded_shared, BLI_mempool_iter::curindex, NULL, BLI_mempool_iter::pool, and blender::compositor::pool.
Referenced by array_store_free_data(), BKE_screen_area_blend_read_lib(), BKE_screen_foreach_id_screen_area(), BLI_array_store_calc_size_compacted_get(), BLI_array_store_is_valid(), BLI_mempool_as_array(), BLI_mempool_as_table(), BLI_mempool_findelem(), BLI_mempool_iter_threadsafe_create(), BLI_task_parallel_mempool(), bmiter__elem_of_mesh_begin(), curve_draw_exec(), curve_draw_exec_precalc(), curve_draw_stroke_3d(), curve_draw_stroke_to_operator(), fill_treehash(), knife_make_cuts(), knifetool_draw(), lib_link_workspace_layout_restore(), outliner_id_remap(), outliner_storage_cleanup(), and tbb_task_pool_work_and_wait().
| void* BLI_mempool_iterstep | ( | BLI_mempool_iter * | iter | ) |
Step over the iterator, returning the mempool item or NULL.
Definition at line 645 of file BLI_mempool.c.
References atomic_cas_ptr(), CHUNK_DATA, BLI_mempool_iter::curchunk, BLI_mempool_iter::curchunk_threaded_shared, BLI_mempool_iter::curindex, BLI_mempool::esize, FREEWORD, BLI_mempool_chunk::next, NULL, BLI_mempool::pchunk, POINTER_OFFSET, BLI_mempool_iter::pool, ret, and UNLIKELY.
Referenced by array_store_free_data(), BKE_screen_area_blend_read_lib(), BKE_screen_foreach_id_screen_area(), BLI_array_store_calc_size_compacted_get(), BLI_array_store_is_valid(), BLI_mempool_as_array(), BLI_mempool_as_table(), BLI_mempool_findelem(), BLI_task_parallel_mempool(), bmiter__elem_of_mesh_step(), curve_draw_exec(), curve_draw_exec_precalc(), curve_draw_stroke_3d(), curve_draw_stroke_to_operator(), fill_treehash(), knife_make_cuts(), knifetool_draw(), lib_link_workspace_layout_restore(), outliner_id_remap(), outliner_storage_cleanup(), parallel_mempool_func(), and tbb_task_pool_work_and_wait().
| int BLI_mempool_len | ( | BLI_mempool * | pool | ) |
Definition at line 454 of file BLI_mempool.c.
References blender::compositor::pool.
Referenced by BKE_outliner_treehash_create_from_treestore(), BKE_outliner_treehash_rebuild_from_treestore(), BLI_array_store_is_valid(), BLI_ghash_free(), BLI_task_parallel_mempool(), BM_mesh_edgenet(), bmiter__elem_of_mesh_begin(), bmiter__elem_of_mesh_step(), blender::ed::outliner::TreeDisplayScenes::buildTree(), blender::ed::outliner::TreeDisplayDataAPI::buildTree(), curve_draw_exec(), curve_draw_exec_precalc(), curve_draw_stroke_3d(), DRW_instance_data_list_resize(), knifetool_draw(), outliner_storage_cleanup(), and write_space_outliner().
| void BLI_mempool_set_memory_debug | ( | void | ) |
Definition at line 769 of file BLI_mempool.c.
References mempool_debug_memset.
Referenced by arg_handle_debug_mode_set().
|
static |
Initialize a chunk and add into pool->chunks
| pool | The pool to add the chunk into. |
| mpchunk | The new uninitialized chunk (can be malloc'd) |
| last_tail | The last element of the previous chunk (used when building free chunks initially) |
Definition at line 196 of file BLI_mempool.c.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, CHUNK_DATA, FREEWORD, BLI_freenode::freeword, BLI_freenode::next, BLI_mempool_chunk::next, NODE_STEP_NEXT, NODE_STEP_PREV, NULL, blender::compositor::pool, and UNLIKELY.
Referenced by BLI_mempool_alloc(), BLI_mempool_clear_ex(), and BLI_mempool_create().
|
static |
Definition at line 182 of file BLI_mempool.c.
References MEM_mallocN, and blender::compositor::pool.
Referenced by BLI_mempool_alloc(), and BLI_mempool_create().
| BLI_INLINE BLI_mempool_chunk* mempool_chunk_find | ( | BLI_mempool_chunk * | head, |
| uint | index | ||
| ) |
Definition at line 163 of file BLI_mempool.c.
References BLI_mempool_chunk::next.
Referenced by BLI_mempool_clear_ex().
|
static |
Definition at line 253 of file BLI_mempool.c.
References MEM_freeN.
Referenced by BLI_mempool_clear_ex(), and mempool_chunk_free_all().
|
static |
Definition at line 258 of file BLI_mempool.c.
References mempool_chunk_free(), and BLI_mempool_chunk::next.
Referenced by BLI_mempool_destroy(), and BLI_mempool_free().
| BLI_INLINE uint mempool_maxchunks | ( | const uint | totelem, |
| const uint | pchunk | ||
| ) |
Definition at line 177 of file BLI_mempool.c.
Referenced by BLI_mempool_clear_ex(), and BLI_mempool_create().
|
static |
Definition at line 86 of file BLI_mempool.c.
Referenced by BLI_mempool_free(), and BLI_mempool_set_memory_debug().