Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

follow-up reallocarr(ay) BSD implementations, #519

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/mimalloc-override.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ not accidentally mix pointers from different allocators).
#define pvalloc(n) mi_pvalloc(n)
#define reallocarray(p,s,n) mi_reallocarray(p,s,n)
#define reallocarr(p,s,n) mi_reallocarr(p,s,n)
#define malloc_conceal(n) mi_malloc_conceal(n)
#define calloc_conceal(n,c) mi_calloc_conceal(n,c)
#define freezero(p,n) mi_freezero(p,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)
Expand Down
3 changes: 3 additions & 0 deletions include/mimalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ mi_decl_nodiscard mi_decl_export mi_decl_restrict void* mi_aligned_alloc(size_t

mi_decl_nodiscard mi_decl_export void* mi_reallocarray(void* p, size_t count, size_t size) mi_attr_noexcept mi_attr_alloc_size2(2,3);
mi_decl_nodiscard mi_decl_export int mi_reallocarr(void* p, size_t count, size_t size) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export void* mi_malloc_conceal(size_t size) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export void* mi_calloc_conceal(size_t count, size_t size) mi_attr_noexcept;
mi_decl_export void mi_freezero(void* p, size_t size) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export void* mi_aligned_recalloc(void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept;
mi_decl_nodiscard mi_decl_export void* mi_aligned_offset_recalloc(void* p, size_t newcount, size_t size, size_t alignment, size_t offset) mi_attr_noexcept;

Expand Down
3 changes: 3 additions & 0 deletions src/alloc-override.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ void* reallocarray(void* p, size_t count, size_t size) { return mi_reallocarray
int reallocarr(void* p, size_t count, size_t size) { return mi_reallocarr(p, count, size); }
void* memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
void* malloc_conceal(size_t size) { return mi_malloc_conceal(size); }
void* calloc_conceal(size_t count, size_t size) { return mi_calloc_conceal(count, size); }
void freezero(void* p, size_t size) { mi_freezero(p, size); }

#if defined(__GLIBC__) && defined(__linux__)
// forward __libc interface (needed for glibc-based Linux distributions)
Expand Down
43 changes: 43 additions & 0 deletions src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,49 @@ extern inline mi_decl_restrict void* mi_malloc(size_t size) mi_attr_noexcept {
return mi_heap_malloc(mi_get_default_heap(), size);
}

mi_decl_restrict void* mi_malloc_conceal(size_t size) mi_attr_noexcept {
void* p;
p = mi_heap_malloc(mi_get_default_heap(), size);
if (mi_likely(p != NULL)) {
#if defined(MADV_DONTDUMP)
madvise(p, size, MADV_DONTDUMP);
#elif defined(MADV_NOCORE)
madvise(p, size, MADV_NOCORE);
#endif
}
return p;
}

mi_decl_restrict void* mi_calloc_conceal(size_t count, size_t size) mi_attr_noexcept {
void* p;
p = mi_heap_calloc(mi_get_default_heap(),count,size);
if (mi_likely(p != NULL)) {
#if defined(MADV_DONTDUMP)
madvise(p, size, MADV_DONTDUMP);
#elif defined(MADV_NOCORE)
madvise(p, size, MADV_NOCORE);
#endif
}
return p;
}

void mi_freezero(void* p, size_t size) mi_attr_noexcept {
mi_free(p);
#if (MI_DEBUG==0)
if (size > 0) {
#if defined(_MSC_VER)
SecureZeroMemory(p, size);
#else
// reusing memset return value and using memory fence
// so memset call is generated regardless of the optimisation level
p = memset(p, 0, size);
__asm__ volatile("" :: "r"(p) : "memory");
#endif
}
#else
MI_UNUSED(size);
#endif
}

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,
Expand Down
8 changes: 8 additions & 0 deletions test/test-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ int main(void) {
CHECK_BODY("calloc0",{
result = (mi_usable_size(mi_calloc(0,1000)) <= 16);
});
#if (MI_DEBUG==0)
CHECK_BODY("malloc_conceal",{
char* p = (char *)mi_malloc(24);
p[0] = 1;
mi_freezero(p, 24);
result = p[0] == 0;
});
#endif

// ---------------------------------------------------
// Extended
Expand Down