mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-05-18 21:19:31 +03:00
Constify the parameters of some functions that can become it
Using the type qualifier const makes the code easier to understand avoiding misunderstandings during programming and allows the user who uses the mimalloc ABI to immediately identify those functions that change the variables passed to the functions. Signed-off-by: Ernesto Castellotti <erny.castell@gmail.com>
This commit is contained in:
parent
1125271c27
commit
860da5d7db
5 changed files with 24 additions and 24 deletions
|
@ -81,7 +81,7 @@ void _mi_stats_done(mi_stats_t* stats);
|
||||||
void* _mi_page_malloc(mi_heap_t* heap, mi_page_t* page, size_t size) mi_attr_noexcept; // called from `_mi_malloc_generic`
|
void* _mi_page_malloc(mi_heap_t* heap, mi_page_t* page, size_t size) mi_attr_noexcept; // called from `_mi_malloc_generic`
|
||||||
void* _mi_heap_malloc_zero(mi_heap_t* heap, size_t size, bool zero);
|
void* _mi_heap_malloc_zero(mi_heap_t* heap, size_t size, bool zero);
|
||||||
void* _mi_realloc_zero(void* p, size_t size, bool zero);
|
void* _mi_realloc_zero(void* p, size_t size, bool zero);
|
||||||
mi_block_t* _mi_page_ptr_unalign(const mi_segment_t* segment, const mi_page_t* page, void* p);
|
mi_block_t* _mi_page_ptr_unalign(const mi_segment_t* segment, const mi_page_t* page, const void* p);
|
||||||
void _mi_free_delayed_block(mi_block_t* block);
|
void _mi_free_delayed_block(mi_block_t* block);
|
||||||
|
|
||||||
#if MI_DEBUG>1
|
#if MI_DEBUG>1
|
||||||
|
|
|
@ -104,7 +104,7 @@ mi_decl_export mi_decl_allocator void* mi_reallocf(void* p, size_t newsize)
|
||||||
mi_decl_export mi_decl_allocator void* mi_rezalloc(void* p, size_t newsize) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2);
|
mi_decl_export mi_decl_allocator void* mi_rezalloc(void* p, size_t newsize) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2);
|
||||||
mi_decl_export mi_decl_allocator void* mi_recalloc(void* p, size_t count, size_t size) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(2,3);
|
mi_decl_export mi_decl_allocator void* mi_recalloc(void* p, size_t count, size_t size) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(2,3);
|
||||||
|
|
||||||
mi_decl_export size_t mi_usable_size(void* p) mi_attr_noexcept;
|
mi_decl_export size_t mi_usable_size(const void* p) mi_attr_noexcept;
|
||||||
mi_decl_export size_t mi_good_size(size_t size) mi_attr_noexcept;
|
mi_decl_export size_t mi_good_size(size_t size) mi_attr_noexcept;
|
||||||
|
|
||||||
mi_decl_export void mi_collect(bool force) mi_attr_noexcept;
|
mi_decl_export void mi_collect(bool force) mi_attr_noexcept;
|
||||||
|
@ -117,7 +117,7 @@ mi_decl_export void mi_thread_done(void) mi_attr_noexcept;
|
||||||
mi_decl_export void mi_thread_stats_print(FILE* out) mi_attr_noexcept;
|
mi_decl_export void mi_thread_stats_print(FILE* out) mi_attr_noexcept;
|
||||||
|
|
||||||
typedef void (mi_deferred_free_fun)(bool force, unsigned long long heartbeat);
|
typedef void (mi_deferred_free_fun)(bool force, unsigned long long heartbeat);
|
||||||
mi_decl_export void mi_register_deferred_free(mi_deferred_free_fun* deferred_free) mi_attr_noexcept;
|
mi_decl_export void mi_register_deferred_free(const mi_deferred_free_fun* deferred_free) mi_attr_noexcept;
|
||||||
|
|
||||||
|
|
||||||
// ------------------------------------------------------
|
// ------------------------------------------------------
|
||||||
|
@ -186,9 +186,9 @@ typedef struct mi_heap_area_s {
|
||||||
size_t block_size; // size in bytes of each block
|
size_t block_size; // size in bytes of each block
|
||||||
} mi_heap_area_t;
|
} mi_heap_area_t;
|
||||||
|
|
||||||
typedef bool (mi_block_visit_fun)(const mi_heap_t* heap, const mi_heap_area_t* area, void* block, size_t block_size, void* arg);
|
typedef bool (mi_block_visit_fun)(const mi_heap_t* heap, const mi_heap_area_t* area, void* block, size_t block_size, const void* arg);
|
||||||
|
|
||||||
mi_decl_export bool mi_heap_visit_blocks(const mi_heap_t* heap, bool visit_all_blocks, mi_block_visit_fun* visitor, void* arg);
|
mi_decl_export bool mi_heap_visit_blocks(const mi_heap_t* heap, bool visit_all_blocks, mi_block_visit_fun* visitor, const void* arg);
|
||||||
|
|
||||||
// ------------------------------------------------------
|
// ------------------------------------------------------
|
||||||
// Convenience
|
// Convenience
|
||||||
|
|
|
@ -177,7 +177,7 @@ static inline void _mi_free_block(mi_page_t* page, bool local, mi_block_t* block
|
||||||
|
|
||||||
|
|
||||||
// Adjust a block that was allocated aligned, to the actual start of the block in the page.
|
// Adjust a block that was allocated aligned, to the actual start of the block in the page.
|
||||||
mi_block_t* _mi_page_ptr_unalign(const mi_segment_t* segment, const mi_page_t* page, void* p) {
|
mi_block_t* _mi_page_ptr_unalign(const mi_segment_t* segment, const mi_page_t* page, const void* p) {
|
||||||
mi_assert_internal(page!=NULL && p!=NULL);
|
mi_assert_internal(page!=NULL && p!=NULL);
|
||||||
size_t diff = (uint8_t*)p - _mi_page_start(segment, page, NULL);
|
size_t diff = (uint8_t*)p - _mi_page_start(segment, page, NULL);
|
||||||
size_t adjust = (diff % page->block_size);
|
size_t adjust = (diff % page->block_size);
|
||||||
|
@ -256,7 +256,7 @@ void _mi_free_delayed_block(mi_block_t* block) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bytes available in a block
|
// Bytes available in a block
|
||||||
size_t mi_usable_size(void* p) mi_attr_noexcept {
|
size_t mi_usable_size(const void* p) mi_attr_noexcept {
|
||||||
if (p==NULL) return 0;
|
if (p==NULL) return 0;
|
||||||
const mi_segment_t* segment = _mi_ptr_segment(p);
|
const mi_segment_t* segment = _mi_ptr_segment(p);
|
||||||
const mi_page_t* page = _mi_segment_page_of(segment,p);
|
const mi_page_t* page = _mi_segment_page_of(segment,p);
|
||||||
|
|
28
src/heap.c
28
src/heap.c
|
@ -17,10 +17,10 @@ terms of the MIT license. A copy of the license can be found in the file
|
||||||
----------------------------------------------------------- */
|
----------------------------------------------------------- */
|
||||||
|
|
||||||
// return `true` if ok, `false` to break
|
// return `true` if ok, `false` to break
|
||||||
typedef bool (heap_page_visitor_fun)(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, void* arg1, void* arg2);
|
typedef bool (heap_page_visitor_fun)(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, const void* arg1, const void* arg2);
|
||||||
|
|
||||||
// Visit all pages in a heap; returns `false` if break was called.
|
// Visit all pages in a heap; returns `false` if break was called.
|
||||||
static bool mi_heap_visit_pages(mi_heap_t* heap, heap_page_visitor_fun* fn, void* arg1, void* arg2)
|
static bool mi_heap_visit_pages(mi_heap_t* heap, heap_page_visitor_fun* fn, const void* arg1, const void* arg2)
|
||||||
{
|
{
|
||||||
if (heap==NULL || heap->page_count==0) return 0;
|
if (heap==NULL || heap->page_count==0) return 0;
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ static bool mi_heap_visit_pages(mi_heap_t* heap, heap_page_visitor_fun* fn, void
|
||||||
|
|
||||||
|
|
||||||
#if MI_DEBUG>1
|
#if MI_DEBUG>1
|
||||||
static bool _mi_heap_page_is_valid(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, void* arg1, void* arg2) {
|
static bool _mi_heap_page_is_valid(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, const void* arg1, const void* arg2) {
|
||||||
UNUSED(arg1);
|
UNUSED(arg1);
|
||||||
UNUSED(arg2);
|
UNUSED(arg2);
|
||||||
UNUSED(pq);
|
UNUSED(pq);
|
||||||
|
@ -81,7 +81,7 @@ typedef enum mi_collect_e {
|
||||||
} mi_collect_t;
|
} mi_collect_t;
|
||||||
|
|
||||||
|
|
||||||
static bool mi_heap_page_collect(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, void* arg_collect, void* arg2 ) {
|
static bool mi_heap_page_collect(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, const void* arg_collect, const void* arg2 ) {
|
||||||
UNUSED(arg2);
|
UNUSED(arg2);
|
||||||
UNUSED(heap);
|
UNUSED(heap);
|
||||||
mi_collect_t collect = (mi_collect_t)arg_collect;
|
mi_collect_t collect = (mi_collect_t)arg_collect;
|
||||||
|
@ -221,7 +221,7 @@ static void mi_heap_free(mi_heap_t* heap) {
|
||||||
Heap destroy
|
Heap destroy
|
||||||
----------------------------------------------------------- */
|
----------------------------------------------------------- */
|
||||||
|
|
||||||
static bool _mi_heap_page_destroy(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, void* arg1, void* arg2) {
|
static bool _mi_heap_page_destroy(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, const void* arg1, const void* arg2) {
|
||||||
UNUSED(arg1);
|
UNUSED(arg1);
|
||||||
UNUSED(arg2);
|
UNUSED(arg2);
|
||||||
UNUSED(heap);
|
UNUSED(heap);
|
||||||
|
@ -362,7 +362,7 @@ bool mi_heap_contains_block(mi_heap_t* heap, const void* p) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static bool mi_heap_page_check_owned(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, void* p, void* vfound) {
|
static bool mi_heap_page_check_owned(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, const void* p, const void* vfound) {
|
||||||
UNUSED(heap);
|
UNUSED(heap);
|
||||||
UNUSED(pq);
|
UNUSED(pq);
|
||||||
bool* found = (bool*)vfound;
|
bool* found = (bool*)vfound;
|
||||||
|
@ -398,7 +398,7 @@ typedef struct mi_heap_area_ex_s {
|
||||||
mi_page_t* page;
|
mi_page_t* page;
|
||||||
} mi_heap_area_ex_t;
|
} mi_heap_area_ex_t;
|
||||||
|
|
||||||
static bool mi_heap_area_visit_blocks(const mi_heap_area_ex_t* xarea, mi_block_visit_fun* visitor, void* arg) {
|
static bool mi_heap_area_visit_blocks(const mi_heap_area_ex_t* xarea, mi_block_visit_fun* visitor, const void* arg) {
|
||||||
mi_assert(xarea != NULL);
|
mi_assert(xarea != NULL);
|
||||||
if (xarea==NULL) return true;
|
if (xarea==NULL) return true;
|
||||||
const mi_heap_area_t* area = &xarea->area;
|
const mi_heap_area_t* area = &xarea->area;
|
||||||
|
@ -457,10 +457,10 @@ static bool mi_heap_area_visit_blocks(const mi_heap_area_ex_t* xarea, mi_block_v
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef bool (mi_heap_area_visit_fun)(const mi_heap_t* heap, const mi_heap_area_ex_t* area, void* arg);
|
typedef bool (mi_heap_area_visit_fun)(const mi_heap_t* heap, mi_heap_area_ex_t* area, const void* arg);
|
||||||
|
|
||||||
|
|
||||||
static bool mi_heap_visit_areas_page(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, void* vfun, void* arg) {
|
static bool mi_heap_visit_areas_page(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, const void* vfun, const void* arg) {
|
||||||
UNUSED(heap);
|
UNUSED(heap);
|
||||||
UNUSED(pq);
|
UNUSED(pq);
|
||||||
mi_heap_area_visit_fun* fun = (mi_heap_area_visit_fun*)vfun;
|
mi_heap_area_visit_fun* fun = (mi_heap_area_visit_fun*)vfun;
|
||||||
|
@ -475,7 +475,7 @@ static bool mi_heap_visit_areas_page(mi_heap_t* heap, mi_page_queue_t* pq, mi_pa
|
||||||
}
|
}
|
||||||
|
|
||||||
// Visit all heap pages as areas
|
// Visit all heap pages as areas
|
||||||
static bool mi_heap_visit_areas(const mi_heap_t* heap, mi_heap_area_visit_fun* visitor, void* arg) {
|
static bool mi_heap_visit_areas(const mi_heap_t* heap, mi_heap_area_visit_fun* visitor, const void* arg) {
|
||||||
if (visitor == NULL) return false;
|
if (visitor == NULL) return false;
|
||||||
return mi_heap_visit_pages((mi_heap_t*)heap, &mi_heap_visit_areas_page, visitor, arg);
|
return mi_heap_visit_pages((mi_heap_t*)heap, &mi_heap_visit_areas_page, visitor, arg);
|
||||||
}
|
}
|
||||||
|
@ -484,10 +484,10 @@ static bool mi_heap_visit_areas(const mi_heap_t* heap, mi_heap_area_visit_fun* v
|
||||||
typedef struct mi_visit_blocks_args_s {
|
typedef struct mi_visit_blocks_args_s {
|
||||||
bool visit_blocks;
|
bool visit_blocks;
|
||||||
mi_block_visit_fun* visitor;
|
mi_block_visit_fun* visitor;
|
||||||
void* arg;
|
const void* arg;
|
||||||
} mi_visit_blocks_args_t;
|
} mi_visit_blocks_args_t;
|
||||||
|
|
||||||
static bool mi_heap_area_visitor(const mi_heap_t* heap, const mi_heap_area_ex_t* xarea, void* arg) {
|
static bool mi_heap_area_visitor(const mi_heap_t* heap, mi_heap_area_ex_t* xarea, const void* arg) {
|
||||||
mi_visit_blocks_args_t* args = (mi_visit_blocks_args_t*)arg;
|
mi_visit_blocks_args_t* args = (mi_visit_blocks_args_t*)arg;
|
||||||
if (!args->visitor(heap, &xarea->area, NULL, xarea->area.block_size, arg)) return false;
|
if (!args->visitor(heap, &xarea->area, NULL, xarea->area.block_size, arg)) return false;
|
||||||
if (args->visit_blocks) {
|
if (args->visit_blocks) {
|
||||||
|
@ -499,8 +499,8 @@ static bool mi_heap_area_visitor(const mi_heap_t* heap, const mi_heap_area_ex_t*
|
||||||
}
|
}
|
||||||
|
|
||||||
// Visit all blocks in a heap
|
// Visit all blocks in a heap
|
||||||
bool mi_heap_visit_blocks(const mi_heap_t* heap, bool visit_blocks, mi_block_visit_fun* visitor, void* arg) {
|
bool mi_heap_visit_blocks(const mi_heap_t* heap, bool visit_blocks, mi_block_visit_fun* visitor, const void* arg) {
|
||||||
mi_visit_blocks_args_t args = { visit_blocks, visitor, arg };
|
const mi_visit_blocks_args_t args = { visit_blocks, visitor, arg };
|
||||||
return mi_heap_visit_areas(heap, &mi_heap_area_visitor, &args);
|
return mi_heap_visit_areas(heap, &mi_heap_area_visitor, &args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -642,7 +642,7 @@ static inline mi_page_t* mi_find_free_page(mi_heap_t* heap, size_t size) {
|
||||||
a certain number of allocations.
|
a certain number of allocations.
|
||||||
----------------------------------------------------------- */
|
----------------------------------------------------------- */
|
||||||
|
|
||||||
static mi_deferred_free_fun* deferred_free = NULL;
|
static const mi_deferred_free_fun* deferred_free = NULL;
|
||||||
|
|
||||||
void _mi_deferred_free(mi_heap_t* heap, bool force) {
|
void _mi_deferred_free(mi_heap_t* heap, bool force) {
|
||||||
heap->tld->heartbeat++;
|
heap->tld->heartbeat++;
|
||||||
|
@ -651,7 +651,7 @@ void _mi_deferred_free(mi_heap_t* heap, bool force) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void mi_register_deferred_free(mi_deferred_free_fun* fn) mi_attr_noexcept {
|
void mi_register_deferred_free(const mi_deferred_free_fun* fn) mi_attr_noexcept {
|
||||||
deferred_free = fn;
|
deferred_free = fn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue