VTK  9.2.6
vtkBuffer.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkBuffer.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
25 #ifndef vtkBuffer_h
26 #define vtkBuffer_h
27 
28 #include "vtkObject.h"
29 #include "vtkObjectFactory.h" // New() implementation
30 
31 #include <algorithm> // for std::min and std::copy
32 
33 template <class ScalarTypeT>
34 class vtkBuffer : public vtkObject
35 {
36 public:
38  typedef ScalarTypeT ScalarType;
39 
40  static vtkBuffer<ScalarTypeT>* New();
42 
46  inline ScalarType* GetBuffer() { return this->Pointer; }
47  inline const ScalarType* GetBuffer() const { return this->Pointer; }
48 
54  void SetBuffer(ScalarType* array, vtkIdType size);
55 
59  void SetMallocFunction(vtkMallocingFunction mallocFunction = malloc);
60 
64  void SetReallocFunction(vtkReallocingFunction reallocFunction = realloc);
65 
72  void SetFreeFunction(bool noFreeFunction, vtkFreeingFunction deleteFunction = free);
73 
77  inline vtkIdType GetSize() const { return this->Size; }
78 
82  bool Allocate(vtkIdType size);
83 
88  bool Reallocate(vtkIdType newsize);
89 
90 protected:
92  : Pointer(nullptr)
93  , Size(0)
94  {
98  }
99 
100  ~vtkBuffer() override { this->SetBuffer(nullptr, 0); }
101 
102  ScalarType* Pointer;
107 
108 private:
109  vtkBuffer(const vtkBuffer&) = delete;
110  void operator=(const vtkBuffer&) = delete;
111 };
112 
113 template <class ScalarT>
115 {
117 }
118 
119 template <class ScalarT>
121 {
122  auto mkhold = vtkMemkindRAII(true);
123  return vtkBuffer<ScalarT>::New();
124 }
125 
126 //------------------------------------------------------------------------------
127 template <typename ScalarT>
129 {
130  if (this->Pointer != array)
131  {
132  if (this->DeleteFunction)
133  {
134  this->DeleteFunction(this->Pointer);
135  }
136  this->Pointer = array;
137  }
138  this->Size = size;
139 }
140 //------------------------------------------------------------------------------
141 template <typename ScalarT>
143 {
144  this->MallocFunction = mallocFunction;
145 }
146 //------------------------------------------------------------------------------
147 template <typename ScalarT>
149 {
150  this->ReallocFunction = reallocFunction;
151 }
152 
153 //------------------------------------------------------------------------------
154 template <typename ScalarT>
155 void vtkBuffer<ScalarT>::SetFreeFunction(bool noFreeFunction, vtkFreeingFunction deleteFunction)
156 {
157  if (noFreeFunction)
158  {
159  this->DeleteFunction = nullptr;
160  }
161  else
162  {
163  this->DeleteFunction = deleteFunction;
164  }
165 }
166 
167 //------------------------------------------------------------------------------
168 template <typename ScalarT>
170 {
171  // release old memory.
172  this->SetBuffer(nullptr, 0);
173  if (size > 0)
174  {
175  ScalarType* newArray;
176  if (this->MallocFunction)
177  {
178  newArray = static_cast<ScalarType*>(this->MallocFunction(size * sizeof(ScalarType)));
179  }
180  else
181  {
182  newArray = static_cast<ScalarType*>(malloc(size * sizeof(ScalarType)));
183  }
184  if (newArray)
185  {
186  this->SetBuffer(newArray, size);
187  if (!this->MallocFunction)
188  {
189  this->DeleteFunction = free;
190  }
191  return true;
192  }
193  return false;
194  }
195  return true; // size == 0
196 }
197 
198 //------------------------------------------------------------------------------
199 template <typename ScalarT>
201 {
202  if (newsize == 0)
203  {
204  return this->Allocate(0);
205  }
206 
207  if (this->Pointer && this->DeleteFunction != free)
208  {
209  ScalarType* newArray;
210  bool forceFreeFunction = false;
211  if (this->MallocFunction)
212  {
213  newArray = static_cast<ScalarType*>(this->MallocFunction(newsize * sizeof(ScalarType)));
214  if (this->MallocFunction == malloc)
215  {
216  // This must be done because the array passed in may have been
217  // allocated outside of the memory management of `vtkBuffer` and
218  // therefore have been registered with a `DeleteFunction` such as
219  // `delete` or `delete[]`. Since the memory is now allocated with
220  // `malloc` here, we must also reset `DeleteFunction` to something
221  // which matches.
222  forceFreeFunction = true;
223  }
224  }
225  else
226  {
227  newArray = static_cast<ScalarType*>(malloc(newsize * sizeof(ScalarType)));
228  }
229  if (!newArray)
230  {
231  return false;
232  }
233  std::copy(this->Pointer, this->Pointer + std::min(this->Size, newsize), newArray);
234  // now save the new array and release the old one too.
235  this->SetBuffer(newArray, newsize);
236  if (!this->MallocFunction || forceFreeFunction)
237  {
238  this->DeleteFunction = free;
239  }
240  }
241  else
242  {
243  // Try to reallocate with minimal memory usage and possibly avoid
244  // copying.
245  ScalarType* newArray = nullptr;
246  if (this->ReallocFunction)
247  {
248  newArray = static_cast<ScalarType*>(
249  this->ReallocFunction(this->Pointer, newsize * sizeof(ScalarType)));
250  }
251  else
252  {
253  newArray = static_cast<ScalarType*>(realloc(this->Pointer, newsize * sizeof(ScalarType)));
254  }
255  if (!newArray)
256  {
257  return false;
258  }
259  this->Pointer = newArray;
260  this->Size = newsize;
261  }
262  return true;
263 }
264 
265 #endif
266 // VTK-HeaderTest-Exclude: vtkBuffer.h
static vtkBuffer< ScalarTypeT > * New()
Definition: vtkBuffer.h:114
static vtkBuffer< ScalarTypeT > * ExtendedNew()
Definition: vtkBuffer.h:120
void SetReallocFunction(vtkReallocingFunction reallocFunction=realloc)
Set the realloc function to be used when allocating space inside this object.
Definition: vtkBuffer.h:148
abstract base class for most VTK objects
Definition: vtkObject.h:62
vtkFreeingFunction DeleteFunction
Definition: vtkBuffer.h:106
void(* vtkFreeingFunction)(void *)
Definition: vtkObjectBase.h:71
vtkIdType GetSize() const
Return the number of elements the current buffer can hold.
Definition: vtkBuffer.h:77
int vtkIdType
Definition: vtkType.h:332
A class to help modify and restore the global UsingMemkind state, like SetUsingMemkind(newValue), but safer.
void SetMallocFunction(vtkMallocingFunction mallocFunction=malloc)
Set the malloc function to be used when allocating space inside this object.
Definition: vtkBuffer.h:142
static vtkReallocingFunction GetCurrentReallocFunction()
void SetFreeFunction(bool noFreeFunction, vtkFreeingFunction deleteFunction=free)
Set the free function to be used when releasing this object.
Definition: vtkBuffer.h:155
vtkMallocingFunction MallocFunction
Definition: vtkBuffer.h:104
internal storage class used by vtkSOADataArrayTemplate, vtkAOSDataArrayTemplate, and others...
Definition: vtkBuffer.h:34
ScalarType * GetBuffer()
Access the buffer as a scalar pointer.
Definition: vtkBuffer.h:46
ScalarType * Pointer
Definition: vtkBuffer.h:102
void SetBuffer(ScalarType *array, vtkIdType size)
Set the memory buffer that this vtkBuffer object will manage.
Definition: vtkBuffer.h:128
ScalarTypeT ScalarType
Definition: vtkBuffer.h:38
void *(* vtkMallocingFunction)(size_t)
Definition: vtkObjectBase.h:69
#define VTK_STANDARD_NEW_BODY(thisClass)
bool Reallocate(vtkIdType newsize)
Allocate a new buffer that holds newsize elements.
Definition: vtkBuffer.h:200
vtkBuffer()
Definition: vtkBuffer.h:91
bool Allocate(vtkIdType size)
Allocate a new buffer that holds size elements.
Definition: vtkBuffer.h:169
static vtkFreeingFunction GetCurrentFreeFunction()
const ScalarType * GetBuffer() const
Definition: vtkBuffer.h:47
~vtkBuffer() override
Definition: vtkBuffer.h:100
void *(* vtkReallocingFunction)(void *, size_t)
Definition: vtkObjectBase.h:70
vtkTemplateTypeMacro(vtkBuffer< ScalarTypeT >, vtkObject)
vtkReallocingFunction ReallocFunction
Definition: vtkBuffer.h:105
static vtkMallocingFunction GetCurrentMallocFunction()
vtkIdType Size
Definition: vtkBuffer.h:103