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