Skip to content

Commit 1ea4aee

Browse files
committed
__builtin_memcpy/set_inline from clang usage introduction.
no extra calls guaranteed during LLVM IR generation but works only on constants unlike their non inline counterparts.
1 parent eb29d6b commit 1ea4aee

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

Diff for: include/mimalloc-internal.h

+25
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,17 @@ bool _mi_page_is_valid(mi_page_t* page);
168168
#define __has_builtin(x) 0
169169
#endif
170170

171+
#if __has_builtin(__builtin_memcpy_inline)
172+
#define _mi_memcpy_inline(x, y, s) __builtin_memcpy_inline(x, y, s)
173+
#else
174+
#define _mi_memcpy_inline(x, y, s) do { _Static_assert(__builtin_choose_expr(__builtin_constant_p(s), 1, 0), "`_mi_memcpy_inline` must be a constant integer"); memcpy(x, y, s); } while (0)
175+
#endif
176+
177+
#if __has_builtin(__builtin_memset_inline)
178+
#define _mi_memset_inline(x, y, s) __builtin_memset_inline(x, y, s)
179+
#else
180+
#define _mi_memset_inline(x, y, s) do { _Static_assert(__builtin_choose_expr(__builtin_constant_p(s), 1, 0), "`_mi_memset_inline` must be a constant integer"); memset(x, y, s); } while (0)
181+
#endif
171182

172183
/* -----------------------------------------------------------
173184
Error codes passed to `_mi_fatal_error`
@@ -975,6 +986,17 @@ static inline void _mi_memzero_aligned(void* dst, size_t n) {
975986
void* adst = __builtin_assume_aligned(dst, MI_INTPTR_SIZE);
976987
_mi_memzero(adst, n);
977988
}
989+
990+
#define _mi_memcpy_inline_aligned(dst, src, n) \
991+
mi_assert_internal(((uintptr_t)dst % MI_INTPTR_SIZE == 0) && ((uintptr_t)src % MI_INTPTR_SIZE == 0)); \
992+
void* adst = __builtin_assume_aligned(dst, MI_INTPTR_SIZE); \
993+
const void* asrc = __builtin_assume_aligned(src, MI_INTPTR_SIZE); \
994+
_mi_memcpy_inline(adst, asrc, n)
995+
996+
#define _mi_memzero_inline_aligned(dst, n) \
997+
mi_assert_internal((uintptr_t)dst % MI_INTPTR_SIZE == 0); \
998+
void* adst = __builtin_assume_aligned(dst, MI_INTPTR_SIZE); \
999+
_mi_memzero_inline(adst, n)
9781000
#else
9791001
// Default fallback on `_mi_memcpy`
9801002
static inline void _mi_memcpy_aligned(void* dst, const void* src, size_t n) {
@@ -986,6 +1008,9 @@ static inline void _mi_memzero_aligned(void* dst, size_t n) {
9861008
mi_assert_internal((uintptr_t)dst % MI_INTPTR_SIZE == 0);
9871009
_mi_memzero(dst, n);
9881010
}
1011+
1012+
#define _mi_memcpy_inline_aligned(dst, src, n) _mi_memcpy_aligned(dst, src, n)
1013+
#define _mi_memzero_inline_aligned(dst, n) _mi_memzero_aligned(dst, n)
9891014
#endif
9901015

9911016

Diff for: src/heap.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ mi_decl_nodiscard mi_heap_t* mi_heap_new(void) {
193193
mi_heap_t* bheap = mi_heap_get_backing();
194194
mi_heap_t* heap = mi_heap_malloc_tp(bheap, mi_heap_t); // todo: OS allocate in secure mode?
195195
if (heap==NULL) return NULL;
196-
_mi_memcpy_aligned(heap, &_mi_heap_empty, sizeof(mi_heap_t));
196+
_mi_memcpy_inline_aligned(heap, &_mi_heap_empty, sizeof(mi_heap_t));
197197
heap->tld = bheap->tld;
198198
heap->thread_id = _mi_thread_id();
199199
_mi_random_split(&bheap->random, &heap->random);
@@ -220,7 +220,7 @@ static void mi_heap_reset_pages(mi_heap_t* heap) {
220220
#ifdef MI_MEDIUM_DIRECT
221221
memset(&heap->pages_free_medium, 0, sizeof(heap->pages_free_medium));
222222
#endif
223-
_mi_memcpy_aligned(&heap->pages, &_mi_heap_empty.pages, sizeof(heap->pages));
223+
_mi_memcpy_inline_aligned(&heap->pages, &_mi_heap_empty.pages, sizeof(heap->pages));
224224
heap->thread_delayed_free = NULL;
225225
heap->page_count = 0;
226226
}

0 commit comments

Comments
 (0)