diff options
author | Daan Leijen <daan@microsoft.com> | 2022-04-20 17:34:56 -0700 |
---|---|---|
committer | Daan Leijen <daan@microsoft.com> | 2022-04-20 17:34:56 -0700 |
commit | cacb387a61df073162064aaf513fcc32be95c983 (patch) | |
tree | 5098e0aa7ca9ba6377e24273860a69862200e19c | |
parent | 83d84b8703644a22839f956dbf4380fa4f272d48 (diff) | |
parent | 864e4be5ce34b90421acec405fcc8b795038248d (diff) |
Merge branch 'dev' into dev-slice
-rw-r--r-- | include/mimalloc-internal.h | 6 | ||||
-rw-r--r-- | src/alloc.c | 4 |
2 files changed, 9 insertions, 1 deletions
diff --git a/include/mimalloc-internal.h b/include/mimalloc-internal.h index 4aede7b..2e1fee4 100644 --- a/include/mimalloc-internal.h +++ b/include/mimalloc-internal.h @@ -226,6 +226,12 @@ static inline bool _mi_is_power_of_two(uintptr_t x) { return ((x & (x - 1)) == 0); } +// Is a pointer aligned? +static inline bool _mi_is_aligned(void* p, size_t alignment) { + mi_assert_internal(alignment != 0); + return (((uintptr_t)p % alignment) == 0); +} + // Align upwards static inline uintptr_t _mi_align_up(uintptr_t sz, size_t alignment) { mi_assert_internal(alignment != 0); diff --git a/src/alloc.c b/src/alloc.c index 60ed76e..3892e8d 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -639,7 +639,9 @@ void* _mi_heap_realloc_zero(mi_heap_t* heap, void* p, size_t newsize, bool zero) memset((uint8_t*)newp + start, 0, newsize - start); } if mi_likely(p != NULL) { - _mi_memcpy_aligned(newp, p, (newsize > size ? size : newsize)); + if mi_likely(_mi_is_aligned(p, sizeof(uintptr_t))) { // a client may pass in an arbitrary pointer `p`.. + _mi_memcpy_aligned(newp, p, (newsize > size ? size : newsize)); + } mi_free(p); // only free the original pointer if successful } } |