Skip to content

Commit 167cc14

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 167cc14

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

Diff for: include/mimalloc-internal.h

+29
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,21 @@ 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+
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
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+
#else
176+
#define _mi_memcpy_inline(x, y, s) memcpy(x, y, s)
177+
#endif
178+
179+
#if __has_builtin(__builtin_memset_inline)
180+
#define _mi_memset_inline(x, y, s) __builtin_memset_inline(x, y, s)
181+
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
182+
#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)
183+
#else
184+
#define _mi_memset_inline(x, y, s) memset(x, y, s)
185+
#endif
171186

172187
/* -----------------------------------------------------------
173188
Error codes passed to `_mi_fatal_error`
@@ -975,6 +990,17 @@ static inline void _mi_memzero_aligned(void* dst, size_t n) {
975990
void* adst = __builtin_assume_aligned(dst, MI_INTPTR_SIZE);
976991
_mi_memzero(adst, n);
977992
}
993+
994+
#define _mi_memcpy_inline_aligned(dst, src, n) \
995+
mi_assert_internal(((uintptr_t)dst % MI_INTPTR_SIZE == 0) && ((uintptr_t)src % MI_INTPTR_SIZE == 0)); \
996+
void* adst = __builtin_assume_aligned(dst, MI_INTPTR_SIZE); \
997+
const void* asrc = __builtin_assume_aligned(src, MI_INTPTR_SIZE); \
998+
_mi_memcpy_inline(adst, asrc, n)
999+
1000+
#define _mi_memzero_inline_aligned(dst, n) \
1001+
mi_assert_internal((uintptr_t)dst % MI_INTPTR_SIZE == 0); \
1002+
void* adst = __builtin_assume_aligned(dst, MI_INTPTR_SIZE); \
1003+
_mi_memzero_inline(adst, n)
9781004
#else
9791005
// Default fallback on `_mi_memcpy`
9801006
static inline void _mi_memcpy_aligned(void* dst, const void* src, size_t n) {
@@ -986,6 +1012,9 @@ static inline void _mi_memzero_aligned(void* dst, size_t n) {
9861012
mi_assert_internal((uintptr_t)dst % MI_INTPTR_SIZE == 0);
9871013
_mi_memzero(dst, n);
9881014
}
1015+
1016+
#define _mi_memcpy_inline_aligned(dst, src, n) _mi_memcpy_aligned(dst, src, n)
1017+
#define _mi_memzero_inline_aligned(dst, n) _mi_memzero_aligned(dst, n)
9891018
#endif
9901019

9911020

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)