Blender V4.3
aligned_malloc.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
7
8#include <cassert>
9
10/* Adopted from Libmv. */
11
12#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
13/* Needed for memalign on Linux and _aligned_alloc on Windows. */
14# ifdef FREE_WINDOWS
15/* Make sure _aligned_malloc is included. */
16# ifdef __MSVCRT_VERSION__
17# undef __MSVCRT_VERSION__
18# endif
19# define __MSVCRT_VERSION__ 0x0700
20# endif /* FREE_WINDOWS */
21# include <malloc.h>
22#else
23/* Apple's `malloc` is 16-byte aligned, and does not have `malloc.h`, so include
24 * `stdilb` instead.
25 */
26# include <cstdlib>
27#endif
28
30
31void *util_aligned_malloc(size_t size, int alignment)
32{
33#ifdef WITH_BLENDER_GUARDEDALLOC
34 return MEM_mallocN_aligned(size, alignment, "Cycles Aligned Alloc");
35#elif defined(_WIN32)
36 return _aligned_malloc(size, alignment);
37#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
38 void *result;
39 if (posix_memalign(&result, alignment, size)) {
40 /* Non-zero means allocation error
41 * either no allocation or bad alignment value.
42 */
43 return NULL;
44 }
45 return result;
46#else /* This is for Linux. */
47 return memalign(alignment, size);
48#endif
49}
50
52{
53#if defined(WITH_BLENDER_GUARDEDALLOC)
54 if (ptr != NULL) {
56 }
57#elif defined(_WIN32)
58 _aligned_free(ptr);
59#else
60 free(ptr);
61#endif
62}
63
void BLI_kdtree_nd_ free(KDTree *tree)
void util_aligned_free(void *ptr)
CCL_NAMESPACE_BEGIN void * util_aligned_malloc(size_t size, int alignment)
#define CCL_NAMESPACE_END
#define NULL
void * MEM_mallocN_aligned(size_t len, size_t alignment, const char *str)
Definition mallocn.cc:110
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
PointerRNA * ptr
Definition wm_files.cc:4126