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