Files
test/intern/cycles/util/aligned_malloc.cpp
Brecht Van Lommel d0c2e68e5f Refactor: Cycles: Automated clang-tidy fixups in Cycles
* Use .empty() and .data()
* Use nullptr instead of 0
* No else after return
* Simple class member initialization
* Add override for virtual methods
* Include C++ instead of C headers
* Remove some unused includes
* Use default constructors
* Always use braces
* Consistent names in definition and declaration
* Change typedef to using

Pull Request: https://projects.blender.org/blender/blender/pulls/132361
2025-01-03 10:22:55 +01:00

68 lines
1.6 KiB
C++

/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include "util/aligned_malloc.h"
#ifdef WITH_BLENDER_GUARDEDALLOC
# include "../../guardedalloc/MEM_guardedalloc.h"
#endif
#include <cassert>
/* Adopted from Libmv. */
#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
/* 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
/* Apple's `malloc` is 16-byte aligned, and does not have `malloc.h`, so include
* `stdilb` instead.
*/
# include <cstdlib>
#endif
CCL_NAMESPACE_BEGIN
void *util_aligned_malloc(size_t size, int alignment)
{
#ifdef WITH_BLENDER_GUARDEDALLOC
return MEM_mallocN_aligned(size, alignment, "Cycles Aligned Alloc");
#elif defined(_WIN32)
return _aligned_malloc(size, alignment);
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
void *result;
if (posix_memalign(&result, alignment, size)) {
/* Non-zero means allocation error
* either no allocation or bad alignment value.
*/
return nullptr;
}
return result;
#else /* This is for Linux. */
return memalign(alignment, size);
#endif
}
void util_aligned_free(void *ptr)
{
#if defined(WITH_BLENDER_GUARDEDALLOC)
if (ptr != nullptr) {
MEM_freeN(ptr);
}
#elif defined(_WIN32)
_aligned_free(ptr);
#else
free(ptr);
#endif
}
CCL_NAMESPACE_END