|
Blender V4.3
|
#include <stdlib.h>#include <string.h>#include "atomic_ops.h"#include "BLI_utildefines.h"#include "BLI_asan.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) ((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 135 of file BLI_mempool.c.
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 141 of file BLI_mempool.c.
Referenced by BLI_mempool_create().
| #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 67 of file BLI_mempool.c.
Referenced by BLI_mempool_free(), BLI_mempool_iterstep(), mempool_chunk_add(), and mempool_iter_threadsafe_step().
Definition at line 55 of file BLI_mempool.c.
Definition at line 56 of file BLI_mempool.c.
| #define MEMPOOL_ELEM_SIZE_MIN (sizeof(void *) * 2) |
Definition at line 133 of file BLI_mempool.c.
Referenced by BLI_mempool_create().
| #define NODE_STEP_NEXT | ( | node | ) | ((void *)((char *)(node) + esize)) |
Definition at line 137 of file BLI_mempool.c.
Referenced by BLI_mempool_as_array(), BLI_mempool_free(), and mempool_chunk_add().
| #define NODE_STEP_PREV | ( | node | ) | ((void *)((char *)(node)-esize)) |
Definition at line 138 of file BLI_mempool.c.
Referenced by BLI_mempool_free(), and mempool_chunk_add().
| #define POISON_REDZONE_SIZE 0 |
Definition at line 43 of file BLI_mempool.c.
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 77 of file BLI_mempool.c.
The 'used' word just needs to be set to something besides FREEWORD.
Definition at line 74 of file BLI_mempool.c.
Referenced by BLI_mempool_alloc().
| 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 402 of file BLI_mempool.c.
References BLI_asan_unpoison, BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_freenode::freeword, mempool_chunk_add(), mempool_chunk_alloc(), BLI_freenode::next, NULL, POISON_REDZONE_SIZE, UNLIKELY, USEDWORD, and VALGRIND_MEMPOOL_ALLOC.
Referenced by 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_log_face_alloc(), bm_log_vert_alloc(), 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(), DRW_instance_data_next(), 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(), seq_cache_allocate_key(), seq_cache_put_ex(), state_link_add(), tbb_task_pool_run(), TEST(), TEST(), and vs_add().
| void BLI_mempool_as_array | ( | BLI_mempool * | pool, |
| void * | data ) |
Definition at line 575 of file BLI_mempool.c.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_mempool_iternew(), BLI_mempool_iterstep(), data, 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 593 of file BLI_mempool.c.
References BLI_mempool_as_array(), data, and MEM_malloc_arrayN.
| void * BLI_mempool_calloc | ( | BLI_mempool * | pool | ) |
Definition at line 443 of file BLI_mempool.c.
References BLI_mempool_alloc(), 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 856 of file BLI_mempool.c.
References BLI_mempool_clear_ex().
Referenced by BLI_array_store_clear(), bm_uidwalk_clear(), bmo_planar_faces_exec(), and tbb_task_pool_work_and_wait().
| 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 808 of file BLI_mempool.c.
References mempool_chunk_add(), mempool_chunk_find(), mempool_chunk_free(), mempool_maxchunks(), BLI_mempool_chunk::next, NULL, 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 | elem_num, | ||
| uint | pchunk, | ||
| uint | flag ) |
Definition at line 333 of file BLI_mempool.c.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_mutex_init(), CHUNK_OVERHEAD, flag, int, MAX2, MEM_mallocN, MEM_SIZE_OVERHEAD, mempool_chunk_add(), mempool_chunk_alloc(), MEMPOOL_ELEM_SIZE_MIN, mempool_maxchunks(), NULL, POISON_REDZONE_SIZE, power_of_2_max_u(), 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_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_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(), drw_instance_data_create(), ghash_new(), IMB_moviecache_create(), knifetool_init(), blender::ed::outliner::outliner_space_blend_read_data(), blender::ed::outliner::outliner_storage_cleanup(), seq_cache_create(), tbb_task_pool_create(), TEST(), and TEST().
| void BLI_mempool_destroy | ( | BLI_mempool * | pool | ) |
Free the mempool itself (and all elements).
Definition at line 861 of file BLI_mempool.c.
References 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_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_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(), DRW_instance_data_free(), IMB_moviecache_free(), knifetool_exit_ex(), blender::ed::outliner::outliner_free(), blender::ed::outliner::outliner_storage_cleanup(), seq_cache_destruct(), tbb_task_pool_free(), TEST(), TEST(), and update_data_blocks().
| void * BLI_mempool_findelem | ( | BLI_mempool * | pool, |
| uint | index ) |
Definition at line 552 of file BLI_mempool.c.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_mempool_iternew(), BLI_mempool_iterstep(), mempool_asan_lock(), mempool_asan_unlock(), and NULL.
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 457 of file BLI_mempool.c.
References ARRAY_HAS_ITEM, BLI_asan_poison, BLI_asan_unpoison, BLI_assert, BLI_assert_msg, BLI_MEMPOOL_ALLOW_ITER, CHUNK_DATA, 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, NULL, POISON_REDZONE_SIZE, 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 600 of file BLI_mempool.c.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_mempool_iter::curchunk, BLI_mempool_iter::curindex, 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(), blender::ed::outliner::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 679 of file BLI_mempool.c.
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, NULL, 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(), blender::ed::outliner::outliner_storage_cleanup(), and tbb_task_pool_work_and_wait().
| int BLI_mempool_len | ( | const BLI_mempool * | pool | ) |
Definition at line 545 of file BLI_mempool.c.
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(), DRW_instance_data_list_resize(), knifetool_draw(), blender::ed::outliner::outliner_storage_cleanup(), and blender::ed::outliner::write_space_outliner().
| void BLI_mempool_set_memory_debug | ( | void | ) |
Definition at line 873 of file BLI_mempool.c.
References mempool_debug_memset.
Referenced by arg_handle_debug_mode_set().
|
static |
Definition at line 152 of file BLI_mempool.c.
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 143 of file BLI_mempool.c.
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 207 of file BLI_mempool.c.
References BLI_asan_poison, BLI_asan_unpoison, BLI_assert, BLI_MEMPOOL_ALLOW_ITER, CHUNK_DATA, FREEWORD, BLI_freenode::freeword, BLI_freenode::next, BLI_mempool_chunk::next, next, NODE_STEP_NEXT, NODE_STEP_PREV, NULL, POISON_REDZONE_SIZE, and UNLIKELY.
Referenced by BLI_mempool_alloc(), BLI_mempool_clear_ex(), and BLI_mempool_create().
|
static |
Definition at line 193 of file BLI_mempool.c.
References 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 174 of file BLI_mempool.c.
References BLI_mempool_chunk::next.
Referenced by BLI_mempool_clear_ex().
|
static |
Definition at line 310 of file BLI_mempool.c.
References BLI_asan_unpoison, MEM_freeN(), and UNUSED_VARS.
Referenced by BLI_mempool_clear_ex(), and mempool_chunk_free_all().
|
static |
Definition at line 323 of file BLI_mempool.c.
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 615 of file BLI_mempool.c.
References BLI_assert, BLI_MEMPOOL_ALLOW_ITER, BLI_mempool_iter::curchunk, BLI_mempool_threadsafe_iter::curchunk_threaded_shared, BLI_mempool_threadsafe_iter::iter, MEM_mallocN, mempool_threadsafe_iternew(), NULL, and ParallelMempoolTaskData::ts_iter.
Referenced by BLI_task_parallel_mempool().
| void mempool_iter_threadsafe_destroy | ( | ParallelMempoolTaskData * | iter_arr | ) |
Definition at line 635 of file BLI_mempool.c.
References BLI_assert, BLI_mempool_threadsafe_iter::curchunk_threaded_shared, MEM_freeN(), NULL, 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 725 of file BLI_mempool.c.
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, NULL, 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 188 of file BLI_mempool.c.
Referenced by BLI_mempool_clear_ex(), and BLI_mempool_create().
|
static |
Definition at line 609 of file BLI_mempool.c.
References BLI_mempool_iternew(), BLI_mempool_threadsafe_iter::curchunk_threaded_shared, BLI_mempool_threadsafe_iter::iter, and NULL.
Referenced by mempool_iter_threadsafe_create().
Definition at line 162 of file BLI_mempool.c.
Referenced by BLI_mempool_create().
|
static |
Definition at line 80 of file BLI_mempool.c.
Referenced by BLI_mempool_free(), and BLI_mempool_set_memory_debug().