2023-06-14 16:52:36 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0 */
|
2015-02-14 17:29:47 +05:00
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "util/aligned_malloc.h"
|
|
|
|
|
#include "util/guarded_allocator.h"
|
2015-02-14 17:29:47 +05:00
|
|
|
|
2015-02-15 23:11:33 +05:00
|
|
|
#include <cassert>
|
|
|
|
|
|
2015-02-14 17:29:47 +05:00
|
|
|
/* Adopted from Libmv. */
|
|
|
|
|
|
2023-05-09 13:19:16 +02:00
|
|
|
#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
|
2015-02-14 17:29:47 +05:00
|
|
|
/* Needed for memalign on Linux and _aligned_alloc on Windows. */
|
|
|
|
|
# ifdef FREE_WINDOWS
|
|
|
|
|
/* Make sure _aligned_malloc is included. */
|
|
|
|
|
# ifdef __MSVCRT_VERSION__
|
|
|
|
|
# undef __MSVCRT_VERSION__
|
|
|
|
|
# endif
|
|
|
|
|
# define __MSVCRT_VERSION__ 0x0700
|
|
|
|
|
# endif /* FREE_WINDOWS */
|
|
|
|
|
# include <malloc.h>
|
|
|
|
|
#else
|
2024-08-23 10:02:36 +10:00
|
|
|
/* Apple's `malloc` is 16-byte aligned, and does not have `malloc.h`, so include
|
|
|
|
|
* `stdilb` instead.
|
2015-02-14 17:29:47 +05:00
|
|
|
*/
|
|
|
|
|
# include <cstdlib>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
2015-02-19 22:17:42 +05:00
|
|
|
void *util_aligned_malloc(size_t size, int alignment)
|
2015-02-14 17:29:47 +05:00
|
|
|
{
|
2015-02-19 01:48:59 +05:00
|
|
|
#ifdef WITH_BLENDER_GUARDEDALLOC
|
|
|
|
|
return MEM_mallocN_aligned(size, alignment, "Cycles Aligned Alloc");
|
2016-02-16 15:32:26 +01:00
|
|
|
#elif defined(_WIN32)
|
2015-02-14 17:29:47 +05:00
|
|
|
return _aligned_malloc(size, alignment);
|
2023-05-09 13:19:16 +02:00
|
|
|
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
2015-02-14 17:29:47 +05:00
|
|
|
void *result;
|
2015-03-28 00:15:15 +05:00
|
|
|
if (posix_memalign(&result, alignment, size)) {
|
2015-02-14 17:29:47 +05:00
|
|
|
/* Non-zero means allocation error
|
|
|
|
|
* either no allocation or bad alignment value.
|
|
|
|
|
*/
|
2024-12-26 17:53:55 +01:00
|
|
|
return nullptr;
|
2015-02-14 17:29:47 +05:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
#else /* This is for Linux. */
|
|
|
|
|
return memalign(alignment, size);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void util_aligned_free(void *ptr)
|
|
|
|
|
{
|
2015-02-19 01:48:59 +05:00
|
|
|
#if defined(WITH_BLENDER_GUARDEDALLOC)
|
2024-12-26 17:53:55 +01:00
|
|
|
if (ptr != nullptr) {
|
2015-02-19 01:48:59 +05:00
|
|
|
MEM_freeN(ptr);
|
|
|
|
|
}
|
|
|
|
|
#elif defined(_WIN32)
|
2015-02-14 17:29:47 +05:00
|
|
|
_aligned_free(ptr);
|
|
|
|
|
#else
|
|
|
|
|
free(ptr);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|