Blender V5.0
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#ifdef WITH_BLENDER_GUARDEDALLOC
10#endif
11
12#include <cassert>
13
14/* Adopted from Libmv. */
15
16#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
17/* Needed for memalign on Linux and _aligned_alloc on Windows. */
18# ifdef FREE_WINDOWS
19/* Make sure _aligned_malloc is included. */
20# ifdef __MSVCRT_VERSION__
21# undef __MSVCRT_VERSION__
22# endif
23# define __MSVCRT_VERSION__ 0x0700
24# endif /* FREE_WINDOWS */
25# include <malloc.h>
26#else
27/* Apple's `malloc` is 16-byte aligned, and does not have `malloc.h`, so include
28 * `stdilb` instead.
29 */
30# include <cstdlib>
31#endif
32
34
35void *util_aligned_malloc(const size_t size, const int alignment)
36{
37 void *mem = nullptr;
38#ifdef WITH_BLENDER_GUARDEDALLOC
39 mem = MEM_mallocN_aligned(size, alignment, "Cycles Aligned Alloc");
40#elif defined(_WIN32)
41 mem = _aligned_malloc(size, alignment);
42#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
43 if (posix_memalign(&mem, alignment, size)) {
44 /* Non-zero means allocation error
45 * either no allocation or bad alignment value. */
46 mem = nullptr;
47 }
48#else /* This is for Linux. */
49 mem = memalign(alignment, size);
50#endif
51 if (mem) {
53 }
54 return mem;
55}
56
57void util_aligned_free(void *ptr, const size_t size)
58{
59 if (ptr) {
61 }
62#if defined(WITH_BLENDER_GUARDEDALLOC)
63 if (ptr != nullptr) {
65 }
66#elif defined(_WIN32)
67 _aligned_free(ptr);
68#else
69 free(ptr);
70#endif
71}
72
void BLI_kdtree_nd_ free(KDTree *tree)
Read Guarded memory(de)allocation.
CCL_NAMESPACE_BEGIN void * util_aligned_malloc(const size_t size, const int alignment)
void util_aligned_free(void *ptr, const size_t size)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
#define CCL_NAMESPACE_END
void util_guarded_mem_alloc(const size_t n)
void util_guarded_mem_free(const size_t n)
void * MEM_mallocN_aligned(size_t len, size_t alignment, const char *str)
Definition mallocn.cc:138
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
PointerRNA * ptr
Definition wm_files.cc:4238