[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.

This commit is contained in:
Al
2023-07-06 16:00:55 -04:00
parent 5a1f6df5a9
commit 7bdcf96c9d

View File

@@ -21,27 +21,19 @@ static inline void *_aligned_realloc(void *p, size_t size, size_t alignment)
return NULL; return NULL;
} }
if (size == 0) { if (p == NULL) {
return NULL; return NULL;
} }
void *rp = realloc(p, size); void *p1 = _aligned_malloc(size, alignment);
if (p1 == NULL) {
/* If realloc result is not already at an aligned boundary, free(p);
_aligned_malloc a new block and copy the contents of the realloc'd return NULL;
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;
} }
return rp; memcpy(p1, p, size);
free(p);
return p1;
} }
static inline void _aligned_free(void *p) static inline void _aligned_free(void *p)
{ {