From c504e8e5537af631c4cfb57224824dac9af678c4 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Sun, 12 Feb 2023 00:14:37 +0100 Subject: [PATCH] FreeBSD: enforce aligned allocation This fixes those errors experienced on FreeBSD: crunch/crnlib/crn_mem.cpp(125): Assertion failed: "crnlib_free: bad ptr" crunch/crnlib/crn_mem.cpp(125): Assertion failed: "crnlib_realloc: bad ptr" The usual malloc should already be aligned, but for some unknown reason it doesn't work on FreeBSD. Quote from `man malloc` on FreeBSD: > Standard API > The malloc() function allocates size bytes of uninitialized memory. The > allocated space is suitably aligned (after possible pointer coercion) > for storage of any type of object. This patch should not be needed. See https://github.com/DaemonEngine/crunch/pull/36 --- crnlib/crn_mem.cpp | 5 +++++ inc/crn_decomp.h | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/crnlib/crn_mem.cpp b/crnlib/crn_mem.cpp index 4040e4cd..de4d0ee6 100644 --- a/crnlib/crn_mem.cpp +++ b/crnlib/crn_mem.cpp @@ -69,7 +69,12 @@ static void* crnlib_default_realloc(void* p, size_t size, size_t* pActual_size, void* p_new; if (!p) { +#if defined(__FreeBSD__) + // See https://github.com/DaemonEngine/crunch/pull/36 + p_new = ::aligned_alloc(CRNLIB_MIN_ALLOC_ALIGNMENT, size); +#else p_new = ::malloc(size); +#endif CRNLIB_ASSERT((reinterpret_cast(p_new) & (CRNLIB_MIN_ALLOC_ALIGNMENT - 1)) == 0); if (!p_new) { diff --git a/inc/crn_decomp.h b/inc/crn_decomp.h index f9fe4a2b..c3269688 100644 --- a/inc/crn_decomp.h +++ b/inc/crn_decomp.h @@ -1928,7 +1928,12 @@ static void* crnd_default_realloc(void* p, size_t size, size_t* pActual_size, bo void* p_new; if (!p) { +#if defined(__FreeBSD__) + // See https://github.com/DaemonEngine/crunch/pull/36 + p_new = ::aligned_alloc(CRNLIB_MIN_ALLOC_ALIGNMENT, size); +#else p_new = ::malloc(size); +#endif if (pActual_size) { #ifdef WIN32