Blender V4.3
stack_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#ifndef __UTIL_STACK_ALLOCATOR_H__
6#define __UTIL_STACK_ALLOCATOR_H__
7
8#include <cstddef>
9#include <memory>
10
12
13/* Stack allocator for the use with STL. */
14template<int SIZE, typename T> class ccl_try_align(16) StackAllocator
15{
16 public:
17 typedef size_t size_type;
18 typedef ptrdiff_t difference_type;
19 typedef T *pointer;
20 typedef const T *const_pointer;
21 typedef T &reference;
22 typedef const T &const_reference;
23 typedef T value_type;
24
25 /* Allocator construction/destruction. */
26
27 StackAllocator() : pointer_(0), use_stack_(true) {}
28
29 StackAllocator(const StackAllocator &) : pointer_(0), use_stack_(true) {}
30
31 template<class U>
32 StackAllocator(const StackAllocator<SIZE, U> &) : pointer_(0), use_stack_(false)
33 {
34 }
35
36 /* Memory allocation/deallocation. */
37
38 T *allocate(size_t n, const void *hint = 0)
39 {
40 (void)hint;
41 if (n == 0) {
42 return NULL;
43 }
44 if (pointer_ + n >= SIZE || use_stack_ == false) {
45 size_t size = n * sizeof(T);
47 T *mem;
48#ifdef WITH_BLENDER_GUARDEDALLOC
49 mem = (T *)MEM_mallocN_aligned(size, 16, "Cycles Alloc");
50#else
51 mem = (T *)malloc(size);
52#endif
53 if (mem == NULL) {
54 throw std::bad_alloc();
55 }
56 return mem;
57 }
58 T *mem = &data_[pointer_];
59 pointer_ += n;
60 return mem;
61 }
62
63 void deallocate(T * p, size_t n)
64 {
65 if (p == NULL) {
66 return;
67 }
68 if (p < data_ || p >= data_ + SIZE) {
69 util_guarded_mem_free(n * sizeof(T));
70#ifdef WITH_BLENDER_GUARDEDALLOC
71 MEM_freeN(p);
72#else
73 free(p);
74#endif
75 return;
76 }
77 /* We don't support memory free for the stack allocator. */
78 }
79
80 /* Address of an reference. */
81
82 T *address(T & x) const
83 {
84 return &x;
85 }
86
87 const T *address(const T &x) const
88 {
89 return &x;
90 }
91
92 /* Object construction/destruction. */
93
94 void construct(T * p, const T &val)
95 {
96 if (p != NULL) {
97 new ((T *)p) T(val);
98 }
99 }
100
101 void destroy(T * p)
102 {
103 p->~T();
104 }
105
106 /* Maximum allocation size. */
107
108 size_t max_size() const
109 {
110 return size_t(-1);
111 }
112
113 /* Rebind to other type of allocator. */
114
115 template<class U> struct rebind {
116 typedef StackAllocator<SIZE, U> other;
117 };
118
119 /* Operators */
120
121 template<class U> inline StackAllocator &operator=(const StackAllocator<SIZE, U> &)
122 {
123 return *this;
124 }
125
126 StackAllocator<SIZE, T> &operator=(const StackAllocator &)
127 {
128 return *this;
129 }
130
131 inline bool operator==(StackAllocator const & /*other*/) const
132 {
133 return true;
134 }
135
136 inline bool operator!=(StackAllocator const &other) const
137 {
138 return !operator==(other);
139 }
140
141 private:
142 int pointer_;
143 bool use_stack_;
144 T data_[SIZE];
145};
146
148
149#endif /* __UTIL_STACK_ALLOCATOR_H__ */
#define SIZE
void BLI_kdtree_nd_ free(KDTree *tree)
bool operator==(const AssetWeakReference &a, const AssetWeakReference &b)
int size_type
btGeneric6DofConstraint & operator=(btGeneric6DofConstraint &other)
#define CCL_NAMESPACE_END
#define NULL
#define ccl_try_align(...)
void util_guarded_mem_free(size_t n)
void util_guarded_mem_alloc(size_t n)
void * MEM_mallocN_aligned(size_t len, size_t alignment, const char *str)
Definition mallocn.cc:110
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
ccl_device_inline bool operator!=(const float2 a, const float2 b)
#define T