|
Blender V5.0
|
#include <algorithm>#include <cstdlib>#include <cstring>#include "atomic_ops.h"#include "BLI_utildefines.h"#include "BLI_asan.h"#include "BLI_math_base.h"#include "BLI_mempool.h"#include "BLI_mempool_private.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 | POISON_REDZONE_SIZE 0 |
| #define | MAKE_ID(a, b, c, d) |
| #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) |
| #define | NODE_STEP_NEXT(node) |
| #define | NODE_STEP_PREV(node) |
| #define | CHUNK_OVERHEAD uint(MEM_SIZE_OVERHEAD + sizeof(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.cc.
| #define CHUNK_DATA | ( | chunk | ) |
Definition at line 129 of file BLI_mempool.cc.
Referenced by BLI_mempool_free(), BLI_mempool_iterstep(), mempool_chunk_add(), and mempool_iter_threadsafe_step().
| #define CHUNK_OVERHEAD uint(MEM_SIZE_OVERHEAD + sizeof(BLI_mempool_chunk)) |
Extra bytes implicitly used for every chunk alloc.
Definition at line 136 of file BLI_mempool.cc.
Referenced by BLI_mempool_create().
| #define FREEWORD |
Important that this value 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 61 of file BLI_mempool.cc.
Referenced by BLI_mempool_free(), BLI_mempool_iterstep(), mempool_chunk_add(), and mempool_iter_threadsafe_step().
| #define MAKE_ID | ( | a, | |
| b, | |||
| c, | |||
| d ) |
Definition at line 50 of file BLI_mempool.cc.
Definition at line 51 of file BLI_mempool.cc.
| #define MEMPOOL_ELEM_SIZE_MIN (sizeof(void *) * 2) |
Definition at line 127 of file BLI_mempool.cc.
Referenced by BLI_mempool_create().
| #define NODE_STEP_NEXT | ( | node | ) |
Definition at line 132 of file BLI_mempool.cc.
Referenced by BLI_mempool_as_array(), BLI_mempool_free(), and mempool_chunk_add().
| #define NODE_STEP_PREV | ( | node | ) |
Definition at line 133 of file BLI_mempool.cc.
Referenced by BLI_mempool_free(), and mempool_chunk_add().
| #define POISON_REDZONE_SIZE 0 |
Definition at line 45 of file BLI_mempool.cc.
Referenced by BLI_mempool_alloc(), BLI_mempool_as_array(), BLI_mempool_calloc(), BLI_mempool_create(), BLI_mempool_free(), BLI_mempool_iterstep(), mempool_chunk_add(), and mempool_iter_threadsafe_step().
| #define USE_CHUNK_POW2 |
Definition at line 71 of file BLI_mempool.cc.
The 'used' word just needs to be set to something besides FREEWORD.
Definition at line 68 of file BLI_mempool.cc.
Referenced by BLI_mempool_alloc().
| void * BLI_mempool_alloc | ( | BLI_mempool * | pool | ) |
Definition at line 383 of file BLI_mempool.cc.
References BLI_asan_unpoison, BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_mempool::chunk_tail, BLI_mempool::esize, BLI_mempool::flag, BLI_mempool::free, BLI_freenode::freeword, mempool_chunk_add(), mempool_chunk_alloc(), BLI_freenode::next, BLI_mempool_chunk::next, POISON_REDZONE_SIZE, BLI_mempool::totused, UNLIKELY, USEDWORD, and VALGRIND_MEMPOOL_ALLOC.
Referenced by ATTR_NONNULL(), bchunk_list_append_only(), bchunk_list_new(), bchunk_new(), BKE_image_pool_acquire_ibuf(), BKE_main_idmap_insert_id(), BKE_main_idmap_lookup_name(), 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_loop_create(), BM_mesh_beautify_fill(), BM_mesh_rebuild(), bm_uidwalk_facestep_begin(), bm_uidwalk_pass_add(), BM_vert_create(), bmo_flag_layer_free(), BMW_state_add(), blender::ed::outliner::check_persistent(), CustomData_bmesh_alloc_block(), CustomData_bmesh_copy_block(), CustomData_bmesh_copy_block(), do_moviecache_put(), blender::bke::pbvh::edge_queue_insert(), ghash_copy(), ghash_insert_ex(), ghash_insert_ex_keyonly(), main_relations_create_idlink_cb(), blender::ed::outliner::outliner_space_blend_read_data(), blender::ed::outliner::outliner_storage_cleanup(), state_link_add(), TEST(), TEST(), and vs_add().
| void BLI_mempool_as_array | ( | BLI_mempool * | pool, |
| void * | data ) |
Definition at line 551 of file BLI_mempool.cc.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_mempool_iternew(), BLI_mempool_iterstep(), data, BLI_mempool::esize, BLI_mempool::flag, mempool_asan_lock(), mempool_asan_unlock(), NODE_STEP_NEXT, and POISON_REDZONE_SIZE.
Referenced by BLI_mempool_as_arrayN().
| void * BLI_mempool_as_arrayN | ( | BLI_mempool * | pool, |
| const char * | allocstr ) |
Definition at line 569 of file BLI_mempool.cc.
References BLI_mempool_as_array(), data, BLI_mempool::esize, MEM_malloc_arrayN(), and BLI_mempool::totused.
| void * BLI_mempool_calloc | ( | BLI_mempool * | pool | ) |
Definition at line 424 of file BLI_mempool.cc.
References BLI_mempool_alloc(), BLI_mempool::esize, and POISON_REDZONE_SIZE.
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(), blender::ed::curves::curve_draw_event_add(), curve_draw_event_add(), blender::ed::curves::curve_draw_stroke_from_operator_elem(), curve_draw_stroke_from_operator_elem(), do_versions_after_linking_280(), knife_append_list(), layer_collection_resync_create_recurse(), layer_collection_sync(), 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 835 of file BLI_mempool.cc.
References BLI_mempool_clear_ex().
Referenced by BLI_array_store_clear(), bm_uidwalk_clear(), and bmo_planar_faces_exec().
| void BLI_mempool_clear_ex | ( | BLI_mempool * | pool, |
| int | elem_num_reserve ) |
Empty the pool, as if it were just created.
| pool | The pool to clear. |
| elem_num_reserve | Optionally reserve how many items should be kept from clearing. |
Definition at line 787 of file BLI_mempool.cc.
References BLI_mempool::chunk_tail, BLI_mempool::chunks, BLI_mempool::free, BLI_mempool::maxchunks, mempool_chunk_add(), mempool_chunk_find(), mempool_chunk_free(), mempool_maxchunks(), BLI_mempool_chunk::next, BLI_mempool::pchunk, BLI_mempool::totused, VALGRIND_CREATE_MEMPOOL, and VALGRIND_DESTROY_MEMPOOL.
Referenced by BLI_ghash_clear_ex(), and BLI_mempool_clear().
| BLI_mempool * BLI_mempool_create | ( | uint | esize, |
| uint | elem_num, | ||
| uint | pchunk, | ||
| uint | flag ) |
Definition at line 316 of file BLI_mempool.cc.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_mutex_init(), CHUNK_OVERHEAD, BLI_mempool::chunk_tail, BLI_mempool::chunks, BLI_mempool::csize, BLI_mempool::esize, BLI_mempool::flag, flag, BLI_mempool::free, i, BLI_mempool::maxchunks, MEM_callocN(), MEM_SIZE_OVERHEAD, mempool_chunk_add(), mempool_chunk_alloc(), MEMPOOL_ELEM_SIZE_MIN, mempool_maxchunks(), BLI_mempool::pchunk, POISON_REDZONE_SIZE, power_of_2_max_u(), BLI_mempool::totused, and VALGRIND_CREATE_MEMPOOL.
Referenced by BKE_image_pool_new(), BKE_layer_collection_sync(), BKE_main_idmap_lookup_name(), BKE_main_relations_create(), BLI_array_store_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_uidwalk_init(), blender::bke::pbvh::bmesh_update_topology(), bmo_connect_vert_pair_exec(), bmo_flag_layer_alloc(), bmo_flag_layer_free(), bmo_planar_faces_exec(), BMW_init(), blender::ed::outliner::check_persistent(), blender::ed::curves::curve_draw_init(), curve_draw_init(), CustomData_bmesh_init_pool(), do_versions_after_linking_280(), ghash_new(), IMB_moviecache_create(), knifetool_init(), blender::ed::outliner::outliner_space_blend_read_data(), blender::ed::outliner::outliner_storage_cleanup(), TEST(), and TEST().
| void BLI_mempool_destroy | ( | BLI_mempool * | pool | ) |
Free the mempool itself (and all elements).
Definition at line 840 of file BLI_mempool.cc.
References BLI_mempool::chunks, MEM_freeN(), mempool_chunk_free_all(), and VALGRIND_DESTROY_MEMPOOL.
Referenced by BKE_image_pool_free(), BKE_layer_collection_sync(), BKE_main_idmap_destroy(), BKE_main_relations_free(), BLI_array_store_destroy(), BLI_ghash_free(), BLO_update_defaults_startup_blend(), 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_uidwalk_free(), blender::bke::pbvh::bmesh_update_topology(), bmo_connect_vert_pair_exec(), bmo_flag_layer_alloc(), bmo_flag_layer_free(), bmo_planar_faces_exec(), BMW_end(), blender::ed::curves::curve_draw_exit(), curve_draw_exit(), CustomData_bmesh_merge_layout(), IMB_moviecache_free(), knifetool_exit_ex(), blender::ed::outliner::outliner_free(), blender::ed::outliner::outliner_storage_cleanup(), TEST(), TEST(), and update_data_blocks().
| void * BLI_mempool_findelem | ( | BLI_mempool * | pool, |
| uint | index ) |
Definition at line 528 of file BLI_mempool.cc.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_mempool_iternew(), BLI_mempool_iterstep(), BLI_mempool::flag, mempool_asan_lock(), and mempool_asan_unlock().
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 ) |
Definition at line 433 of file BLI_mempool.cc.
References ARRAY_HAS_ITEM, BLI_asan_poison, BLI_asan_unpoison, BLI_assert, BLI_assert_msg, BLI_MEMPOOL_ALLOW_ITER, CHUNK_DATA, BLI_mempool::chunk_tail, BLI_mempool::chunks, BLI_mempool::csize, BLI_mempool::esize, BLI_mempool::flag, BLI_mempool::free, FREEWORD, BLI_freenode::freeword, mempool_chunk_free_all(), mempool_debug_memset, BLI_freenode::next, BLI_mempool_chunk::next, next, NODE_STEP_NEXT, NODE_STEP_PREV, BLI_mempool::pchunk, POISON_REDZONE_SIZE, BLI_mempool::totused, UNLIKELY, and VALGRIND_MEMPOOL_ALLOC.
| 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 577 of file BLI_mempool.cc.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_mempool::chunks, BLI_mempool_iter::curchunk, BLI_mempool_iter::curindex, BLI_mempool::flag, and BLI_mempool_iter::pool.
Referenced by array_store_free_data(), BLI_array_store_calc_size_compacted_get(), BLI_array_store_is_valid(), BLI_mempool_as_array(), BLI_mempool_findelem(), BLI_task_parallel_mempool(), bmiter__elem_of_mesh_begin(), curve_draw_exec(), blender::ed::curves::curve_draw_exec_precalc(), curve_draw_exec_precalc(), blender::ed::curves::curve_draw_stroke_3d(), curve_draw_stroke_3d(), blender::ed::curves::curve_draw_stroke_to_operator(), curve_draw_stroke_to_operator(), blender::ed::curves::curves_draw_exec(), knife_make_cuts(), knifetool_draw(), knifetool_undo(), mempool_threadsafe_iternew(), blender::ed::outliner::outliner_foreach_id(), blender::ed::outliner::outliner_id_remap(), blender::ed::outliner::outliner_space_blend_read_after_liblink(), and blender::ed::outliner::outliner_storage_cleanup().
| void * BLI_mempool_iterstep | ( | BLI_mempool_iter * | iter | ) |
Step over the iterator, returning the mempool item or NULL.
Definition at line 657 of file BLI_mempool.cc.
References BLI_asan_poison, BLI_asan_unpoison, CHUNK_DATA, BLI_mempool_iter::curchunk, BLI_mempool_iter::curindex, BLI_mempool::esize, FREEWORD, BLI_mempool_chunk::next, BLI_mempool::pchunk, POINTER_OFFSET, POISON_REDZONE_SIZE, BLI_mempool_iter::pool, ret, and UNLIKELY.
Referenced by array_store_free_data(), BLI_array_store_calc_size_compacted_get(), BLI_array_store_is_valid(), BLI_mempool_as_array(), BLI_mempool_findelem(), BLI_task_parallel_mempool(), bmiter__elem_of_mesh_step(), curve_draw_exec(), blender::ed::curves::curve_draw_exec_precalc(), curve_draw_exec_precalc(), blender::ed::curves::curve_draw_stroke_3d(), curve_draw_stroke_3d(), blender::ed::curves::curve_draw_stroke_to_operator(), curve_draw_stroke_to_operator(), blender::ed::curves::curves_draw_exec(), knife_make_cuts(), knifetool_draw(), knifetool_undo(), blender::ed::outliner::outliner_foreach_id(), blender::ed::outliner::outliner_id_remap(), blender::ed::outliner::outliner_space_blend_read_after_liblink(), and blender::ed::outliner::outliner_storage_cleanup().
| int BLI_mempool_len | ( | const BLI_mempool * | pool | ) |
Definition at line 521 of file BLI_mempool.cc.
References ret, and BLI_mempool::totused.
Referenced by 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::TreeDisplayDataAPI::build_tree(), blender::ed::outliner::TreeDisplayScenes::build_tree(), curve_draw_exec(), blender::ed::curves::curve_draw_exec_precalc(), curve_draw_exec_precalc(), blender::ed::curves::curve_draw_stroke_3d(), curve_draw_stroke_3d(), blender::ed::curves::curves_draw_exec(), knifetool_draw(), blender::ed::outliner::outliner_storage_cleanup(), and blender::ed::outliner::write_space_outliner().
| void BLI_mempool_set_memory_debug | ( | ) |
Definition at line 852 of file BLI_mempool.cc.
References mempool_debug_memset.
Referenced by arg_handle_debug_mode_set().
|
static |
Definition at line 147 of file BLI_mempool.cc.
References BLI_mutex_lock(), and UNUSED_VARS.
Referenced by BLI_mempool_as_array(), BLI_mempool_findelem(), and mempool_iter_threadsafe_step().
|
static |
Definition at line 138 of file BLI_mempool.cc.
References BLI_mutex_unlock(), and UNUSED_VARS.
Referenced by BLI_mempool_as_array(), BLI_mempool_findelem(), and mempool_iter_threadsafe_step().
|
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 190 of file BLI_mempool.cc.
References BLI_asan_poison, BLI_asan_unpoison, BLI_assert, BLI_MEMPOOL_ALLOW_ITER, CHUNK_DATA, BLI_mempool::chunk_tail, BLI_mempool::chunks, BLI_mempool::esize, BLI_mempool::flag, BLI_mempool::free, FREEWORD, BLI_freenode::freeword, BLI_freenode::next, BLI_mempool_chunk::next, next, NODE_STEP_NEXT, NODE_STEP_PREV, BLI_mempool::pchunk, POISON_REDZONE_SIZE, and UNLIKELY.
Referenced by BLI_mempool_alloc(), BLI_mempool_clear_ex(), and BLI_mempool_create().
|
static |
Definition at line 175 of file BLI_mempool.cc.
References BLI_mempool::csize, and MEM_mallocN().
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 156 of file BLI_mempool.cc.
References BLI_INLINE, and BLI_mempool_chunk::next.
Referenced by BLI_mempool_clear_ex().
|
static |
Definition at line 293 of file BLI_mempool.cc.
References BLI_asan_unpoison, BLI_mempool::csize, BLI_mempool::esize, MEM_freeN(), and UNUSED_VARS.
Referenced by BLI_mempool_clear_ex(), and mempool_chunk_free_all().
|
static |
Definition at line 306 of file BLI_mempool.cc.
References mempool_chunk_free(), and BLI_mempool_chunk::next.
Referenced by BLI_mempool_destroy(), and BLI_mempool_free().
| ParallelMempoolTaskData * mempool_iter_threadsafe_create | ( | BLI_mempool * | pool, |
| size_t | iter_num ) |
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 lock-less 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 592 of file BLI_mempool.cc.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_mempool_iter::curchunk, BLI_mempool_threadsafe_iter::curchunk_threaded_shared, BLI_mempool::flag, i, BLI_mempool_threadsafe_iter::iter, MEM_calloc_arrayN(), MEM_callocN(), mempool_threadsafe_iternew(), and ParallelMempoolTaskData::ts_iter.
Referenced by BLI_task_parallel_mempool().
| void mempool_iter_threadsafe_destroy | ( | ParallelMempoolTaskData * | iter_arr | ) |
Definition at line 613 of file BLI_mempool.cc.
References BLI_assert, BLI_mempool_threadsafe_iter::curchunk_threaded_shared, MEM_freeN(), and ParallelMempoolTaskData::ts_iter.
Referenced by BLI_task_parallel_mempool().
| void * mempool_iter_threadsafe_step | ( | BLI_mempool_threadsafe_iter * | ts_iter | ) |
A version of BLI_mempool_iterstep that uses BLI_mempool_threadsafe_iter.curchunk_threaded_shared for threaded iteration support. (threaded section noted in comments).
Definition at line 703 of file BLI_mempool.cc.
References atomic_cas_ptr(), BLI_asan_poison, BLI_asan_unpoison, CHUNK_DATA, BLI_mempool_iter::curchunk, BLI_mempool_threadsafe_iter::curchunk_threaded_shared, BLI_mempool_iter::curindex, BLI_mempool::esize, FREEWORD, BLI_mempool_threadsafe_iter::iter, mempool_asan_lock(), mempool_asan_unlock(), BLI_mempool_chunk::next, BLI_mempool::pchunk, POINTER_OFFSET, POISON_REDZONE_SIZE, BLI_mempool_iter::pool, ret, and UNLIKELY.
Referenced by parallel_mempool_func().
| BLI_INLINE uint mempool_maxchunks | ( | const uint | elem_num, |
| const uint | pchunk ) |
Definition at line 170 of file BLI_mempool.cc.
References BLI_INLINE.
Referenced by BLI_mempool_clear_ex(), and BLI_mempool_create().
|
static |
Definition at line 586 of file BLI_mempool.cc.
References BLI_mempool_iternew(), BLI_mempool_threadsafe_iter::curchunk_threaded_shared, and BLI_mempool_threadsafe_iter::iter.
Referenced by mempool_iter_threadsafe_create().
|
static |
Definition at line 74 of file BLI_mempool.cc.
Referenced by BLI_mempool_free(), and BLI_mempool_set_memory_debug().