wip: improving tracking of allocation locations

This commit is contained in:
daan 2020-02-11 20:27:09 -08:00
parent 21a95c7449
commit 2fbe0e8842
9 changed files with 393 additions and 447 deletions

View File

@ -128,9 +128,8 @@ mi_block_t* _mi_page_ptr_unalign(const mi_segment_t* segment, const mi_page_t* p
bool _mi_free_delayed_block(mi_block_t* block);
void _mi_block_zero_init(const mi_page_t* page, void* p, size_t size);
mi_decl_allocator void* _mi_heapx_malloc(mi_heap_t* heap, size_t size MI_SOURCE_PARAM) mi_attr_noexcept;
mi_decl_allocator void* _mi_heapx_malloc_zero(mi_heap_t* heap, size_t size, bool zero MI_SOURCE_PARAM);
mi_decl_allocator void* _mi_heapx_realloc_zero(mi_heap_t* heap, void* p, size_t newsize, bool zero MI_SOURCE_PARAM);
mi_decl_allocator void* _mi_heap_source_malloc_zero(mi_heap_t* heap, size_t size, bool zero MI_SOURCE_PARAM);
mi_decl_allocator void* _mi_heap_source_realloc_zero(mi_heap_t* heap, void* p, size_t newsize, bool zero MI_SOURCE_PARAM);
#if MI_DEBUG>1
bool _mi_page_is_valid(mi_page_t* page);
@ -740,4 +739,56 @@ static inline uintptr_t _mi_thread_id(void) mi_attr_noexcept {
#endif
#define MI_ALLOC_API1(tp,name,tp0,arg0,tp1,arg1) \
static inline mi_decl_allocator tp mi_heap_source_##name(tp0 arg0, tp1 arg1 MI_SOURCE_PARAM) mi_attr_noexcept; \
mi_decl_allocator tp mi_source_##name(tp1 arg1 MI_SOURCE_PARAM) mi_attr_noexcept { return mi_heap_source_##name(mi_get_default_heap(), arg1 MI_SOURCE_ARG); } \
mi_decl_allocator tp mi_heap_##name(mi_heap_t* heap, tp1 arg1) mi_attr_noexcept { return mi_heap_source_##name(heap, arg1 MI_SOURCE_RET()); } \
mi_decl_allocator tp mi_##name(tp1 arg1) mi_attr_noexcept { return mi_heap_source_##name(mi_get_default_heap(), arg1 MI_SOURCE_RET()); } \
static inline mi_decl_allocator tp mi_heap_source_##name(tp0 arg0, tp1 arg1 MI_SOURCE_PARAM) mi_attr_noexcept
#define MI_ALLOC_API2(tp,name,tp0,arg0,tp1,arg1,tp2,arg2) \
static inline mi_decl_allocator tp mi_heap_source_##name(tp0 arg0, tp1 arg1, tp2 arg2 MI_SOURCE_PARAM) mi_attr_noexcept; \
mi_decl_allocator tp mi_source_##name(tp1 arg1, tp2 arg2 MI_SOURCE_PARAM) mi_attr_noexcept { return mi_heap_source_##name(mi_get_default_heap(), arg1, arg2 MI_SOURCE_ARG); } \
mi_decl_allocator tp mi_heap_##name(mi_heap_t* heap, tp1 arg1, tp2 arg2) mi_attr_noexcept { return mi_heap_source_##name(heap, arg1, arg2 MI_SOURCE_RET()); } \
mi_decl_allocator tp mi_##name(tp1 arg1, tp2 arg2) mi_attr_noexcept { return mi_heap_source_##name(mi_get_default_heap(), arg1, arg2 MI_SOURCE_RET()); } \
static inline mi_decl_allocator tp mi_heap_source_##name(tp0 arg0, tp1 arg1, tp2 arg2 MI_SOURCE_PARAM) mi_attr_noexcept
#define MI_ALLOC_API3(tp,name,tp0,arg0,tp1,arg1,tp2,arg2,tp3,arg3) \
static inline mi_decl_allocator tp mi_heap_source_##name(tp0 arg0, tp1 arg1, tp2 arg2, tp3 arg3 MI_SOURCE_PARAM) mi_attr_noexcept; \
mi_decl_allocator tp mi_source_##name(tp1 arg1, tp2 arg2, tp3 arg3 MI_SOURCE_PARAM) mi_attr_noexcept { return mi_heap_source_##name(mi_get_default_heap(), arg1, arg2, arg3 MI_SOURCE_ARG); } \
mi_decl_allocator tp mi_heap_##name(mi_heap_t* heap, tp1 arg1, tp2 arg2, tp3 arg3) mi_attr_noexcept { return mi_heap_source_##name(heap, arg1, arg2, arg3 MI_SOURCE_RET()); } \
mi_decl_allocator tp mi_##name(tp1 arg1, tp2 arg2, tp3 arg3) mi_attr_noexcept { return mi_heap_source_##name(mi_get_default_heap(), arg1, arg2, arg3 MI_SOURCE_RET()); } \
static inline mi_decl_allocator tp mi_heap_source_##name(tp0 arg0, tp1 arg1, tp2 arg2, tp3 arg3 MI_SOURCE_PARAM) mi_attr_noexcept
#define MI_ALLOC_API4(tp,name,tp0,arg0,tp1,arg1,tp2,arg2,tp3,arg3,tp4,arg4) \
static inline mi_decl_allocator tp mi_heap_source_##name(tp0 arg0, tp1 arg1, tp2 arg2, tp3 arg3, tp4 arg4 MI_SOURCE_PARAM) mi_attr_noexcept; \
mi_decl_allocator tp mi_source_##name(tp1 arg1, tp2 arg2, tp3 arg3, tp4 arg4 MI_SOURCE_PARAM) mi_attr_noexcept { return mi_heap_source_##name(mi_get_default_heap(), arg1, arg2, arg3, arg4 MI_SOURCE_ARG); } \
mi_decl_allocator tp mi_heap_##name(mi_heap_t* heap, tp1 arg1, tp2 arg2, tp3 arg3, tp4 arg4) mi_attr_noexcept { return mi_heap_source_##name(heap, arg1, arg2, arg3, arg4 MI_SOURCE_RET()); } \
mi_decl_allocator tp mi_##name(tp1 arg1, tp2 arg2, tp3 arg3, tp4 arg4) mi_attr_noexcept { return mi_heap_source_##name(mi_get_default_heap(), arg1, arg2, arg3, arg4 MI_SOURCE_RET()); } \
static inline mi_decl_allocator tp mi_heap_source_##name(tp0 arg0, tp1 arg1, tp2 arg2, tp3 arg3, tp4 arg4 MI_SOURCE_PARAM) mi_attr_noexcept
#define MI_ALLOC_API5(tp,name,tp0,arg0,tp1,arg1,tp2,arg2,tp3,arg3,tp4,arg4,tp5,arg5) \
static inline mi_decl_allocator tp mi_heap_source_##name(tp0 arg0, tp1 arg1, tp2 arg2, tp3 arg3, tp4 arg4, tp5 arg5 MI_SOURCE_PARAM) mi_attr_noexcept; \
mi_decl_allocator tp mi_source_##name(tp1 arg1, tp2 arg2, tp3 arg3, tp4 arg4, tp5 arg5 MI_SOURCE_PARAM) mi_attr_noexcept { return mi_heap_source_##name(mi_get_default_heap(), arg1, arg2, arg3, arg4, arg5 MI_SOURCE_ARG); } \
mi_decl_allocator tp mi_heap_##name(mi_heap_t* heap, tp1 arg1, tp2 arg2, tp3 arg3, tp4 arg4, tp5 arg5) mi_attr_noexcept { return mi_heap_source_##name(heap, arg1, arg2, arg3, arg4, arg5 MI_SOURCE_RET()); } \
mi_decl_allocator tp mi_##name(tp1 arg1, tp2 arg2, tp3 arg3, tp4 arg4, tp5 arg5) mi_attr_noexcept { return mi_heap_source_##name(mi_get_default_heap(), arg1, arg2, arg3, arg4, arg5 MI_SOURCE_RET()); } \
static inline mi_decl_allocator tp mi_heap_source_##name(tp0 arg0, tp1 arg1, tp2 arg2, tp3 arg3, tp4 arg4, tp5 arg5 MI_SOURCE_PARAM) mi_attr_noexcept
#define MI_SOURCE_API1(tp,name,tp1,arg1) \
tp mi_source_##name(tp1 arg1 MI_SOURCE_PARAM) mi_attr_noexcept; \
tp mi_##name(tp1 arg1) mi_attr_noexcept { return mi_source_##name(arg1 MI_SOURCE_RET()); } \
tp mi_source_##name(tp1 arg1 MI_SOURCE_PARAM) mi_attr_noexcept
#define MI_SOURCE_API2(tp,name,tp1,arg1,tp2,arg2) \
tp mi_source_##name(tp1 arg1, tp2 arg2 MI_SOURCE_PARAM) mi_attr_noexcept; \
tp mi_##name(tp1 arg1, tp2 arg2) mi_attr_noexcept { return mi_source_##name(arg1, arg2 MI_SOURCE_RET()); } \
tp mi_source_##name(tp1 arg1, tp2 arg2 MI_SOURCE_PARAM) mi_attr_noexcept
#define MI_SOURCE_API3(tp,name,tp1,arg1,tp2,arg2,tp3,arg3) \
tp mi_source_##name(tp1 arg1, tp2 arg2, tp3 arg3 MI_SOURCE_PARAM) mi_attr_noexcept; \
tp mi_##name(tp1 arg1, tp2 arg2, tp3 arg3) mi_attr_noexcept { return mi_source_##name(arg1, arg2, arg3 MI_SOURCE_RET()); } \
tp mi_source_##name(tp1 arg1, tp2 arg2, tp3 arg3 MI_SOURCE_PARAM) mi_attr_noexcept
#endif

View File

@ -47,6 +47,15 @@ terms of the MIT license. A copy of the license can be found in the file
void* operator new (std::size_t n, std::align_val_t al, const std::nothrow_t&) noexcept { return mi_source_new_aligned_nothrow(n, static_cast<size_t>(al) MI_SOURCE_RET()); }
void* operator new[](std::size_t n, std::align_val_t al, const std::nothrow_t&) noexcept { return mi_source_new_aligned_nothrow(n, static_cast<size_t>(al) MI_SOURCE_RET()); }
#endif
// Instances for debug override of the new operator
#ifndef NDEBUG
void* operator new(std::size_t n MI_SOURCE_PARAM) noexcept(false) { return mi_source_new(n MI_SOURCE_ARG); }
void* operator new[](std::size_t n MI_SOURCE_PARAM) noexcept(false) { return mi_source_new(n MI_SOURCE_ARG); }
void operator delete(void* p MI_SOURCE_PARAM) noexcept { mi_free(p); };
void operator delete[](void* p MI_SOURCE_PARAM) noexcept { mi_free(p); };
#endif
#endif
#endif // MIMALLOC_NEW_DELETE_H

View File

@ -30,37 +30,44 @@ not accidentally mix pointers from different allocators).
// Microsoft extensions
#define _expand(p,n) mi_expand(p,n)
#define _msize(p) mi_usable_size(p)
#define _recalloc(p,n,c) mi_recalloc(p,n,c)
#define _recalloc(p,n,c) mi_source_recalloc(p,n,c MI_SOURCE_LOC())
#define _strdup(s) mi_source_strdup(s MI_SOURCE_LOC())
#define _strndup(s) mi_source_strndup(s MI_SOURCE_LOC())
#define _wcsdup(s) (wchar_t*)mi_wcsdup((const unsigned short*)(s))
#define _mbsdup(s) mi_mbsdup(s)
#define _dupenv_s(b,n,v) mi_dupenv_s(b,n,v)
#define _wdupenv_s(b,n,v) mi_wdupenv_s((unsigned short*)(b),n,(const unsigned short*)(v))
#define _wcsdup(s) (wchar_t*)mi_source_wcsdup((const unsigned short*)(s) MI_SOURCE_LOC())
#define _mbsdup(s) mi_source_mbsdup(s MI_SOURCE_LOC())
#define _dupenv_s(b,n,v) mi_source_dupenv_s(b,n,v MI_SOURCE_LOC())
#define _wdupenv_s(b,n,v) mi_source_wdupenv_s((unsigned short*)(b),n,(const unsigned short*)(v) MI_SOURCE_LOC())
// Various Posix and Unix variants
#define reallocf(p,n) mi_reallocf(p,n)
#define reallocf(p,n) mi_source_reallocf(p,n MI_SOURCE_LOC())
#define malloc_size(p) mi_usable_size(p)
#define malloc_usable_size(p) mi_usable_size(p)
#define cfree(p) mi_free(p)
#define valloc(n) mi_valloc(n)
#define pvalloc(n) mi_pvalloc(n)
#define reallocarray(p,s,n) mi_reallocarray(p,s,n)
#define memalign(a,n) mi_memalign(a,n)
#define aligned_alloc(a,n) mi_aligned_alloc(a,n)
#define posix_memalign(p,a,n) mi_posix_memalign(p,a,n)
#define _posix_memalign(p,a,n) mi_posix_memalign(p,a,n)
#define valloc(n) mi_source_valloc(n MI_SOURCE_LOC())
#define pvalloc(n) mi_source_pvalloc(n MI_SOURCE_LOC())
#define reallocarray(p,s,n) mi_source_reallocarray(p,s,n MI_SOURCE_LOC())
#define memalign(a,n) mi_source_memalign(a,n MI_SOURCE_LOC())
#define aligned_alloc(a,n) mi_source_aligned_alloc(a,n MI_SOURCE_LOC())
#define posix_memalign(p,a,n) mi_source_posix_memalign(p,a,n MI_SOURCE_LOC())
#define _posix_memalign(p,a,n) mi_source_posix_memalign(p,a,n MI_SOURCE_LOC())
// Microsoft aligned variants
#define _aligned_malloc(n,a) mi_source_malloc_aligned(n,a MI_SOURCE_LOC())
#define _aligned_realloc(p,n,a) mi_realloc_aligned(p,n,a)
#define _aligned_recalloc(p,s,n,a) mi_aligned_recalloc(p,s,n,a)
#define _aligned_realloc(p,n,a) mi_source_realloc_aligned(p,n,a MI_SOURCE_LOC())
#define _aligned_recalloc(p,s,n,a) mi_source_recalloc_aligned(p,s,n,a MI_SOURCE_LOC())
#define _aligned_msize(p,a,o) mi_usable_size(p)
#define _aligned_free(p) mi_free(p)
#define _aligned_offset_malloc(n,a,o) mi_malloc_aligned_at(n,a,o)
#define _aligned_offset_realloc(p,n,a,o) mi_realloc_aligned_at(p,n,a,o)
#define _aligned_offset_recalloc(p,s,n,a,o) mi_recalloc_aligned_at(p,s,n,a,o)
#define _aligned_offset_malloc(n,a,o) mi_source_malloc_aligned_at(n,a,o MI_SOURCE_LOC())
#define _aligned_offset_realloc(p,n,a,o) mi_source_realloc_aligned_at(p,n,a,o MI_SOURCE_LOC())
#define _aligned_offset_recalloc(p,s,n,a,o) mi_source_recalloc_aligned_at(p,s,n,a,o MI_SOURCE_LOC())
// Overload new operators
// This requires including <mimalloc-new-delete.h> somewhere!
// See also <https://www.modernescpp.com/index.php/overloading-operator-new-and-delete-2>
#if !defined(NDEBUG) && defined(__cplusplus) && !defined(MI_NO_NEW_OVERRIDE)
#define new new(mi_source_loc(__FILE__,__LINE__))
#endif
#endif // MIMALLOC_OVERRIDE_H

View File

@ -101,9 +101,9 @@ mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_realloc(void* p, siz
mi_decl_export mi_decl_allocator void* mi_expand(void* p, size_t newsize) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2);
mi_decl_export void mi_free(void* p) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export char* mi_strdup(const char* s) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export char* mi_strndup(const char* s, size_t n) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export char* mi_realpath(const char* fname, char* resolved_name) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export mi_decl_allocator char* mi_strdup(const char* s) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export mi_decl_allocator char* mi_strndup(const char* s, size_t n) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export mi_decl_allocator char* mi_realpath(const char* fname, char* resolved_name) mi_attr_noexcept;
// ------------------------------------------------------
// Extended functionality
@ -187,12 +187,12 @@ mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_heap_mallocn(mi_heap
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_heap_malloc_small(mi_heap_t* heap, size_t size) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_heap_realloc(mi_heap_t* heap, void* p, size_t newsize) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(3);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_heap_reallocn(mi_heap_t* heap, void* p, size_t count, size_t size) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_heap_reallocn(mi_heap_t* heap, void* p, size_t count, size_t size) mi_attr_noexcept mi_attr_malloc;
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_heap_reallocf(mi_heap_t* heap, void* p, size_t newsize) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(3);
mi_decl_nodiscard mi_decl_export char* mi_heap_strdup(mi_heap_t* heap, const char* s) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export char* mi_heap_strndup(mi_heap_t* heap, const char* s, size_t n) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export char* mi_heap_realpath(mi_heap_t* heap, const char* fname, char* resolved_name) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export mi_decl_allocator char* mi_heap_strdup(mi_heap_t* heap, const char* s) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export mi_decl_allocator char* mi_heap_strndup(mi_heap_t* heap, const char* s, size_t n) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export mi_decl_allocator char* mi_heap_realpath(mi_heap_t* heap, const char* fname, char* resolved_name) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_heap_malloc_aligned(mi_heap_t* heap, size_t size, size_t alignment) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2) mi_attr_alloc_align(3);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_heap_malloc_aligned_at(mi_heap_t* heap, size_t size, size_t alignment, size_t offset) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2);
@ -211,31 +211,30 @@ mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_heap_realloc_aligned
// see <https://github.com/microsoft/mimalloc/issues/63#issuecomment-508272992>
// --------------------------------------------------------------------------------
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 newcount, size_t size) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(2,3);
mi_decl_nodiscard 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_nodiscard mi_decl_export mi_decl_allocator void* mi_recalloc(void* p, size_t newcount, size_t size) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(2,3);
mi_decl_export mi_decl_allocator void* mi_rezalloc_aligned(void* p, size_t newsize, size_t alignment) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2) mi_attr_alloc_align(3);
mi_decl_export mi_decl_allocator void* mi_rezalloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2);
mi_decl_export mi_decl_allocator void* mi_recalloc_aligned(void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(2,3) mi_attr_alloc_align(4);
mi_decl_export mi_decl_allocator void* mi_recalloc_aligned_at(void* p, size_t newcount, size_t size, size_t alignment, size_t offset) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(2,3);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_rezalloc_aligned(void* p, size_t newsize, size_t alignment) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2) mi_attr_alloc_align(3);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_rezalloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_recalloc_aligned(void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(2,3) mi_attr_alloc_align(4);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_recalloc_aligned_at(void* p, size_t newcount, size_t size, size_t alignment, size_t offset) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(2,3);
mi_decl_export mi_decl_allocator void* mi_heap_rezalloc(mi_heap_t* heap, void* p, size_t newsize) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(3);
mi_decl_export mi_decl_allocator void* mi_heap_recalloc(mi_heap_t* heap, void* p, size_t newcount, size_t size) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(3,4);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_heap_rezalloc(mi_heap_t* heap, void* p, size_t newsize) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(3);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_heap_recalloc(mi_heap_t* heap, void* p, size_t newcount, size_t size) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(3,4);
mi_decl_export mi_decl_allocator void* mi_heap_rezalloc_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(3) mi_attr_alloc_align(4);
mi_decl_export mi_decl_allocator void* mi_heap_rezalloc_aligned_at(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(3);
mi_decl_export mi_decl_allocator void* mi_heap_recalloc_aligned(mi_heap_t* heap, void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(3,4) mi_attr_alloc_align(5);
mi_decl_export mi_decl_allocator void* mi_heap_recalloc_aligned_at(mi_heap_t* heap, void* p, size_t newcount, size_t size, size_t alignment, size_t offset) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(3,4);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_heap_rezalloc_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(3) mi_attr_alloc_align(4);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_heap_rezalloc_aligned_at(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(3);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_heap_recalloc_aligned(mi_heap_t* heap, void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(3,4) mi_attr_alloc_align(5);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_heap_recalloc_aligned_at(mi_heap_t* heap, void* p, size_t newcount, size_t size, size_t alignment, size_t offset) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(3,4);
// ------------------------------------------------------
// Analysis
// ------------------------------------------------------
mi_decl_export bool mi_heap_contains_block(mi_heap_t* heap, const void* p);
mi_decl_export bool mi_heap_check_owned(mi_heap_t* heap, const void* p);
mi_decl_export bool mi_check_owned(const void* p);
mi_decl_nodiscard mi_decl_export bool mi_heap_contains_block(mi_heap_t* heap, const void* p);
mi_decl_nodiscard mi_decl_export bool mi_heap_check_owned(mi_heap_t* heap, const void* p);
mi_decl_nodiscard mi_decl_export bool mi_check_owned(const void* p);
// An area of heap space contains blocks of a single size.
typedef struct mi_heap_area_s {
@ -352,13 +351,13 @@ mi_decl_export void mi_free_aligned(void* p, size_t alignment) mi_attr_noexcept;
// The `mi_new` wrappers implement C++ semantics on out-of-memory instead of directly returning `NULL`.
// (and call `std::get_new_handler` and potentially raise a `std::bad_alloc` exception).
mi_decl_nodiscard mi_decl_export void* mi_new(size_t size) mi_attr_malloc mi_attr_alloc_size(1);
mi_decl_nodiscard mi_decl_export void* mi_new_aligned(size_t size, size_t alignment) mi_attr_malloc mi_attr_alloc_size(1) mi_attr_alloc_align(2);
mi_decl_nodiscard mi_decl_export void* mi_new_nothrow(size_t size) mi_attr_malloc mi_attr_alloc_size(1);
mi_decl_nodiscard mi_decl_export void* mi_new_aligned_nothrow(size_t size, size_t alignment) mi_attr_malloc mi_attr_alloc_size(1) mi_attr_alloc_align(2);
mi_decl_nodiscard mi_decl_export void* mi_new_n(size_t count, size_t size) mi_attr_malloc mi_attr_alloc_size2(1, 2);
mi_decl_nodiscard mi_decl_export void* mi_new_realloc(void* p, size_t newsize) mi_attr_malloc mi_attr_alloc_size(2);
mi_decl_nodiscard mi_decl_export void* mi_new_reallocn(void* p, size_t newcount, size_t size) mi_attr_malloc mi_attr_alloc_size2(2, 3);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_new(size_t size) mi_attr_malloc mi_attr_alloc_size(1);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_new_aligned(size_t size, size_t alignment) mi_attr_malloc mi_attr_alloc_size(1) mi_attr_alloc_align(2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_new_nothrow(size_t size) mi_attr_malloc mi_attr_alloc_size(1);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_new_aligned_nothrow(size_t size, size_t alignment) mi_attr_malloc mi_attr_alloc_size(1) mi_attr_alloc_align(2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_new_n(size_t count, size_t size) mi_attr_malloc mi_attr_alloc_size2(1, 2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_new_realloc(void* p, size_t newsize) mi_attr_malloc mi_attr_alloc_size(2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_new_reallocn(void* p, size_t newcount, size_t size) mi_attr_malloc mi_attr_alloc_size2(2, 3);
// ---------------------------------------------------------------------------------------------
@ -393,20 +392,61 @@ mi_decl_export void* mi_source_unpack(mi_source_t source, const char** fname, in
#define MI_SOURCE_LOC() , mi_source_loc(__FILE__,__LINE__)
#endif
// malloc
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_malloc(size_t size MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(1);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_calloc(size_t count, size_t size MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(1, 2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_realloc(void* p, size_t newsize MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_malloc_aligned(size_t size, size_t alignment MI_SOURCE_PARAM) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export mi_decl_allocator char* mi_source_strdup(const char* s MI_SOURCE_PARAM) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export mi_decl_allocator char* mi_source_strndup(const char* s, size_t n MI_SOURCE_PARAM) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export mi_decl_allocator char* mi_source_realpath(const char* fname, char* resolved_name MI_SOURCE_PARAM) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_malloc_small(size_t size MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(1);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_zalloc_small(size_t size MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(1);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_zalloc(size_t size MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(1);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_mallocn(size_t count, size_t size MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(1, 2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_reallocn(void* p, size_t count, size_t size MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(2, 3);
mi_decl_nodiscard mi_decl_export char* mi_source_strdup(const char* s MI_SOURCE_PARAM) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export char* mi_source_strndup(const char* s, size_t n MI_SOURCE_PARAM) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export char* mi_source_realpath(const char* fname, char* resolved_name MI_SOURCE_PARAM) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_reallocf(void* p, size_t newsize MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_malloc_aligned(size_t size, size_t alignment MI_SOURCE_PARAM) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_malloc_aligned_at(size_t size, size_t alignment, size_t offset MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(1);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_zalloc_aligned(size_t size, size_t alignment MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(1) mi_attr_alloc_align(2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_zalloc_aligned_at(size_t size, size_t alignment, size_t offset MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(1);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_calloc_aligned(size_t count, size_t size, size_t alignment MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(1, 2) mi_attr_alloc_align(3);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_calloc_aligned_at(size_t count, size_t size, size_t alignment, size_t offset MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(1, 2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_realloc_aligned(void* p, size_t newsize, size_t alignment MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2) mi_attr_alloc_align(3);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_realloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_rezalloc(void* p, size_t newsize MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_recalloc(void* p, size_t newcount, size_t size MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(2, 3);
mi_decl_nodiscard mi_decl_export void* mi_source_new(size_t size MI_SOURCE_PARAM) mi_attr_malloc mi_attr_alloc_size(1);
mi_decl_nodiscard mi_decl_export void* mi_source_new_aligned(size_t size, size_t alignment MI_SOURCE_PARAM) mi_attr_malloc mi_attr_alloc_size(1) mi_attr_alloc_align(2);
mi_decl_nodiscard mi_decl_export void* mi_source_new_nothrow(size_t size MI_SOURCE_PARAM) mi_attr_malloc mi_attr_alloc_size(1);
mi_decl_nodiscard mi_decl_export void* mi_source_new_aligned_nothrow(size_t size, size_t alignment MI_SOURCE_PARAM) mi_attr_malloc mi_attr_alloc_size(1) mi_attr_alloc_align(2);
// new delete
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_new(size_t size MI_SOURCE_PARAM) mi_attr_malloc mi_attr_alloc_size(1);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_new_aligned(size_t size, size_t alignment MI_SOURCE_PARAM) mi_attr_malloc mi_attr_alloc_size(1) mi_attr_alloc_align(2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_new_nothrow(size_t size MI_SOURCE_PARAM) mi_attr_malloc mi_attr_alloc_size(1);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_new_aligned_nothrow(size_t size, size_t alignment MI_SOURCE_PARAM) mi_attr_malloc mi_attr_alloc_size(1) mi_attr_alloc_align(2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_new_n(size_t count, size_t size MI_SOURCE_PARAM) mi_attr_malloc mi_attr_alloc_size2(1, 2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_new_realloc(void* p, size_t newsize MI_SOURCE_PARAM) mi_attr_malloc mi_attr_alloc_size(2);
mi_decl_nodiscard mi_decl_export mi_decl_allocator void* mi_source_new_reallocn(void* p, size_t newcount, size_t size MI_SOURCE_PARAM) mi_attr_malloc mi_attr_alloc_size2(2, 3);
// posix
mi_decl_export int mi_source_posix_memalign(void** p, size_t alignment, size_t size MI_SOURCE_PARAM) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export void* mi_source_memalign(size_t alignment, size_t size MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2) mi_attr_alloc_align(1);
mi_decl_nodiscard mi_decl_export void* mi_source_valloc(size_t size MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(1);
mi_decl_nodiscard mi_decl_export void* mi_source_pvalloc(size_t size MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(1);
mi_decl_nodiscard mi_decl_export void* mi_source_aligned_alloc(size_t alignment, size_t size MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(2) mi_attr_alloc_align(1);
mi_decl_nodiscard mi_decl_export void* mi_source_reallocarray(void* p, size_t count, size_t size MI_SOURCE_PARAM) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size2(2, 3);
mi_decl_nodiscard mi_decl_export void* mi_source_aligned_recalloc(void* p, size_t newcount, size_t size, size_t alignment MI_SOURCE_PARAM) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export void* mi_sourcealigned_offset_recalloc(void* p, size_t newcount, size_t size, size_t alignment, size_t offset MI_SOURCE_PARAM) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export unsigned short* mi_source_wcsdup(const unsigned short* s MI_SOURCE_PARAM) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export unsigned char* mi_source_mbsdup(const unsigned char* s MI_SOURCE_PARAM) mi_attr_noexcept;
mi_decl_export int mi_source_dupenv_s(char** buf, size_t* size, const char* name MI_SOURCE_PARAM) mi_attr_noexcept;
mi_decl_export int mi_source_wdupenv_s(unsigned short** buf, size_t* size, const unsigned short* name MI_SOURCE_PARAM) mi_attr_noexcept;
// ----------------------------------------------------------------------

View File

@ -14,14 +14,14 @@ terms of the MIT license. A copy of the license can be found in the file
// Aligned Allocation
// ------------------------------------------------------
static void* mi_heapx_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero MI_SOURCE_PARAM) mi_attr_noexcept {
static void* mi_heap_source_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero MI_SOURCE_PARAM) mi_attr_noexcept {
// note: we don't require `size > offset`, we just guarantee that
// the address at offset is aligned regardless of the allocated size.
mi_assert(alignment > 0 && alignment % sizeof(void*) == 0);
if (mi_unlikely(size > PTRDIFF_MAX)) return NULL; // we don't allocate more than PTRDIFF_MAX (see <https://sourceware.org/ml/libc-announce/2019/msg00001.html>)
if (mi_unlikely(alignment==0 || !_mi_is_power_of_two(alignment))) return NULL; // require power-of-two (see <https://en.cppreference.com/w/c/memory/aligned_alloc>)
if (alignment <= MI_MAX_ALIGN_SIZE && offset==0) return _mi_heapx_malloc_zero(heap, size, zero MI_SOURCE_ARG);
if (alignment <= MI_MAX_ALIGN_SIZE && offset==0) return _mi_heap_source_malloc_zero(heap, size, zero MI_SOURCE_ARG);
const uintptr_t align_mask = alignment-1; // for any x, `(x & align_mask) == (x % alignment)`
// try if there is a small block available with just the right alignment
@ -44,13 +44,13 @@ static void* mi_heapx_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t
// use regular allocation if it is guaranteed to fit the alignment constraints
if (offset==0 && alignment<=padsize && padsize<=MI_MEDIUM_OBJ_SIZE_MAX && (padsize&align_mask)==0) {
void* p = _mi_heapx_malloc_zero(heap, size, zero MI_SOURCE_ARG);
void* p = _mi_heap_source_malloc_zero(heap, size, zero MI_SOURCE_ARG);
mi_assert_internal(p == NULL || ((uintptr_t)p % alignment) == 0);
return p;
}
// otherwise over-allocate
void* p = _mi_heapx_malloc_zero(heap, size + alignment - 1, zero MI_SOURCE_ARG);
void* p = _mi_heap_source_malloc_zero(heap, size + alignment - 1, zero MI_SOURCE_ARG);
if (p == NULL) return NULL;
// .. and align within the allocation
@ -64,98 +64,53 @@ static void* mi_heapx_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t
}
static inline void* mi_heapx_malloc_aligned_at(mi_heap_t* heap, size_t size, size_t alignment, size_t offset MI_SOURCE_PARAM) mi_attr_noexcept {
return mi_heapx_malloc_zero_aligned_at(heap, size, alignment, offset, false MI_SOURCE_ARG);
MI_ALLOC_API3(void*, malloc_aligned_at, mi_heap_t*, heap, size_t, size, size_t, alignment, size_t, offset)
{
return mi_heap_source_malloc_zero_aligned_at(heap, size, alignment, offset, false MI_SOURCE_ARG);
}
extern inline mi_decl_allocator void* _mi_heapx_malloc_aligned(mi_heap_t* heap, size_t size, size_t alignment MI_SOURCE_PARAM) mi_attr_noexcept {
return mi_heapx_malloc_zero_aligned_at(heap, size, alignment, 0, false MI_SOURCE_ARG);
MI_ALLOC_API2(void*, malloc_aligned, mi_heap_t*,heap, size_t, size, size_t, alignment)
{
return mi_heap_source_malloc_zero_aligned_at(heap, size, alignment, 0, false MI_SOURCE_ARG);
}
static inline void* mi_heapx_zalloc_aligned_at(mi_heap_t* heap, size_t size, size_t alignment, size_t offset MI_SOURCE_PARAM) mi_attr_noexcept {
return mi_heapx_malloc_zero_aligned_at(heap, size, alignment, offset, true MI_SOURCE_ARG);
MI_ALLOC_API3(void*, zalloc_aligned_at, mi_heap_t*, heap, size_t, size, size_t, alignment, size_t, offset)
{
return mi_heap_source_malloc_zero_aligned_at(heap, size, alignment, offset, true MI_SOURCE_ARG);
}
static inline void* mi_heapx_zalloc_aligned(mi_heap_t* heap, size_t size, size_t alignment MI_SOURCE_PARAM) mi_attr_noexcept {
return mi_heapx_malloc_zero_aligned_at(heap, size, alignment, 0, true MI_SOURCE_ARG);
MI_ALLOC_API2(void*, zalloc_aligned, mi_heap_t*,heap, size_t, size, size_t, alignment)
{
return mi_heap_source_malloc_zero_aligned_at(heap, size, alignment, 0, true MI_SOURCE_ARG);
}
static inline void* mi_heapx_calloc_aligned_at(mi_heap_t* heap, size_t count, size_t size, size_t alignment, size_t offset MI_SOURCE_PARAM) mi_attr_noexcept {
MI_ALLOC_API4(void*, calloc_aligned_at, mi_heap_t*, heap, size_t, count, size_t, size, size_t, alignment, size_t, offset)
{
size_t total;
if (mi_count_size_overflow(count, size, &total)) return NULL;
return mi_heapx_malloc_zero_aligned_at(heap, total, alignment, offset, true MI_SOURCE_ARG);
return mi_heap_source_malloc_zero_aligned_at(heap, total, alignment, offset, true MI_SOURCE_ARG);
}
static inline void* mi_heapx_calloc_aligned(mi_heap_t* heap, size_t count, size_t size, size_t alignment MI_SOURCE_PARAM) mi_attr_noexcept {
MI_ALLOC_API3(void*, calloc_aligned, mi_heap_t*, heap, size_t, count, size_t, size, size_t, alignment)
{
size_t total;
if (mi_count_size_overflow(count, size, &total)) return NULL;
return mi_heapx_malloc_zero_aligned_at(heap, total, alignment, 0, true MI_SOURCE_ARG);
}
mi_decl_allocator void* mi_heap_malloc_aligned_at(mi_heap_t* heap, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
return mi_heapx_malloc_aligned_at(heap, size, alignment, offset MI_SOURCE_RET());
}
mi_decl_allocator void* mi_heap_malloc_aligned(mi_heap_t* heap, size_t size, size_t alignment) mi_attr_noexcept {
return _mi_heapx_malloc_aligned(heap, size, alignment MI_SOURCE_RET());
}
mi_decl_allocator void* mi_heap_zalloc_aligned_at(mi_heap_t* heap, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
return mi_heapx_zalloc_aligned_at(heap, size, alignment, offset MI_SOURCE_RET());
}
mi_decl_allocator void* mi_heap_zalloc_aligned(mi_heap_t* heap, size_t size, size_t alignment) mi_attr_noexcept {
return mi_heapx_zalloc_aligned(heap, size, alignment MI_SOURCE_RET());
}
mi_decl_allocator void* mi_heap_calloc_aligned_at(mi_heap_t* heap, size_t count, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
return mi_heapx_calloc_aligned_at(heap, count, size, alignment, offset MI_SOURCE_RET());
}
mi_decl_allocator void* mi_heap_calloc_aligned(mi_heap_t* heap, size_t count, size_t size, size_t alignment) mi_attr_noexcept {
return mi_heapx_calloc_aligned(heap, count, size, alignment MI_SOURCE_RET());
return mi_heap_source_malloc_zero_aligned_at(heap, total, alignment, 0, true MI_SOURCE_ARG);
}
mi_decl_allocator void* mi_malloc_aligned_at(size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
return mi_heapx_malloc_aligned_at(mi_get_default_heap(), size, alignment, offset MI_SOURCE_RET());
}
mi_decl_allocator void* mi_source_malloc_aligned(size_t size, size_t alignment MI_SOURCE_PARAM) mi_attr_noexcept {
return _mi_heapx_malloc_aligned(mi_get_default_heap(), size, alignment MI_SOURCE_ARG);
}
mi_decl_allocator void* mi_malloc_aligned(size_t size, size_t alignment) mi_attr_noexcept {
return mi_source_malloc_aligned(size, alignment MI_SOURCE_RET());
}
mi_decl_allocator void* mi_zalloc_aligned_at(size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
return mi_heapx_zalloc_aligned_at(mi_get_default_heap(), size, alignment, offset MI_SOURCE_RET());
}
mi_decl_allocator void* mi_zalloc_aligned(size_t size, size_t alignment) mi_attr_noexcept {
return mi_heapx_zalloc_aligned(mi_get_default_heap(), size, alignment MI_SOURCE_RET());
}
mi_decl_allocator void* mi_calloc_aligned_at(size_t count, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
return mi_heapx_calloc_aligned_at(mi_get_default_heap(), count, size, alignment, offset MI_SOURCE_RET());
}
mi_decl_allocator void* mi_calloc_aligned(size_t count, size_t size, size_t alignment) mi_attr_noexcept {
return mi_heapx_calloc_aligned(mi_get_default_heap(), count, size, alignment MI_SOURCE_RET());
}
static void* mi_heapx_realloc_zero_aligned_at(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, size_t offset, bool zero MI_SOURCE_PARAM) mi_attr_noexcept {
static void* mi_heap_source_realloc_zero_aligned_at(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, size_t offset, bool zero MI_SOURCE_PARAM) mi_attr_noexcept {
mi_assert(alignment > 0);
if (alignment <= sizeof(uintptr_t)) return _mi_heapx_realloc_zero(heap,p,newsize,zero MI_SOURCE_ARG);
if (p == NULL) return mi_heapx_malloc_zero_aligned_at(heap,newsize,alignment,offset,zero MI_SOURCE_ARG);
if (alignment <= sizeof(uintptr_t)) return _mi_heap_source_realloc_zero(heap,p,newsize,zero MI_SOURCE_ARG);
if (p == NULL) return mi_heap_source_malloc_zero_aligned_at(heap,newsize,alignment,offset,zero MI_SOURCE_ARG);
size_t size = mi_usable_size(p);
if (newsize <= size && newsize >= (size - (size / 2))
&& (((uintptr_t)p + offset) % alignment) == 0) {
return p; // reallocation still fits, is aligned and not more than 50% waste
}
else {
void* newp = mi_heapx_malloc_aligned_at(heap,newsize,alignment,offset MI_SOURCE_ARG);
void* newp = mi_heap_source_malloc_aligned_at(heap,newsize,alignment,offset MI_SOURCE_ARG);
if (newp != NULL) {
if (zero && newsize > size) {
const mi_page_t* page = _mi_ptr_page(newp);
@ -176,90 +131,43 @@ static void* mi_heapx_realloc_zero_aligned_at(mi_heap_t* heap, void* p, size_t n
}
}
static void* mi_heapx_realloc_zero_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, bool zero MI_SOURCE_PARAM) mi_attr_noexcept {
static void* mi_heap_source_realloc_zero_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, bool zero MI_SOURCE_PARAM) mi_attr_noexcept {
mi_assert(alignment > 0);
if (alignment <= sizeof(uintptr_t)) return _mi_heapx_realloc_zero(heap,p,newsize,zero MI_SOURCE_ARG);
if (alignment <= sizeof(uintptr_t)) return _mi_heap_source_realloc_zero(heap,p,newsize,zero MI_SOURCE_ARG);
size_t offset = ((uintptr_t)p % alignment); // use offset of previous allocation (p can be NULL)
return mi_heapx_realloc_zero_aligned_at(heap,p,newsize,alignment,offset,zero MI_SOURCE_ARG);
return mi_heap_source_realloc_zero_aligned_at(heap,p,newsize,alignment,offset,zero MI_SOURCE_ARG);
}
static inline void* mi_heapx_realloc_aligned_at(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, size_t offset MI_SOURCE_PARAM) mi_attr_noexcept {
return mi_heapx_realloc_zero_aligned_at(heap,p,newsize,alignment,offset,false MI_SOURCE_ARG);
MI_ALLOC_API4(void*, realloc_aligned_at, mi_heap_t*, heap, void*, p, size_t, newsize, size_t, alignment, size_t, offset)
{
return mi_heap_source_realloc_zero_aligned_at(heap,p,newsize,alignment,offset,false MI_SOURCE_ARG);
}
static inline void* mi_heapx_realloc_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment MI_SOURCE_PARAM) mi_attr_noexcept {
return mi_heapx_realloc_zero_aligned(heap,p,newsize,alignment,false MI_SOURCE_ARG);
MI_ALLOC_API3(void*, realloc_aligned, mi_heap_t*, heap, void*, p, size_t, newsize, size_t, alignment)
{
return mi_heap_source_realloc_zero_aligned(heap,p,newsize,alignment,false MI_SOURCE_ARG);
}
static inline void* mi_heapx_rezalloc_aligned_at(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, size_t offset MI_SOURCE_PARAM) mi_attr_noexcept {
return mi_heapx_realloc_zero_aligned_at(heap, p, newsize, alignment, offset, true MI_SOURCE_ARG);
MI_ALLOC_API4(void*, rezalloc_aligned_at, mi_heap_t*, heap, void*, p, size_t, newsize, size_t, alignment, size_t, offset)
{
return mi_heap_source_realloc_zero_aligned_at(heap, p, newsize, alignment, offset, true MI_SOURCE_ARG);
}
static inline void* mi_heapx_rezalloc_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment MI_SOURCE_PARAM) mi_attr_noexcept {
return mi_heapx_realloc_zero_aligned(heap, p, newsize, alignment, true MI_SOURCE_ARG);
MI_ALLOC_API3(void*, rezalloc_aligned, mi_heap_t*, heap, void*, p, size_t, newsize, size_t, alignment)
{
return mi_heap_source_realloc_zero_aligned(heap, p, newsize, alignment, true MI_SOURCE_ARG);
}
static inline void* mi_heapx_recalloc_aligned_at(mi_heap_t* heap, void* p, size_t newcount, size_t size, size_t alignment, size_t offset MI_SOURCE_PARAM) mi_attr_noexcept {
MI_ALLOC_API5(void*, recalloc_aligned_at, mi_heap_t*, heap, void*, p, size_t, newcount, size_t, size, size_t, alignment, size_t, offset)
{
size_t total;
if (mi_count_size_overflow(newcount, size, &total)) return NULL;
return mi_heapx_realloc_zero_aligned_at(heap, p, total, alignment, offset, true MI_SOURCE_ARG);
return mi_heap_source_realloc_zero_aligned_at(heap, p, total, alignment, offset, true MI_SOURCE_ARG);
}
static inline void* mi_heapx_recalloc_aligned(mi_heap_t* heap, void* p, size_t newcount, size_t size, size_t alignment MI_SOURCE_PARAM) mi_attr_noexcept {
MI_ALLOC_API4(void*, recalloc_aligned, mi_heap_t*, heap, void*, p, size_t, newcount, size_t, size, size_t, alignment)
{
size_t total;
if (mi_count_size_overflow(newcount, size, &total)) return NULL;
return mi_heapx_realloc_zero_aligned_at(heap, p, total, alignment, 0, true MI_SOURCE_ARG);
}
mi_decl_allocator void* mi_realloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept {
return mi_heapx_realloc_aligned_at(mi_get_default_heap(), p, newsize, alignment, offset MI_SOURCE_RET());
}
mi_decl_allocator void* mi_realloc_aligned(void* p, size_t newsize, size_t alignment) mi_attr_noexcept {
return mi_heapx_realloc_aligned(mi_get_default_heap(), p, newsize, alignment MI_SOURCE_RET());
}
mi_decl_allocator void* mi_rezalloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept {
return mi_heapx_rezalloc_aligned_at(mi_get_default_heap(), p, newsize, alignment, offset MI_SOURCE_RET());
}
mi_decl_allocator void* mi_rezalloc_aligned(void* p, size_t newsize, size_t alignment) mi_attr_noexcept {
return mi_heapx_rezalloc_aligned(mi_get_default_heap(), p, newsize, alignment MI_SOURCE_RET());
}
mi_decl_allocator void* mi_recalloc_aligned_at(void* p, size_t newcount, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
return mi_heapx_recalloc_aligned_at(mi_get_default_heap(), p, newcount, size, alignment, offset MI_SOURCE_RET());
}
mi_decl_allocator void* mi_recalloc_aligned(void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept {
return mi_heapx_recalloc_aligned(mi_get_default_heap(), p, newcount, size, alignment MI_SOURCE_RET());
}
mi_decl_allocator void* mi_heap_realloc_aligned_at(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept {
return mi_heapx_realloc_aligned_at(heap, p, newsize, alignment, offset MI_SOURCE_RET());
}
mi_decl_allocator void* mi_heap_realloc_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment) mi_attr_noexcept {
return mi_heapx_realloc_aligned(heap, p, newsize, alignment MI_SOURCE_RET());
}
mi_decl_allocator void* mi_heap_rezalloc_aligned_at(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept {
return mi_heapx_rezalloc_aligned_at(heap, p, newsize, alignment, offset MI_SOURCE_RET());
}
mi_decl_allocator void* mi_heap_rezalloc_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment) mi_attr_noexcept {
return mi_heapx_rezalloc_aligned(heap, p, newsize, alignment MI_SOURCE_RET());
}
mi_decl_allocator void* mi_heap_recalloc_aligned_at(mi_heap_t* heap, void* p, size_t newcount, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
size_t total;
if (mi_count_size_overflow(newcount, size, &total)) return NULL;
return mi_heapx_rezalloc_aligned_at(heap, p, total, alignment, offset MI_SOURCE_RET());
}
mi_decl_allocator void* mi_heap_recalloc_aligned(mi_heap_t* heap, void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept {
size_t total;
if (mi_count_size_overflow(newcount, size, &total)) return NULL;
return mi_heapx_rezalloc_aligned(heap, p, total, alignment MI_SOURCE_RET());
return mi_heap_source_realloc_zero_aligned_at(heap, p, total, alignment, 0, true MI_SOURCE_ARG);
}

View File

@ -26,18 +26,18 @@ terms of the MIT license. A copy of the license can be found in the file
#else
#define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default")))
#endif
#define MI_FORWARD1(fun,x) MI_FORWARD(fun)
#define MI_FORWARD2(fun,x,y) MI_FORWARD(fun)
#define MI_FORWARD3(fun,x,y,z) MI_FORWARD(fun)
#define MI_FORWARD0(fun,x) MI_FORWARD(fun)
#define MI_FORWARD02(fun,x,y) MI_FORWARD(fun)
#define MI_FORWARD1(fun,x) MI_FORWARD(mi_##fun)
#define MI_FORWARD2(fun,x,y) MI_FORWARD(mi_##fun)
#define MI_FORWARD3(fun,x,y,z) MI_FORWARD(mi_##fun)
#define MI_FORWARD0(fun,x) MI_FORWARD(mi_##fun)
#define MI_FORWARD02(fun,x,y) MI_FORWARD(mi_##fun)
#else
// use forwarding by calling our `mi_` function
#define MI_FORWARD1(fun,x) { return fun(x); }
#define MI_FORWARD2(fun,x,y) { return fun(x,y); }
#define MI_FORWARD3(fun,x,y,z) { return fun(x,y,z); }
#define MI_FORWARD0(fun,x) { fun(x); }
#define MI_FORWARD02(fun,x,y) { fun(x,y); }
#define MI_FORWARD1(fun,x) { return mi_source_##fun(x MI_SOURCE_RET()); }
#define MI_FORWARD2(fun,x,y) { return mi_source_##fun(x,y MI_SOURCE_RET()); }
#define MI_FORWARD3(fun,x,y,z) { return mi_source_##fun(x,y,z MI_SOURCE_RET()); }
#define MI_FORWARD0(fun,x) { mi_##fun(x); }
#define MI_FORWARD02(fun,x,y) { mi_##fun(x,y); }
#endif
#if defined(__APPLE__) && defined(MI_SHARED_LIB_EXPORT) && defined(MI_INTERPOSE)
@ -68,10 +68,10 @@ terms of the MIT license. A copy of the license can be found in the file
// we just override new/delete which does work in a static library.
#else
// On all other systems forward to our API
void* malloc(size_t size) MI_FORWARD1(mi_malloc, size);
void* calloc(size_t size, size_t n) MI_FORWARD2(mi_calloc, size, n);
void* realloc(void* p, size_t newsize) MI_FORWARD2(mi_realloc, p, newsize);
void free(void* p) MI_FORWARD0(mi_free, p);
void* malloc(size_t size) MI_FORWARD1(malloc, size);
void* calloc(size_t size, size_t n) MI_FORWARD2(calloc, size, n);
void* realloc(void* p, size_t newsize) MI_FORWARD2(realloc, p, newsize);
void free(void* p) MI_FORWARD0(free, p);
#endif
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__MACH__)
@ -89,18 +89,18 @@ terms of the MIT license. A copy of the license can be found in the file
// see <https://en.cppreference.com/w/cpp/memory/new/operator_new>
// ------------------------------------------------------
#include <new>
void operator delete(void* p) noexcept MI_FORWARD0(mi_free,p);
void operator delete[](void* p) noexcept MI_FORWARD0(mi_free,p);
void operator delete(void* p) noexcept MI_FORWARD0(free,p);
void operator delete[](void* p) noexcept MI_FORWARD0(free,p);
void* operator new(std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n);
void* operator new[](std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n);
void* operator new(std::size_t n) noexcept(false) MI_FORWARD1(new,n);
void* operator new[](std::size_t n) noexcept(false) MI_FORWARD1(new,n);
void* operator new (std::size_t n, const std::nothrow_t& tag) noexcept { UNUSED(tag); return mi_new_nothrow(n); }
void* operator new[](std::size_t n, const std::nothrow_t& tag) noexcept { UNUSED(tag); return mi_new_nothrow(n); }
void* operator new (std::size_t n, const std::nothrow_t& ) noexcept { return mi_source_new_nothrow(n MI_SOURCE_RET()); }
void* operator new[](std::size_t n, const std::nothrow_t& ) noexcept { return mi_source_new_nothrow(n MI_SOURCE_RET()); }
#if (__cplusplus >= 201402L || _MSC_VER >= 1916)
void operator delete (void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n);
void operator delete[](void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n);
void operator delete (void* p, std::size_t n) noexcept MI_FORWARD02(free_size,p,n);
void operator delete[](void* p, std::size_t n) noexcept MI_FORWARD02(free_size,p,n);
#endif
#if (__cplusplus > 201402L || defined(__cpp_aligned_new)) && (!defined(__GNUC__) || (__GNUC__ > 5))
@ -109,10 +109,10 @@ terms of the MIT license. A copy of the license can be found in the file
void operator delete (void* p, std::size_t n, std::align_val_t al) noexcept { mi_free_size_aligned(p, n, static_cast<size_t>(al)); };
void operator delete[](void* p, std::size_t n, std::align_val_t al) noexcept { mi_free_size_aligned(p, n, static_cast<size_t>(al)); };
void* operator new( std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
void* operator new[]( std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
void* operator new (std::size_t n, std::align_val_t al, const std::nothrow_t&) noexcept { return mi_new_aligned_nothrow(n, static_cast<size_t>(al)); }
void* operator new[](std::size_t n, std::align_val_t al, const std::nothrow_t&) noexcept { return mi_new_aligned_nothrow(n, static_cast<size_t>(al)); }
void* operator new( std::size_t n, std::align_val_t al) noexcept(false) { return mi_source_new_aligned(n, static_cast<size_t>(al) MI_SOURCE_RET()); }
void* operator new[]( std::size_t n, std::align_val_t al) noexcept(false) { return mi_source_new_aligned(n, static_cast<size_t>(al) MI_SOURCE_RET()); }
void* operator new (std::size_t n, std::align_val_t al, const std::nothrow_t&) noexcept { return mi_source_new_aligned_nothrow(n, static_cast<size_t>(al) MI_SOURCE_RET()); }
void* operator new[](std::size_t n, std::align_val_t al, const std::nothrow_t&) noexcept { return mi_source_new_aligned_nothrow(n, static_cast<size_t>(al) MI_SOURCE_RET()); }
#endif
#elif (defined(__GNUC__) || defined(__clang__))
@ -121,10 +121,10 @@ terms of the MIT license. A copy of the license can be found in the file
// used by GCC and CLang).
// See <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling>
// ------------------------------------------------------
void _ZdlPv(void* p) MI_FORWARD0(mi_free,p); // delete
void _ZdaPv(void* p) MI_FORWARD0(mi_free,p); // delete[]
void _ZdlPvm(void* p, size_t n) MI_FORWARD02(mi_free_size,p,n);
void _ZdaPvm(void* p, size_t n) MI_FORWARD02(mi_free_size,p,n);
void _ZdlPv(void* p) MI_FORWARD0(free,p); // delete
void _ZdaPv(void* p) MI_FORWARD0(free,p); // delete[]
void _ZdlPvm(void* p, size_t n) MI_FORWARD02(free_size,p,n);
void _ZdaPvm(void* p, size_t n) MI_FORWARD02(free_size,p,n);
void _ZdlPvSt11align_val_t(void* p, size_t al) { mi_free_aligned(p,al); }
void _ZdaPvSt11align_val_t(void* p, size_t al) { mi_free_aligned(p,al); }
void _ZdlPvmSt11align_val_t(void* p, size_t n, size_t al) { mi_free_size_aligned(p,n,al); }
@ -132,19 +132,19 @@ terms of the MIT license. A copy of the license can be found in the file
typedef struct mi_nothrow_s { } mi_nothrow_t;
#if (MI_INTPTR_SIZE==8)
void* _Znwm(size_t n) MI_FORWARD1(mi_new,n); // new 64-bit
void* _Znam(size_t n) MI_FORWARD1(mi_new,n); // new[] 64-bit
void* _ZnwmSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al);
void* _ZnamSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al);
void* _ZnwmRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_new_nothrow(n); }
void* _ZnamRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_new_nothrow(n); }
void* _ZnwmSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
void* _ZnamSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
void* _Znwm(size_t n) MI_FORWARD1(new,n); // new 64-bit
void* _Znam(size_t n) MI_FORWARD1(new,n); // new[] 64-bit
void* _ZnwmSt11align_val_t(size_t n, size_t al) MI_FORWARD2(new_aligned, n, al);
void* _ZnamSt11align_val_t(size_t n, size_t al) MI_FORWARD2(new_aligned, n, al);
void* _ZnwmRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_source_new_nothrow(n MI_SOURCE_RET()); }
void* _ZnamRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_source_new_nothrow(n MI_SOURCE_RET()); }
void* _ZnwmSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { UNUSED(tag); return mi_new_aligned_nothrow(n,al MI_SOURCE_RET()); }
void* _ZnamSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { UNUSED(tag); return mi_new_aligned_nothrow(n,al MI_SOURCE_RET()); }
#elif (MI_INTPTR_SIZE==4)
void* _Znwj(size_t n) MI_FORWARD1(mi_new,n); // new 64-bit
void* _Znaj(size_t n) MI_FORWARD1(mi_new,n); // new[] 64-bit
void* _ZnwjSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al);
void* _ZnajSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al);
void* _Znwj(size_t n) MI_FORWARD1(new,n); // new 64-bit
void* _Znaj(size_t n) MI_FORWARD1(new,n); // new[] 64-bit
void* _ZnwjSt11align_val_t(size_t n, size_t al) MI_FORWARD2(new_aligned, n, al);
void* _ZnajSt11align_val_t(size_t n, size_t al) MI_FORWARD2(new_aligned, n, al);
void* _ZnwjRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_new_nothrow(n); }
void* _ZnajRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_new_nothrow(n); }
void* _ZnwjSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
@ -163,31 +163,31 @@ extern "C" {
// Posix & Unix functions definitions
// ------------------------------------------------------
void* reallocf(void* p, size_t newsize) MI_FORWARD2(mi_reallocf,p,newsize);
size_t malloc_size(void* p) MI_FORWARD1(mi_usable_size,p);
size_t malloc_usable_size(void *p) MI_FORWARD1(mi_usable_size,p);
void cfree(void* p) MI_FORWARD0(mi_free, p);
void* reallocf(void* p, size_t newsize) MI_FORWARD2(reallocf,p,newsize);
void cfree(void* p) MI_FORWARD0(free, p);
size_t malloc_size(void* p) { return mi_usable_size(p); }
size_t malloc_usable_size(void* p) { return mi_usable_size(p); }
// no forwarding here due to aliasing/name mangling issues
void* valloc(size_t size) { return mi_valloc(size); }
void* pvalloc(size_t size) { return mi_pvalloc(size); }
void* reallocarray(void* p, size_t count, size_t size) { return mi_reallocarray(p, count, size); }
void* memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
void* aligned_alloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
int posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p, alignment, size); }
void* valloc(size_t size) { return mi_source_valloc(size MI_SOURCE_RET()); }
void* pvalloc(size_t size) { return mi_source_pvalloc(size MI_SOURCE_RET()); }
void* reallocarray(void* p, size_t count, size_t size) { return mi_source_reallocarray(p, count, size MI_SOURCE_RET()); }
void* memalign(size_t alignment, size_t size) { return mi_source_memalign(alignment, size MI_SOURCE_RET()); }
void* aligned_alloc(size_t alignment, size_t size) { return mi_source_aligned_alloc(alignment, size MI_SOURCE_RET()); }
int posix_memalign(void** p, size_t alignment, size_t size) { return mi_source_posix_memalign(p, alignment, size MI_SOURCE_RET()); }
#if defined(__GLIBC__) && defined(__linux__)
// forward __libc interface (needed for glibc-based Linux distributions)
void* __libc_malloc(size_t size) MI_FORWARD1(mi_malloc,size);
void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(mi_calloc,count,size);
void* __libc_realloc(void* p, size_t size) MI_FORWARD2(mi_realloc,p,size);
void __libc_free(void* p) MI_FORWARD0(mi_free,p);
void __libc_cfree(void* p) MI_FORWARD0(mi_free,p);
void* __libc_malloc(size_t size) MI_FORWARD1(malloc,size);
void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(calloc,count,size);
void* __libc_realloc(void* p, size_t size) MI_FORWARD2(realloc,p,size);
void __libc_free(void* p) MI_FORWARD0(free,p);
void __libc_cfree(void* p) MI_FORWARD0(free,p);
void* __libc_valloc(size_t size) { return mi_valloc(size); }
void* __libc_pvalloc(size_t size) { return mi_pvalloc(size); }
void* __libc_memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
int __posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p, alignment, size); }
void* __libc_valloc(size_t size) { return mi_source_valloc(size MI_SOURCE_RET()); }
void* __libc_pvalloc(size_t size) { return mi_source_pvalloc(size MI_SOURCE_RET()); }
void* __libc_memalign(size_t alignment, size_t size) { return mi_source_memalign(alignment, size MI_SOURCE_RET()); }
int __posix_memalign(void** p, size_t alignment, size_t size) { return mi_source_posix_memalign(p, alignment, size MI_SOURCE_RET()); }
#endif
#ifdef __cplusplus

View File

@ -42,7 +42,8 @@ void mi_cfree(void* p) mi_attr_noexcept {
}
}
int mi_posix_memalign(void** p, size_t alignment, size_t size) mi_attr_noexcept {
MI_SOURCE_API3(int, posix_memalign, void**, p, size_t, alignment, size_t, size)
{
// Note: The spec dictates we should not modify `*p` on an error. (issue#27)
// <http://man7.org/linux/man-pages/man3/posix_memalign.3.html>
if (p == NULL) return EINVAL;
@ -50,10 +51,10 @@ int mi_posix_memalign(void** p, size_t alignment, size_t size) mi_attr_noexcept
if (!_mi_is_power_of_two(alignment)) return EINVAL; // not a power of 2
void* q;
if (alignment <= MI_MAX_ALIGN_SIZE) {
q = mi_source_malloc(size MI_SOURCE_RET());
q = mi_source_malloc(size MI_SOURCE_ARG);
}
else {
q = mi_source_malloc_aligned(size, alignment MI_SOURCE_RET());
q = mi_source_malloc_aligned(size, alignment MI_SOURCE_ARG);
}
if (q==NULL && size != 0) return ENOMEM;
mi_assert_internal(((uintptr_t)q % alignment) == 0);
@ -61,45 +62,50 @@ int mi_posix_memalign(void** p, size_t alignment, size_t size) mi_attr_noexcept
return 0;
}
void* mi_memalign(size_t alignment, size_t size) mi_attr_noexcept {
MI_SOURCE_API2(void*, memalign, size_t, alignment, size_t, size)
{
void* p;
if (alignment <= MI_MAX_ALIGN_SIZE) {
p = mi_source_malloc(size MI_SOURCE_RET());
p = mi_source_malloc(size MI_SOURCE_ARG);
}
else {
p = mi_source_malloc_aligned(size, alignment MI_SOURCE_RET());
p = mi_source_malloc_aligned(size, alignment MI_SOURCE_ARG);
}
mi_assert_internal(((uintptr_t)p % alignment) == 0);
return p;
}
void* mi_valloc(size_t size) mi_attr_noexcept {
return mi_source_malloc_aligned(size, _mi_os_page_size() MI_SOURCE_RET());
MI_SOURCE_API1(void*, valloc, size_t, size)
{
return mi_source_malloc_aligned(size, _mi_os_page_size() MI_SOURCE_ARG);
}
void* mi_pvalloc(size_t size) mi_attr_noexcept {
MI_SOURCE_API1(void*, pvalloc, size_t, size)
{
size_t psize = _mi_os_page_size();
if (size >= SIZE_MAX - psize) return NULL; // overflow
size_t asize = ((size + psize - 1) / psize) * psize;
return mi_source_malloc_aligned(asize, psize MI_SOURCE_RET());
return mi_source_malloc_aligned(asize, psize MI_SOURCE_ARG);
}
void* mi_aligned_alloc(size_t alignment, size_t size) mi_attr_noexcept {
MI_SOURCE_API2(void*, aligned_alloc, size_t, alignment, size_t, size)
{
if (alignment==0 || !_mi_is_power_of_two(alignment)) return NULL;
if ((size&(alignment-1)) != 0) return NULL; // C11 requires integral multiple, see <https://en.cppreference.com/w/c/memory/aligned_alloc>
void* p;
if (alignment <= MI_MAX_ALIGN_SIZE) {
p = mi_source_malloc(size MI_SOURCE_RET());
p = mi_source_malloc(size MI_SOURCE_ARG);
}
else {
p = mi_source_malloc_aligned(size, alignment MI_SOURCE_RET());
p = mi_source_malloc_aligned(size, alignment MI_SOURCE_ARG);
}
mi_assert_internal(((uintptr_t)p % alignment) == 0);
return p;
}
void* mi_reallocarray( void* p, size_t count, size_t size ) mi_attr_noexcept { // BSD
void* newp = mi_source_reallocn(p, count, size MI_SOURCE_RET());
MI_SOURCE_API3(void*, reallocarray, void*, p, size_t, count, size_t, size )
{
void* newp = mi_source_reallocn(p, count, size MI_SOURCE_ARG);
if (newp==NULL) errno = ENOMEM;
return newp;
}
@ -110,23 +116,26 @@ void* mi__expand(void* p, size_t newsize) mi_attr_noexcept { // Microsoft
return res;
}
unsigned short* mi_wcsdup(const unsigned short* s) mi_attr_noexcept {
MI_SOURCE_API1(unsigned short*, wcsdup, const unsigned short*, s)
{
if (s==NULL) return NULL;
size_t len;
for(len = 0; s[len] != 0; len++) { }
size_t size = (len+1)*sizeof(unsigned short);
unsigned short* p = (unsigned short*)mi_source_malloc(size MI_SOURCE_RET());
unsigned short* p = (unsigned short*)mi_source_malloc(size MI_SOURCE_ARG);
if (p != NULL) {
memcpy(p,s,size);
}
return p;
}
unsigned char* mi_mbsdup(const unsigned char* s) mi_attr_noexcept {
return (unsigned char*)mi_source_strdup((const char*)s MI_SOURCE_RET());
MI_SOURCE_API1(unsigned char*, mbsdup, const unsigned char*, s)
{
return (unsigned char*)mi_source_strdup((const char*)s MI_SOURCE_ARG);
}
int mi_dupenv_s(char** buf, size_t* size, const char* name) mi_attr_noexcept {
MI_SOURCE_API3(int, dupenv_s, char**, buf, size_t*, size, const char*, name)
{
if (buf==NULL || name==NULL) return EINVAL;
if (size != NULL) *size = 0;
#pragma warning(suppress:4996)
@ -135,18 +144,22 @@ int mi_dupenv_s(char** buf, size_t* size, const char* name) mi_attr_noexcept {
*buf = NULL;
}
else {
*buf = mi_source_strdup(p MI_SOURCE_RET());
*buf = mi_source_strdup(p MI_SOURCE_ARG);
if (*buf==NULL) return ENOMEM;
if (size != NULL) *size = strlen(p);
}
return 0;
}
int mi_wdupenv_s(unsigned short** buf, size_t* size, const unsigned short* name) mi_attr_noexcept {
MI_SOURCE_API3(int, wdupenv_s, unsigned short**, buf, size_t*, size, const unsigned short*, name)
{
if (buf==NULL || name==NULL) return EINVAL;
if (size != NULL) *size = 0;
#if !defined(_WIN32) || (defined(WINAPI_FAMILY) && (WINAPI_FAMILY != WINAPI_FAMILY_DESKTOP_APP))
// not supported
#ifndef NDEBUG
UNUSED(__mi_source);
#endif
*buf = NULL;
return EINVAL;
#else
@ -156,7 +169,7 @@ int mi_wdupenv_s(unsigned short** buf, size_t* size, const unsigned short* name)
*buf = NULL;
}
else {
*buf = mi_wcsdup(p);
*buf = mi_source_wcsdup(p MI_SOURCE_ARG);
if (*buf==NULL) return ENOMEM;
if (size != NULL) *size = wcslen((const wchar_t*)p);
}

View File

@ -15,6 +15,7 @@ terms of the MIT license. A copy of the license can be found in the file
#include "alloc-override.c"
#undef MI_IN_ALLOC_C
// ------------------------------------------------------
// Allocation
// ------------------------------------------------------
@ -59,7 +60,8 @@ extern inline void* _mi_page_malloc(mi_heap_t* heap, mi_page_t* page, size_t siz
}
// allocate a small block
static inline mi_decl_allocator void* mi_heapx_malloc_small(mi_heap_t* heap, size_t size MI_SOURCE_PARAM) mi_attr_noexcept {
MI_ALLOC_API1(void*,malloc_small, mi_heap_t*,heap, size_t,size)
{
mi_assert(heap!=NULL);
mi_assert(heap->thread_id == 0 || heap->thread_id == _mi_thread_id()); // heaps are thread local
mi_assert(size <= MI_SMALL_SIZE_MAX);
@ -75,18 +77,12 @@ static inline mi_decl_allocator void* mi_heapx_malloc_small(mi_heap_t* heap, siz
return p;
}
extern inline mi_decl_allocator void* mi_heap_malloc_small(mi_heap_t* heap, size_t size) mi_attr_noexcept {
return mi_heapx_malloc_small(heap, size MI_SOURCE_RET());
}
extern inline mi_decl_allocator void* mi_malloc_small(size_t size) mi_attr_noexcept {
return mi_heapx_malloc_small(mi_get_default_heap(), size MI_SOURCE_RET());
}
// The main allocation function
inline mi_decl_allocator void* _mi_heapx_malloc(mi_heap_t* heap, size_t size MI_SOURCE_PARAM) mi_attr_noexcept {
MI_ALLOC_API1(void*, malloc, mi_heap_t*,heap, size_t,size)
{
if (mi_likely(size <= MI_SMALL_SIZE_MAX)) {
return mi_heapx_malloc_small(heap, size MI_SOURCE_ARG);
return mi_heap_source_malloc_small(heap, size MI_SOURCE_ARG);
}
else {
mi_assert(heap!=NULL);
@ -103,16 +99,6 @@ inline mi_decl_allocator void* _mi_heapx_malloc(mi_heap_t* heap, size_t size MI_
}
}
extern inline mi_decl_allocator void* mi_heap_malloc(mi_heap_t* heap, size_t size) mi_attr_noexcept {
return _mi_heapx_malloc(heap, size MI_SOURCE_RET());
}
extern inline mi_decl_allocator void* mi_source_malloc(size_t size MI_SOURCE_PARAM) mi_attr_noexcept {
return _mi_heapx_malloc(mi_get_default_heap(), size MI_SOURCE_ARG);
}
extern inline mi_decl_allocator void* mi_malloc(size_t size) mi_attr_noexcept {
return mi_source_malloc(size MI_SOURCE_RET());
}
void _mi_block_zero_init(const mi_page_t* page, void* p, size_t size) {
// note: we need to initialize the whole usable block size to zero, not just the requested size,
@ -133,32 +119,26 @@ void _mi_block_zero_init(const mi_page_t* page, void* p, size_t size) {
}
// zero initialized small block
mi_decl_allocator void* mi_zalloc_small(size_t size) mi_attr_noexcept {
void* p = mi_heapx_malloc_small(mi_get_default_heap(), size MI_SOURCE_RET());
MI_ALLOC_API1(void*, zalloc_small, mi_heap_t*, heap, size_t, size)
{
void* p = mi_heap_source_malloc_small(heap, size MI_SOURCE_ARG);
if (p != NULL) {
_mi_block_zero_init(_mi_ptr_page(p), p, size); // todo: can we avoid getting the page again?
}
return p;
}
mi_decl_allocator void* _mi_heapx_malloc_zero(mi_heap_t* heap, size_t size, bool zero MI_SOURCE_PARAM) {
void* p = _mi_heapx_malloc(heap,size MI_SOURCE_ARG);
mi_decl_allocator void* _mi_heap_source_malloc_zero(mi_heap_t* heap, size_t size, bool zero MI_SOURCE_PARAM) {
void* p = mi_heap_source_malloc(heap,size MI_SOURCE_ARG);
if (zero && p != NULL) {
_mi_block_zero_init(_mi_ptr_page(p),p,size); // todo: can we avoid getting the page again?
}
return p;
}
static inline mi_decl_allocator void* mi_heapx_zalloc(mi_heap_t* heap, size_t size MI_SOURCE_PARAM) mi_attr_noexcept {
return _mi_heapx_malloc_zero(heap, size, true MI_SOURCE_ARG);
}
extern inline mi_decl_allocator void* mi_heap_zalloc(mi_heap_t* heap, size_t size) mi_attr_noexcept {
return mi_heapx_zalloc(heap, size MI_SOURCE_RET());
}
mi_decl_allocator void* mi_zalloc(size_t size) mi_attr_noexcept {
return mi_heapx_zalloc(mi_get_default_heap(), size MI_SOURCE_RET());
MI_ALLOC_API1(void*, zalloc, mi_heap_t*,heap, size_t,size)
{
return _mi_heap_source_malloc_zero(heap, size, true MI_SOURCE_ARG);
}
@ -538,36 +518,21 @@ void mi_free_aligned(void* p, size_t alignment) mi_attr_noexcept {
mi_free(p);
}
static inline mi_decl_allocator void* mi_heapx_calloc(mi_heap_t* heap, size_t count, size_t size MI_SOURCE_PARAM) mi_attr_noexcept {
MI_ALLOC_API2(void*, calloc, mi_heap_t*,heap, size_t,count, size_t,size)
{
size_t total;
if (mi_count_size_overflow(count,size,&total)) return NULL;
return mi_heapx_zalloc(heap, total MI_SOURCE_ARG);
}
mi_decl_allocator void* mi_heap_calloc(mi_heap_t* heap, size_t count, size_t size) mi_attr_noexcept {
return mi_heapx_calloc(heap, count, size MI_SOURCE_RET());
}
mi_decl_allocator void* mi_source_calloc(size_t count, size_t size MI_SOURCE_PARAM) mi_attr_noexcept {
return mi_heapx_calloc(mi_get_default_heap(), count, size MI_SOURCE_ARG);
}
mi_decl_allocator void* mi_calloc(size_t count, size_t size) mi_attr_noexcept {
return mi_source_calloc(count, size MI_SOURCE_RET());
return mi_heap_source_zalloc(heap, total MI_SOURCE_ARG);
}
// Uninitialized `calloc`
static inline mi_decl_allocator void* mi_heapx_mallocn(mi_heap_t* heap, size_t count, size_t size MI_SOURCE_PARAM) mi_attr_noexcept {
MI_ALLOC_API2(void*, mallocn, mi_heap_t*,heap, size_t,count, size_t,size)
{
size_t total;
if (mi_count_size_overflow(count, size, &total)) return NULL;
return _mi_heapx_malloc(heap, total MI_SOURCE_ARG);
return mi_heap_source_malloc(heap, total MI_SOURCE_ARG);
}
extern mi_decl_allocator void* mi_heap_mallocn(mi_heap_t* heap, size_t count, size_t size) mi_attr_noexcept {
return mi_heapx_mallocn(heap, count, size MI_SOURCE_RET());
}
mi_decl_allocator void* mi_mallocn(size_t count, size_t size) mi_attr_noexcept {
return mi_heapx_mallocn(mi_get_default_heap(), count, size MI_SOURCE_RET());
}
// Expand in place or fail
mi_decl_allocator void* mi_expand(void* p, size_t newsize) mi_attr_noexcept {
@ -577,13 +542,13 @@ mi_decl_allocator void* mi_expand(void* p, size_t newsize) mi_attr_noexcept {
return p; // it fits
}
mi_decl_allocator void* _mi_heapx_realloc_zero(mi_heap_t* heap, void* p, size_t newsize, bool zero MI_SOURCE_PARAM) {
if (p == NULL) return _mi_heapx_malloc_zero(heap,newsize,zero MI_SOURCE_ARG);
mi_decl_allocator void* _mi_heap_source_realloc_zero(mi_heap_t* heap, void* p, size_t newsize, bool zero MI_SOURCE_PARAM) {
if (p == NULL) return _mi_heap_source_malloc_zero(heap,newsize,zero MI_SOURCE_ARG);
size_t size = mi_usable_size(p);
if (newsize <= size && newsize >= (size / 2)) {
return p; // reallocation still fits and not more than 50% waste
}
void* newp = _mi_heapx_malloc(heap,newsize MI_SOURCE_ARG);
void* newp = mi_heap_source_malloc(heap,newsize MI_SOURCE_ARG);
if (mi_likely(newp != NULL)) {
if (zero && newsize > size) {
// also set last word in the previous allocation to zero to ensure any padding is zero-initialized
@ -596,71 +561,36 @@ mi_decl_allocator void* _mi_heapx_realloc_zero(mi_heap_t* heap, void* p, size_t
return newp;
}
static mi_decl_allocator void* mi_heapx_realloc(mi_heap_t* heap, void* p, size_t newsize MI_SOURCE_PARAM) mi_attr_noexcept {
return _mi_heapx_realloc_zero(heap, p, newsize, false MI_SOURCE_ARG);
MI_ALLOC_API2(void*, realloc, mi_heap_t*,heap, void*,p, size_t,newsize)
{
return _mi_heap_source_realloc_zero(heap, p, newsize, false MI_SOURCE_ARG);
}
mi_decl_allocator void* mi_heap_realloc(mi_heap_t* heap, void* p, size_t newsize) mi_attr_noexcept {
return mi_heapx_realloc(heap, p, newsize MI_SOURCE_RET());
}
mi_decl_allocator void* _mi_heapx_reallocn(mi_heap_t* heap, void* p, size_t count, size_t size MI_SOURCE_PARAM) mi_attr_noexcept {
MI_ALLOC_API3(void*, reallocn, mi_heap_t*,heap, void*,p, size_t,count, size_t,size)
{
size_t total;
if (mi_count_size_overflow(count, size, &total)) return NULL;
return mi_heapx_realloc(heap, p, total MI_SOURCE_ARG);
return mi_heap_source_realloc(heap, p, total MI_SOURCE_ARG);
}
mi_decl_allocator void* mi_heap_reallocn(mi_heap_t* heap, void* p, size_t count, size_t size) mi_attr_noexcept {
return _mi_heapx_reallocn(heap, p, count, size MI_SOURCE_RET());
}
// Reallocate but free `p` on errors
mi_decl_allocator void* mi_heap_reallocf(mi_heap_t* heap, void* p, size_t newsize) mi_attr_noexcept {
void* newp = mi_heapx_realloc(heap, p, newsize MI_SOURCE_RET());
MI_ALLOC_API2(void*, reallocf, mi_heap_t*, heap, void*, p, size_t, newsize)
{
void* newp = mi_heap_source_realloc(heap, p, newsize MI_SOURCE_ARG);
if (newp==NULL && p!=NULL) mi_free(p);
return newp;
}
mi_decl_allocator void* mi_heap_rezalloc(mi_heap_t* heap, void* p, size_t newsize) mi_attr_noexcept {
return _mi_heapx_realloc_zero(heap, p, newsize, true MI_SOURCE_RET());
MI_ALLOC_API2(void*, rezalloc, mi_heap_t*, heap, void*, p, size_t, newsize)
{
return _mi_heap_source_realloc_zero(heap, p, newsize, true MI_SOURCE_ARG);
}
mi_decl_allocator void* mi_heap_recalloc(mi_heap_t* heap, void* p, size_t count, size_t size) mi_attr_noexcept {
MI_ALLOC_API3(void*, recalloc, mi_heap_t*, heap, void*, p, size_t, count, size_t, size)
{
size_t total;
if (mi_count_size_overflow(count, size, &total)) return NULL;
return _mi_heapx_realloc_zero(heap, p, total, true MI_SOURCE_RET());
}
mi_decl_allocator void* mi_source_realloc(void* p, size_t newsize MI_SOURCE_PARAM) mi_attr_noexcept {
return mi_heapx_realloc(mi_get_default_heap(), p, newsize MI_SOURCE_ARG);
}
mi_decl_allocator void* mi_realloc(void* p, size_t newsize) mi_attr_noexcept {
return mi_source_realloc(p, newsize MI_SOURCE_RET());
}
mi_decl_allocator void* mi_source_reallocn(void* p, size_t count, size_t size MI_SOURCE_PARAM) mi_attr_noexcept {
return _mi_heapx_reallocn(mi_get_default_heap(), p, count, size MI_SOURCE_ARG);
}
mi_decl_allocator void* mi_reallocn(void* p, size_t count, size_t size) mi_attr_noexcept {
return mi_source_reallocn(p,count,size MI_SOURCE_RET());
}
// Reallocate but free `p` on errors
mi_decl_allocator void* mi_reallocf(void* p, size_t newsize) mi_attr_noexcept {
void* newp = mi_heapx_realloc(mi_get_default_heap(), p, newsize MI_SOURCE_RET());
if (newp==NULL && p!=NULL) mi_free(p);
return newp;
}
mi_decl_allocator void* mi_rezalloc(void* p, size_t newsize) mi_attr_noexcept {
return _mi_heapx_realloc_zero(mi_get_default_heap(), p, newsize, true MI_SOURCE_RET());
}
mi_decl_allocator void* mi_recalloc(void* p, size_t count, size_t size) mi_attr_noexcept {
size_t total;
if (mi_count_size_overflow(count, size, &total)) return NULL;
return _mi_heapx_realloc_zero(mi_get_default_heap(), p, total, true MI_SOURCE_RET());
return _mi_heap_source_realloc_zero(heap, p, total, true MI_SOURCE_ARG);
}
@ -670,45 +600,29 @@ mi_decl_allocator void* mi_recalloc(void* p, size_t count, size_t size) mi_attr_
// ------------------------------------------------------
// `strdup` using mi_malloc
static char* mi_heapx_strdup(mi_heap_t* heap, const char* s MI_SOURCE_PARAM) mi_attr_noexcept {
MI_ALLOC_API1(char*, strdup, mi_heap_t*,heap, const char*,s)
{
if (s == NULL) return NULL;
size_t n = strlen(s);
char* t = (char*)_mi_heapx_malloc(heap, n+1 MI_SOURCE_ARG);
char* t = (char*)mi_heap_source_malloc(heap, n+1 MI_SOURCE_ARG);
if (t != NULL) memcpy(t, s, n + 1);
return t;
}
char* mi_heap_strdup(mi_heap_t* heap, const char* s) mi_attr_noexcept {
return mi_heapx_strdup(heap, s MI_SOURCE_RET());
}
char* mi_source_strdup(const char* s MI_SOURCE_PARAM) mi_attr_noexcept {
return mi_heapx_strdup(mi_get_default_heap(), s MI_SOURCE_ARG);
}
char* mi_strdup(const char* s) mi_attr_noexcept {
return mi_source_strdup(s MI_SOURCE_RET());
}
// `strndup` using mi_malloc
static char* mi_heapx_strndup(mi_heap_t* heap, const char* s, size_t n MI_SOURCE_PARAM) mi_attr_noexcept {
MI_ALLOC_API2(char*, strndup, mi_heap_t*, heap, const char*, s, size_t, n)
{
if (s == NULL) return NULL;
size_t m = strlen(s);
if (n > m) n = m;
char* t = (char*)_mi_heapx_malloc(heap, n+1 MI_SOURCE_ARG);
char* t = (char*)mi_heap_source_malloc(heap, n+1 MI_SOURCE_ARG);
if (t == NULL) return NULL;
memcpy(t, s, n);
t[n] = 0;
return t;
}
char* mi_heap_strndup(mi_heap_t* heap, const char* s, size_t n) mi_attr_noexcept {
return mi_heapx_strndup(heap, s, n MI_SOURCE_RET());
}
char* mi_source_strndup(const char* s, size_t n MI_SOURCE_PARAM) mi_attr_noexcept {
return mi_heapx_strndup(mi_get_default_heap(), s, n MI_SOURCE_ARG);
}
char* mi_strndup(const char* s, size_t n) mi_attr_noexcept {
return mi_source_strndup(s, n MI_SOURCE_RET());
}
#ifndef __wasi__
// `realpath` using mi_malloc
@ -717,7 +631,8 @@ char* mi_strndup(const char* s, size_t n) mi_attr_noexcept {
#define PATH_MAX MAX_PATH
#endif
#include <windows.h>
static char* mi_heapx_realpath(mi_heap_t* heap, const char* fname, char* resolved_name MI_SOURCE_PARAM) mi_attr_noexcept {
MI_ALLOC_API2(char*, realpath, mi_heap_t*, heap, const char*, fname, char*, resolved_name)
{
// todo: use GetFullPathNameW to allow longer file names
char buf[PATH_MAX];
DWORD res = GetFullPathNameA(fname, PATH_MAX, (resolved_name == NULL ? buf : resolved_name), NULL);
@ -731,7 +646,7 @@ static char* mi_heapx_realpath(mi_heap_t* heap, const char* fname, char* resolve
return resolved_name;
}
else {
return mi_heapx_strndup(heap, buf, PATH_MAX MI_SOURCE_ARG);
return mi_heap_source_strndup(heap, buf, PATH_MAX MI_SOURCE_ARG);
}
}
#else
@ -747,7 +662,8 @@ static size_t mi_path_max() {
return path_max;
}
static char* mi_heapx_realpath(mi_heap_t* heap, const char* fname, char* resolved_name MI_SOURCE_PARAM) mi_attr_noexcept {
MI_ALLOC_API2(char*, realpath, mi_heap_t*, heap, const char*, fname, char*, resolved_name)
{
if (resolved_name != NULL) {
return realpath(fname,resolved_name);
}
@ -756,22 +672,13 @@ static char* mi_heapx_realpath(mi_heap_t* heap, const char* fname, char* resolve
char* buf = (char*)mi_malloc(n+1);
if (buf==NULL) return NULL;
char* rname = realpath(fname,buf);
char* result = mi_heapx_strndup(heap, rname, n MI_SOURCE_ARG); // ok if `rname==NULL`
char* result = mi_heap_source_strndup(heap, rname, n MI_SOURCE_ARG); // ok if `rname==NULL`
mi_free(buf);
return result;
}
}
#endif
char* mi_heap_realpath(mi_heap_t* heap, const char* fname, char* resolved_name) mi_attr_noexcept {
return mi_heapx_realpath(heap, fname, resolved_name MI_SOURCE_RET());
}
char* mi_source_realpath(const char* fname, char* resolved_name MI_SOURCE_PARAM) mi_attr_noexcept {
return mi_heapx_realpath(mi_get_default_heap(), fname, resolved_name MI_SOURCE_ARG);
}
char* mi_realpath(const char* fname, char* resolved_name) mi_attr_noexcept {
return mi_heapx_realpath(mi_get_default_heap(), fname, resolved_name MI_SOURCE_RET());
}
#endif
/*-------------------------------------------------------
@ -834,28 +741,28 @@ static mi_decl_noinline void* mi_source_try_new(size_t size, bool nothrow MI_S
return p;
}
inline void* mi_source_new(size_t size MI_SOURCE_PARAM) {
inline mi_decl_allocator void* mi_source_new(size_t size MI_SOURCE_PARAM) {
void* p = mi_source_malloc(size MI_SOURCE_ARG);
if (mi_unlikely(p == NULL)) return mi_source_try_new(size, false MI_SOURCE_ARG);
return p;
}
void* mi_new(size_t size) {
mi_decl_allocator void* mi_new(size_t size) {
return mi_source_new(size MI_SOURCE_RET());
}
void* mi_source_new_nothrow(size_t size MI_SOURCE_PARAM) {
mi_decl_allocator void* mi_source_new_nothrow(size_t size MI_SOURCE_PARAM) {
void* p = mi_source_malloc(size MI_SOURCE_ARG);
if (mi_unlikely(p == NULL)) return mi_source_try_new(size, true MI_SOURCE_ARG);
return p;
}
void* mi_new_nothrow(size_t size) {
mi_decl_allocator void* mi_new_nothrow(size_t size) {
return mi_source_new_nothrow(size MI_SOURCE_RET());
}
void* mi_source_new_aligned(size_t size, size_t alignment MI_SOURCE_PARAM) {
mi_decl_allocator void* mi_source_new_aligned(size_t size, size_t alignment MI_SOURCE_PARAM) {
void* p;
do {
p = mi_source_malloc_aligned(size, alignment MI_SOURCE_ARG);
@ -864,11 +771,11 @@ void* mi_source_new_aligned(size_t size, size_t alignment MI_SOURCE_PARAM) {
return p;
}
void* mi_new_aligned(size_t size, size_t alignment) {
mi_decl_allocator void* mi_new_aligned(size_t size, size_t alignment) {
return mi_source_new_aligned(size, alignment MI_SOURCE_RET());
}
void* mi_source_new_aligned_nothrow(size_t size, size_t alignment MI_SOURCE_PARAM) {
mi_decl_allocator void* mi_source_new_aligned_nothrow(size_t size, size_t alignment MI_SOURCE_PARAM) {
void* p;
do {
p = mi_source_malloc_aligned(size, alignment MI_SOURCE_ARG);
@ -877,33 +784,39 @@ void* mi_source_new_aligned_nothrow(size_t size, size_t alignment MI_SOURCE_PAR
return p;
}
void* mi_new_aligned_nothrow(size_t size, size_t alignment) {
mi_decl_allocator void* mi_new_aligned_nothrow(size_t size, size_t alignment) {
return mi_source_new_aligned_nothrow(size, alignment MI_SOURCE_RET());
}
void* mi_new_n(size_t count, size_t size) {
mi_decl_allocator void* mi_source_new_n(size_t count, size_t size MI_SOURCE_PARAM) {
size_t total;
if (mi_unlikely(mi_count_size_overflow(count, size, &total))) {
mi_try_new_handler(false); // on overflow we invoke the try_new_handler once to potentially throw std::bad_alloc
return NULL;
}
else {
return mi_source_new(total MI_SOURCE_RET());
return mi_source_new(total MI_SOURCE_ARG);
}
}
void* mi_new_realloc(void* p, size_t newsize) {
mi_heap_t* const heap = mi_get_default_heap();
mi_decl_allocator void* mi_new_n(size_t count, size_t size) {
return mi_source_new_n(count, size MI_SOURCE_RET());
}
mi_decl_allocator void* mi_source_new_realloc(void* p, size_t newsize MI_SOURCE_PARAM) {
void* q;
do {
q = mi_heapx_realloc(heap, p, newsize MI_SOURCE_RET());
q = mi_source_realloc(p, newsize MI_SOURCE_ARG);
} while (q == NULL && mi_try_new_handler(false));
return q;
}
void* mi_new_reallocn(void* p, size_t newcount, size_t size) {
mi_heap_t* const heap = mi_get_default_heap();
mi_decl_allocator void* mi_new_realloc(void* p, size_t newsize) {
return mi_source_new_realloc(p, newsize MI_SOURCE_RET());
}
mi_decl_allocator void* mi_source_new_reallocn(void* p, size_t newcount, size_t size MI_SOURCE_PARAM) {
size_t total;
if (mi_unlikely(mi_count_size_overflow(newcount, size, &total))) {
mi_try_new_handler(false); // on overflow we invoke the try_new_handler once to potentially throw std::bad_alloc
@ -911,7 +824,11 @@ void* mi_new_reallocn(void* p, size_t newcount, size_t size) {
}
void* q;
do {
q = mi_heapx_realloc(heap, p, total MI_SOURCE_RET());
q = mi_source_realloc(p, total MI_SOURCE_ARG);
} while (q == NULL && mi_try_new_handler(false));
return q;
}
mi_decl_allocator void* mi_new_reallocn(void* p, size_t newcount, size_t size) {
return mi_source_new_reallocn(p, newcount, size MI_SOURCE_RET());
}

View File

@ -6,9 +6,9 @@
#include <mimalloc-new-delete.h>
#include <mimalloc.h>
#include <mimalloc-override.h>
#include <new>
#include <vector>
// #include <mimalloc-override.h>
static void* p = malloc(8);
@ -30,12 +30,12 @@ void dangling_ptr_write();
int main() {
mi_stats_reset(); // ignore earlier allocations
atexit(free_p);
dangling_ptr_write();
//dangling_ptr_write();
void* p1 = malloc(78);
void* p2 = mi_malloc_aligned(16,24);
free(p1);
p1 = malloc(8);
char* s = mi_strdup("hello\n");
char* s = _strdup("hello\n");
/*
char* s = _strdup("hello\n");
char* buf = NULL;
@ -48,10 +48,11 @@ int main() {
p1 = realloc(p1, 32);
free(p1);
free(p2);
mi_free(s);
mi_free(s);
Test* t = new Test(42);
delete t;
t = new (std::nothrow) Test(42);
// t = new(std::nothrow) Test(42); // does not work with overriding :-(
t = new Test(42);
delete t;
mi_stats_print(NULL);
return 0;
@ -60,7 +61,7 @@ int main() {
static void dangling_ptr_write() {
for (int i = 0; i < 1000; i++) {
uint8_t* p;
if ((i & 1) == 0) {
if ((i & 1) == 1) {
p = (uint8_t*)malloc(16);
free(p);
}