| OpenSubdiv

Loading...
Searching...
No Matches
stackBuffer.h
Go to the documentation of this file.
1//
2// Copyright 2015 DreamWorks Animation LLC.
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://opensubdiv.org/license.
6//
7#ifndef OPENSUBDIV3_VTR_STACK_BUFFER_H
8#define OPENSUBDIV3_VTR_STACK_BUFFER_H
9
10#include "../version.h"
11
12 namespace OpenSubdiv {
13 namespace OPENSUBDIV_VERSION {
14
15 namespace Vtr {
16 namespace internal {
17
18//
19// The StackBuffer class is intended solely to take the place of VLAs (Variable
20// Length Arrays) which most compilers support, but are not strictly standard C++.
21// Portability concerns forces us to make use of either alloca() or some other
22// mechanism to create small arrays on the stack that are typically based on the
23// valence of a vertex -- small in general, but occasionally large.
24//
25// Note also that since the intent of this is to replace VLAs -- not general
26// std::vectors -- support for std::vector functionality is intentionally limited
27// and STL-like naming is avoided. Like a VLA there is no incremental growth.
28// Support for resizing is available to reuse an instance at the beginning of a
29// loop with a new size, but resizing in this case reinitializes all elements.
30//
31
32 template <typename TYPE, unsigned int SIZE, bool POD_TYPE = false>
33 class StackBuffer
34{
35 public:
36 typedef unsigned int size_type;
37
38 public:
39 // Constructors and destructor -- declared inline below:
40 StackBuffer();
41 StackBuffer(size_type size);
42 ~StackBuffer();
43
44 public:
45 // Note the reliance on implicit casting so that it can be used similar to
46 // a VLA. This removes the need for operator[] as the resulting TYPE* will
47 // natively support []. (The presence of both TYPE* and operator[] also
48 // causes an ambiguous overloading error with 32-bit MSVC builds.)
49
50 operator TYPE const * () const { return _data; }
51 operator TYPE * () { return _data; }
52
53 size_type GetSize() const { return _size; }
54
55 void SetSize(size_type size);
56 void Reserve(size_type capacity);
57
58 private:
59 // Non-copyable:
60 StackBuffer(const StackBuffer<TYPE,SIZE,POD_TYPE> &) { }
61 StackBuffer& operator=(const StackBuffer<TYPE,SIZE,POD_TYPE> &) { return *this; }
62
63 void allocate(size_type capacity);
64 void deallocate();
65 void construct();
66 void destruct();
67
68 private:
69 TYPE * _data;
70 size_type _size;
71 size_type _capacity;
72
73 // Is alignment an issue here? The staticData arena will at least be double-word
74 // aligned within this struct, which meets current and most anticipated needs.
75 char _staticData[SIZE * sizeof(TYPE)];
76 char * _dynamicData;
77};
78
79
80//
81// Core allocation/deallocation methods:
82//
83 template <typename TYPE, unsigned int SIZE, bool POD_TYPE>
84 inline void
85StackBuffer<TYPE,SIZE,POD_TYPE>::allocate(size_type capacity) {
86
87 // Again, is alignment an issue here? C++ spec says new will return pointer
88 // "suitably aligned" for conversion to pointers of other types, which implies
89 // at least an alignment of 16.
90 _dynamicData = static_cast<char*>(::operator new(capacity * sizeof(TYPE)));
91
92 _data = reinterpret_cast<TYPE*>(_dynamicData);
93 _capacity = capacity;
94}
95
96 template <typename TYPE, unsigned int SIZE, bool POD_TYPE>
97 inline void
98StackBuffer<TYPE,SIZE,POD_TYPE>::deallocate() {
99
100 ::operator delete(_dynamicData);
101
102 _data = reinterpret_cast<TYPE*>(_staticData);
103 _capacity = SIZE;
104}
105
106//
107// Explicit element-wise construction and destruction within allocated memory.
108// Compilers do not always optimize out the iteration here even when there is
109// no construction or destruction, so the POD_TYPE arguement can be used to
110// force this when/if it becomes an issue (and it has been in some cases).
111//
112 template <typename TYPE, unsigned int SIZE, bool POD_TYPE>
113 inline void
114StackBuffer<TYPE,SIZE,POD_TYPE>::construct() {
115
116 for (size_type i = 0; i < _size; ++i) {
117 (void) new (&_data[i]) TYPE;
118 }
119}
120 template <typename TYPE, unsigned int SIZE, bool POD_TYPE>
121 inline void
122StackBuffer<TYPE,SIZE,POD_TYPE>::destruct() {
123
124 for (size_type i = 0; i < _size; ++i) {
125 _data[i].~TYPE();
126 }
127}
128
129//
130// Inline constructors and destructor:
131//
132 template <typename TYPE, unsigned int SIZE, bool POD_TYPE>
133 inline
134StackBuffer<TYPE,SIZE,POD_TYPE>::StackBuffer() :
135 _data(reinterpret_cast<TYPE*>(_staticData)),
136 _size(0),
137 _capacity(SIZE),
138 _dynamicData(0) {
139
140}
141
142 template <typename TYPE, unsigned int SIZE, bool POD_TYPE>
143 inline
144StackBuffer<TYPE,SIZE,POD_TYPE>::StackBuffer(size_type size) :
145 _data(reinterpret_cast<TYPE*>(_staticData)),
146 _size(size),
147 _capacity(SIZE),
148 _dynamicData(0) {
149
150 if (size > SIZE) {
151 allocate(size);
152 }
153 if (!POD_TYPE) {
154 construct();
155 }
156}
157
158 template <typename TYPE, unsigned int SIZE, bool POD_TYPE>
159 inline
160StackBuffer<TYPE,SIZE,POD_TYPE>::~StackBuffer() {
161
162 if (!POD_TYPE) {
163 destruct();
164 }
165 deallocate();
166}
167
168//
169// Inline sizing methods:
170//
171 template <typename TYPE, unsigned int SIZE, bool POD_TYPE>
172 inline void
173StackBuffer<TYPE,SIZE,POD_TYPE>::Reserve(size_type capacity) {
174
175 if (capacity > _capacity) {
176 if (!POD_TYPE) {
177 destruct();
178 }
179 deallocate();
180 allocate(capacity);
181 }
182}
183
184 template <typename TYPE, unsigned int SIZE, bool POD_TYPE>
185 inline void
186StackBuffer<TYPE,SIZE,POD_TYPE>::SetSize(size_type size)
187{
188 if (!POD_TYPE) {
189 destruct();
190 }
191 if (size == 0) {
192 deallocate();
193 } else if (size > _capacity) {
194 deallocate();
195 allocate(size);
196 }
197 _size = size;
198 if (!POD_TYPE) {
199 construct();
200 }
201}
202
203} // end namespace internal
204} // end namespace Vtr
205
206} // end namespace OPENSUBDIV_VERSION
207 using namespace OPENSUBDIV_VERSION;
208} // end namespace OpenSubdiv
209
210#endif /* OPENSUBDIV3_VTR_STACK_BUFFER_H */
OpenSubdiv
Definition limits.h:15
OpenSubdiv::OPENSUBDIV_VERSION
Definition limits.h:16
OpenSubdiv::OPENSUBDIV_VERSION::Vtr
Definition topologyRefiner.h:23