Blender V5.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#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 * `stdlib` instead.
29 */
30# include <cstdlib>
31#endif
32
33#ifdef _WIN32
34# include "util/windows.h"
35#else
36# include <sys/mman.h>
37#endif
38
40
41void *util_aligned_malloc(const size_t size, const int alignment)
42{
43 void *mem = nullptr;
44#ifdef WITH_BLENDER_GUARDEDALLOC
45 mem = MEM_new_uninitialized_aligned(size, alignment, "Cycles Aligned Alloc");
46#elif defined(_WIN32)
47 mem = _aligned_malloc(size, alignment);
48#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
49 if (posix_memalign(&mem, alignment, size)) {
50 /* Non-zero means allocation error
51 * either no allocation or bad alignment value. */
52 mem = nullptr;
53 }
54#else /* This is for Linux. */
55 mem = memalign(alignment, size);
56#endif
57 if (mem) {
59 }
60 return mem;
61}
62
63void *util_page_aligned_malloc(const size_t size)
64{
65 void *mem = nullptr;
66#ifdef _WIN32
67 static const auto VirtualAlloc2 = reinterpret_cast<PVOID (*)(
68 HANDLE, PVOID, SIZE_T, ULONG, ULONG, MEM_EXTENDED_PARAMETER *, ULONG)>(
69 GetProcAddress(GetModuleHandleW(L"KernelBase.dll"), "VirtualAlloc2"));
70 if (VirtualAlloc2 != nullptr) {
71 MEM_ADDRESS_REQUIREMENTS address_requirements = {};
72 address_requirements.Alignment = GetLargePageMinimum();
73
74 MEM_EXTENDED_PARAMETER extended_param = {};
75 extended_param.Type = MemExtendedParameterAddressRequirements;
76 extended_param.Pointer = &address_requirements;
77
78 mem = VirtualAlloc2(
79 nullptr, nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE, &extended_param, 1);
80 }
81 else {
82 mem = VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
83 }
84#else
85 mem = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
86 if (mem == MAP_FAILED) {
87 return nullptr;
88 }
89#endif
90 if (mem) {
92 }
93 return mem;
94}
95
96void util_aligned_free(void *ptr, const size_t size)
97{
98 if (ptr) {
100 }
101#if defined(WITH_BLENDER_GUARDEDALLOC)
102 if (ptr != nullptr) {
103 MEM_delete_void(ptr);
104 }
105#elif defined(_WIN32)
106 _aligned_free(ptr);
107#else
108 free(ptr);
109#endif
110}
111
112void util_page_aligned_free(void *ptr, const size_t size)
113{
114 if (ptr) {
116 }
117#ifdef _WIN32
118 VirtualFree(ptr, 0, MEM_RELEASE);
119#else
120 munmap(ptr, size);
121#endif
122}
123
Read Blender memory allocation and freeing.
void * util_page_aligned_malloc(const size_t size)
void util_page_aligned_free(void *ptr, const size_t size)
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_delete_void(void *vmemh)
Definition mallocn.cc:109
void * MEM_new_uninitialized_aligned(size_t len, size_t alignment, const char *str)
Definition mallocn.cc:134
#define L