Blender  V2.93
util_stack_allocator.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __UTIL_STACK_ALLOCATOR_H__
18 #define __UTIL_STACK_ALLOCATOR_H__
19 
20 #include <cstddef>
21 #include <memory>
22 
24 
25 /* Stack allocator for the use with STL. */
26 template<int SIZE, typename T> class ccl_try_align(16) StackAllocator
27 {
28  public:
29  typedef size_t size_type;
30  typedef ptrdiff_t difference_type;
31  typedef T *pointer;
32  typedef const T *const_pointer;
33  typedef T &reference;
34  typedef const T &const_reference;
35  typedef T value_type;
36 
37  /* Allocator construction/destruction. */
38 
39  StackAllocator() : pointer_(0), use_stack_(true)
40  {
41  }
42 
43  StackAllocator(const StackAllocator &) : pointer_(0), use_stack_(true)
44  {
45  }
46 
47  template<class U>
48  StackAllocator(const StackAllocator<SIZE, U> &) : pointer_(0), use_stack_(false)
49  {
50  }
51 
52  /* Memory allocation/deallocation. */
53 
54  T *allocate(size_t n, const void *hint = 0)
55  {
56  (void)hint;
57  if (n == 0) {
58  return NULL;
59  }
60  if (pointer_ + n >= SIZE || use_stack_ == false) {
61  size_t size = n * sizeof(T);
63  T *mem;
64 #ifdef WITH_BLENDER_GUARDEDALLOC
65  mem = (T *)MEM_mallocN_aligned(size, 16, "Cycles Alloc");
66 #else
67  mem = (T *)malloc(size);
68 #endif
69  if (mem == NULL) {
70  throw std::bad_alloc();
71  }
72  return mem;
73  }
74  T *mem = &data_[pointer_];
75  pointer_ += n;
76  return mem;
77  }
78 
79  void deallocate(T * p, size_t n)
80  {
81  if (p == NULL) {
82  return;
83  }
84  if (p < data_ || p >= data_ + SIZE) {
85  util_guarded_mem_free(n * sizeof(T));
86 #ifdef WITH_BLENDER_GUARDEDALLOC
87  MEM_freeN(p);
88 #else
89  free(p);
90 #endif
91  return;
92  }
93  /* We don't support memory free for the stack allocator. */
94  }
95 
96  /* Address of an reference. */
97 
98  T *address(T & x) const
99  {
100  return &x;
101  }
102 
103  const T *address(const T &x) const
104  {
105  return &x;
106  }
107 
108  /* Object construction/destruction. */
109 
110  void construct(T * p, const T &val)
111  {
112  if (p != NULL) {
113  new ((T *)p) T(val);
114  }
115  }
116 
117  void destroy(T * p)
118  {
119  p->~T();
120  }
121 
122  /* Maximum allocation size. */
123 
124  size_t max_size() const
125  {
126  return size_t(-1);
127  }
128 
129  /* Rebind to other type of allocator. */
130 
131  template<class U> struct rebind {
132  typedef StackAllocator<SIZE, U> other;
133  };
134 
135  /* Operators */
136 
137  template<class U> inline StackAllocator &operator=(const StackAllocator<SIZE, U> &)
138  {
139  return *this;
140  }
141 
142  StackAllocator<SIZE, T> &operator=(const StackAllocator &)
143  {
144  return *this;
145  }
146 
147  inline bool operator==(StackAllocator const & /*other*/) const
148  {
149  return true;
150  }
151 
152  inline bool operator!=(StackAllocator const &other) const
153  {
154  return !operator==(other);
155  }
156 
157  private:
158  int pointer_;
159  bool use_stack_;
160  T data_[SIZE];
161 };
162 
164 
165 #endif /* __UTIL_STACK_ALLOCATOR_H__ */
#define SIZE
void BLI_kdtree_nd_() free(KDTree *tree)
Definition: kdtree_impl.h:116
int size_type
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
btGeneric6DofConstraint & operator=(btGeneric6DofConstraint &other)
T * data_
bool operator==(const GeometrySet &UNUSED(a), const GeometrySet &UNUSED(b))
#define CCL_NAMESPACE_END
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN_aligned)(size_t len, size_t alignment, const char *str)
Definition: mallocn.c:49
#define T
constexpr bool operator!=(StringRef a, StringRef b)
void util_guarded_mem_free(size_t n)
void util_guarded_mem_alloc(size_t n)
CCL_NAMESPACE_BEGIN class ccl_try_align(16) StackAllocator