From 7bdcf96c9d9c61811ffd4570ba9fbbac5ffd237f Mon Sep 17 00:00:00 2001 From: Al Date: Thu, 6 Jul 2023 16:00:55 -0400 Subject: [PATCH] [memalign] no more realloc on aligned pointers, just do an aligned malloc and copy to it. Slower but safe and this is not called that often in practice, usually to resize larger matrices. --- src/vector.h | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/vector.h b/src/vector.h index 78a0fad4..462a8baf 100644 --- a/src/vector.h +++ b/src/vector.h @@ -21,27 +21,19 @@ static inline void *_aligned_realloc(void *p, size_t size, size_t alignment) return NULL; } - if (size == 0) { + if (p == NULL) { return NULL; } - void *rp = realloc(p, size); - - /* If realloc result is not already at an aligned boundary, - _aligned_malloc a new block and copy the contents of the realloc'd - pointer to the aligned block, free the realloc'd pointer and return - the aligned pointer. - */ - if ( ((size_t)rp & (alignment - 1)) != 0) { - void *p1 = _aligned_malloc(size, alignment); - if (p1 != NULL) { - memcpy(p1, rp, size); - } - free(rp); - rp = p1; + void *p1 = _aligned_malloc(size, alignment); + if (p1 == NULL) { + free(p); + return NULL; } - return rp; + memcpy(p1, p, size); + free(p); + return p1; } static inline void _aligned_free(void *p) {