Blender  V2.93
util_aligned_malloc.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2015 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
19 
20 #include <cassert>
21 
22 /* Adopted from Libmv. */
23 
24 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
25 /* Needed for memalign on Linux and _aligned_alloc on Windows. */
26 # ifdef FREE_WINDOWS
27 /* Make sure _aligned_malloc is included. */
28 # ifdef __MSVCRT_VERSION__
29 # undef __MSVCRT_VERSION__
30 # endif
31 # define __MSVCRT_VERSION__ 0x0700
32 # endif /* FREE_WINDOWS */
33 # include <malloc.h>
34 #else
35 /* Apple's malloc is 16-byte aligned, and does not have malloc.h, so include
36  * stdilb instead.
37  */
38 # include <cstdlib>
39 #endif
40 
42 
43 void *util_aligned_malloc(size_t size, int alignment)
44 {
45 #ifdef WITH_BLENDER_GUARDEDALLOC
46  return MEM_mallocN_aligned(size, alignment, "Cycles Aligned Alloc");
47 #elif defined(_WIN32)
48  return _aligned_malloc(size, alignment);
49 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
50  void *result;
51  if (posix_memalign(&result, alignment, size)) {
52  /* Non-zero means allocation error
53  * either no allocation or bad alignment value.
54  */
55  return NULL;
56  }
57  return result;
58 #else /* This is for Linux. */
59  return memalign(alignment, size);
60 #endif
61 }
62 
64 {
65 #if defined(WITH_BLENDER_GUARDEDALLOC)
66  if (ptr != NULL) {
67  MEM_freeN(ptr);
68  }
69 #elif defined(_WIN32)
70  _aligned_free(ptr);
71 #else
72  free(ptr);
73 #endif
74 }
75 
void BLI_kdtree_nd_() free(KDTree *tree)
Definition: kdtree_impl.h:116
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
#define CCL_NAMESPACE_END
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN_aligned)(size_t len, size_t alignment, const char *str)
Definition: mallocn.c:49
void util_aligned_free(void *ptr)
CCL_NAMESPACE_BEGIN void * util_aligned_malloc(size_t size, int alignment)
PointerRNA * ptr
Definition: wm_files.c:3157