Blender V5.0
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#pragma once
6
7#include <cstddef>
8
9#include "util/defines.h"
11
13
14/* Stack allocator for the use with STL. */
15template<int SIZE, typename T> class ccl_try_align(16) StackAllocator {
16 public:
17 using size_type = size_t;
18 using difference_type = ptrdiff_t;
19 using pointer = T *;
20 using const_pointer = const T *;
21 using reference = T &;
22 using const_reference = const T &;
23 using value_type = T;
24
25 /* Allocator construction/destruction. */
26
27 StackAllocator() : pointer_(0), use_stack_(true) {}
28
29 StackAllocator(const StackAllocator & /*unused*/) : pointer_(0), use_stack_(true) {}
30
31 template<class U>
32 StackAllocator(const StackAllocator<SIZE, U> & /*unused*/) : pointer_(0), use_stack_(false)
33 {
34 }
35
36 /* Memory allocation/deallocation. */
37
38 T *allocate(const size_t n, const void *hint = nullptr)
39 {
40 (void)hint;
41 if (n == 0) {
42 return nullptr;
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 == nullptr) {
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, const size_t n)
64 {
65 if (p == nullptr) {
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(static_cast<void *>(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 != nullptr) {
97 new (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 using other = StackAllocator<SIZE, U>;
117 };
118
119 /* Operators */
120
121 template<class U> StackAllocator &operator=(const StackAllocator<SIZE, U> & /*unused*/)
122 {
123 return *this;
124 }
125
126 StackAllocator<SIZE, T> &operator=(const StackAllocator & /*unused*/)
127 {
128 return *this;
129 }
130
131 bool operator==(const StackAllocator & /*other*/) const
132 {
133 return true;
134 }
135
136 bool operator!=(const StackAllocator &other) const
137 {
138 return !operator==(other);
139 }
140
141 private:
142 int pointer_;
143 bool use_stack_;
144 T data_[SIZE];
145};
146
#define SIZE
void BLI_kdtree_nd_ free(KDTree *tree)
BLI_INLINE bool operator!=(const ListBase &a, const ListBase &b)
bool operator==(const AssetWeakReference &a, const AssetWeakReference &b)
int size_type
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
btGeneric6DofConstraint & operator=(btGeneric6DofConstraint &other)
#define ccl_try_align(...)
#define CCL_NAMESPACE_END
void util_guarded_mem_alloc(const size_t n)
void util_guarded_mem_free(const size_t n)
void * MEM_mallocN_aligned(size_t len, size_t alignment, const char *str)
Definition mallocn.cc:138
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
#define T