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