Skip to content

Commit

Permalink
follow-up reallocarr(ay) BSD implementations,
Browse files Browse the repository at this point in the history
here OpenBSD malloc_conceal api. not much different of classic
malloc/calloc but content not to be dumped in core files.
  • Loading branch information
devnexen committed Jan 8, 2022
1 parent 43e5cd2 commit d89a630
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
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
6 changes: 6 additions & 0 deletions test/test-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ int main(void) {
CHECK_BODY("calloc0",{
result = (mi_usable_size(mi_calloc(0,1000)) <= 16);
});
CHECK_BODY("malloc_conceal",{
char* p = (char *)mi_malloc(24);
p[0] = 1;
mi_freezero(p, 24);
result = p[0] == 0;
});

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

0 comments on commit d89a630

Please sign in to comment.