Blender V5.3
guarded_allocator.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include <cstddef>
8#include <cstdlib>
9#include <memory>
10#include <new>
11
12#ifdef WITH_BLENDER_GUARDEDALLOC
14#endif
15
17
18/* Internal use only. */
19void util_guarded_mem_alloc(const size_t n);
20void util_guarded_mem_free(const size_t n);
21
22/* Guarded allocator for the use with STL. */
23template<typename T> class GuardedAllocator {
24 public:
25 using size_type = size_t;
26 using difference_type = ptrdiff_t;
27 using pointer = T *;
28 using const_pointer = const T *;
29 using reference = T &;
30 using const_reference = const T &;
31 using value_type = T;
32
33 GuardedAllocator() = default;
34 GuardedAllocator(const GuardedAllocator & /*unused*/) = default;
35
36 T *allocate(const size_t n, const void *hint = nullptr)
37 {
38 (void)hint;
39 size_t size = n * sizeof(T);
41 if (n == 0) {
42 return nullptr;
43 }
44 T *mem;
45#ifdef WITH_BLENDER_GUARDEDALLOC
46 /* C++ standard requires allocation functions to allocate memory suitably
47 * aligned for any standard type. This is 16 bytes for 64 bit platform as
48 * far as i concerned. We might over-align on 32bit here, but that should
49 * be all safe actually.
50 */
51 mem = (T *)MEM_new_uninitialized_aligned(size, 16, "Cycles Alloc");
52#else
53 mem = (T *)malloc(size);
54#endif
55 if (mem == nullptr) {
56 throw std::bad_alloc();
57 }
58 return mem;
59 }
60
61 void deallocate(T *p, const size_t n)
62 {
63 util_guarded_mem_free(n * sizeof(T));
64 if (p != nullptr) {
65#ifdef WITH_BLENDER_GUARDEDALLOC
66 MEM_delete_void(const_cast<void *>(static_cast<const void *>(p)));
67#else
68 free(p);
69#endif
70 }
71 }
72
73 T *address(T &x) const
74 {
75 return &x;
76 }
77
78 const T *address(const T &x) const
79 {
80 return &x;
81 }
82
83 GuardedAllocator<T> &operator=(const GuardedAllocator & /*unused*/) = default;
84
85 size_t max_size() const
86 {
87 return size_t(-1);
88 }
89
90 template<class U> struct rebind {
92 };
93
94 template<class U> GuardedAllocator(const GuardedAllocator<U> & /*unused*/) {}
95
96 template<class U> GuardedAllocator &operator=(const GuardedAllocator<U> & /*unused*/)
97 {
98 return *this;
99 }
100
101 bool operator==(const GuardedAllocator & /*other*/) const
102 {
103 return true;
104 }
105 bool operator!=(const GuardedAllocator &other) const
106 {
107 return !operator==(other);
108 }
109
110#if defined(_MSC_VER) && _ITERATOR_DEBUG_LEVEL
111 /* Welcome to the black magic here.
112 *
113 * The issue is that MSVC C++ allocates container proxy on any
114 * vector initialization, including static vectors which don't
115 * have any data yet. This leads to several issues:
116 *
117 * - Static objects initialization fiasco (global_stats from
118 * util_stats.h might not be initialized yet).
119 * - If main() function changes allocator type (for example,
120 * this might happen with `blender --debug-memory`) nobody
121 * will know how to convert already allocated memory to a new
122 * guarded allocator.
123 *
124 * Here we work this around by making it so container proxy does
125 * not use guarded allocation. A bit fragile, unfortunately.
126 *
127 * Note: _Container_proxy only exists when _ITERATOR_DEBUG_LEVEL != 0,
128 * which is typically only in Debug builds with newer MSVC versions.
129 */
130 template<> struct rebind<std::_Container_proxy> {
131 typedef std::allocator<std::_Container_proxy> other;
132 };
133
134 operator std::allocator<std::_Container_proxy>() const
135 {
136 return std::allocator<std::_Container_proxy>();
137 }
138#endif
139};
140
141/* Get memory usage and peak from the guarded STL allocator. */
144
145/* Call given function and keep track if it runs out of memory.
146 *
147 * If it does run out f memory, stop execution and set progress
148 * to do a global cancel.
149 *
150 * It's not fully robust, but good enough to catch obvious issues
151 * when running out of memory.
152 */
153#define MEM_GUARDED_CALL(progress, func, ...) \
154 do { \
155 try { \
156 (func)(__VA_ARGS__); \
157 } \
158 catch (std::bad_alloc &) { \
159 LOG_ERROR << "Out of memory"; \
160 fflush(stderr); \
161 (progress)->set_error("Out of memory"); \
162 } \
163 } while (false)
164
Read Blender memory allocation and freeing.
bool operator!=(const GuardedAllocator &other) const
GuardedAllocator< T > & operator=(const GuardedAllocator &)=default
bool operator==(const GuardedAllocator &) const
T * allocate(const size_t n, const void *hint=nullptr)
GuardedAllocator & operator=(const GuardedAllocator< U > &)
T * address(T &x) const
GuardedAllocator(const GuardedAllocator< U > &)
GuardedAllocator()=default
const T * address(const T &x) const
GuardedAllocator(const GuardedAllocator &)=default
size_t max_size() const
void deallocate(T *p, const size_t n)
#define CCL_NAMESPACE_END
size_t util_guarded_get_mem_used()
size_t util_guarded_get_mem_peak()
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 T
GuardedAllocator< U > other