VTK  9.6.20260211
vtkBuffer.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
18
19#ifndef vtkBuffer_h
20#define vtkBuffer_h
21
22#include "vtkAbstractBuffer.h"
23#include "vtkObjectFactory.h" // New() implementation
24#include "vtkTypeTraits.h" // For vtkTypeTraits
25
26#include <algorithm> // for std::min and std::copy
27
28VTK_ABI_NAMESPACE_BEGIN
29template <class ScalarTypeT>
31{
32public:
34 typedef ScalarTypeT ScalarType;
35
38
42 ScalarType* GetBuffer() { return this->Pointer; }
43 const ScalarType* GetBuffer() const { return this->Pointer; }
44
50 void SetBuffer(ScalarType* array, vtkIdType size);
51
55 void SetMallocFunction(vtkMallocingFunction mallocFunction = malloc);
56
60 void SetReallocFunction(vtkReallocingFunction reallocFunction = realloc);
61
68 void SetFreeFunction(bool noFreeFunction, vtkFreeingFunction deleteFunction = free);
69
73 vtkIdType GetSize() const { return this->Size; }
74
76
79 void* GetVoidBuffer() override { return this->Pointer; }
80 vtkIdType GetNumberOfElements() const override { return this->Size; }
81 int GetDataType() const override { return vtkTypeTraits<ScalarType>::VTKTypeID(); }
82 int GetDataTypeSize() const override { return sizeof(ScalarType); }
84
88 bool Allocate(vtkIdType size);
89
94 bool Reallocate(vtkIdType newsize);
95
96protected:
105
106 ~vtkBuffer() override { this->SetBuffer(nullptr, 0); }
107
108 ScalarType* Pointer;
113
114private:
115 vtkBuffer(const vtkBuffer&) = delete;
116 void operator=(const vtkBuffer&) = delete;
117};
118
119template <class ScalarT>
124
125template <class ScalarT>
127{
128 auto mkhold = vtkMemkindRAII(true);
130}
131
132//------------------------------------------------------------------------------
133template <typename ScalarT>
135{
136 if (this->Pointer != array)
137 {
138 if (this->DeleteFunction)
139 {
140 this->DeleteFunction(this->Pointer);
141 }
142 this->Pointer = array;
143 }
144 this->Size = sz;
145}
146//------------------------------------------------------------------------------
147template <typename ScalarT>
149{
150 this->MallocFunction = mallocFunction;
151}
152//------------------------------------------------------------------------------
153template <typename ScalarT>
155{
156 this->ReallocFunction = reallocFunction;
157}
158
159//------------------------------------------------------------------------------
160template <typename ScalarT>
161void vtkBuffer<ScalarT>::SetFreeFunction(bool noFreeFunction, vtkFreeingFunction deleteFunction)
162{
163 if (noFreeFunction)
164 {
165 this->DeleteFunction = nullptr;
166 }
167 else
168 {
169 this->DeleteFunction = deleteFunction;
170 }
171}
172
173//------------------------------------------------------------------------------
174template <typename ScalarT>
176{
177 // release old memory.
178 this->SetBuffer(nullptr, 0);
179 if (size > 0)
180 {
181 ScalarType* newArray;
182 if (this->MallocFunction)
183 {
184 newArray = static_cast<ScalarType*>(this->MallocFunction(size * sizeof(ScalarType)));
185 }
186 else
187 {
188 newArray = static_cast<ScalarType*>(malloc(size * sizeof(ScalarType)));
189 }
190 if (newArray)
191 {
192 this->SetBuffer(newArray, size);
193 if (!this->MallocFunction)
194 {
195 this->DeleteFunction = free;
196 }
197 return true;
198 }
199 return false;
200 }
201 return true; // size == 0
202}
203
204//------------------------------------------------------------------------------
205template <typename ScalarT>
207{
208 if (newsize == 0)
209 {
210 return this->Allocate(0);
211 }
212
213 if (this->Pointer && this->DeleteFunction != free)
214 {
215 ScalarType* newArray;
216 bool forceFreeFunction = false;
217 if (this->MallocFunction)
218 {
219 newArray = static_cast<ScalarType*>(this->MallocFunction(newsize * sizeof(ScalarType)));
220 if (this->MallocFunction == malloc)
221 {
222 // This must be done because the array passed in may have been
223 // allocated outside of the memory management of `vtkBuffer` and
224 // therefore have been registered with a `DeleteFunction` such as
225 // `delete` or `delete[]`. Since the memory is now allocated with
226 // `malloc` here, we must also reset `DeleteFunction` to something
227 // which matches.
228 forceFreeFunction = true;
229 }
230 }
231 else
232 {
233 newArray = static_cast<ScalarType*>(malloc(newsize * sizeof(ScalarType)));
234 }
235 if (!newArray)
236 {
237 return false;
238 }
239 std::copy(this->Pointer, this->Pointer + (std::min)(this->Size, newsize), newArray);
240 // now save the new array and release the old one too.
241 this->SetBuffer(newArray, newsize);
242 if (!this->MallocFunction || forceFreeFunction)
243 {
244 this->DeleteFunction = free;
245 }
246 }
247 else
248 {
249 // Try to reallocate with minimal memory usage and possibly avoid
250 // copying.
251 ScalarType* newArray = nullptr;
252 if (this->ReallocFunction)
253 {
254 newArray = static_cast<ScalarType*>(
255 this->ReallocFunction(this->Pointer, newsize * sizeof(ScalarType)));
256 }
257 else
258 {
259 newArray = static_cast<ScalarType*>(realloc(this->Pointer, newsize * sizeof(ScalarType)));
260 }
261 if (!newArray)
262 {
263 return false;
264 }
265 this->Pointer = newArray;
266 this->Size = newsize;
267 }
268 return true;
269}
270
271VTK_ABI_NAMESPACE_END
272#endif
273// VTK-HeaderTest-Exclude: vtkBuffer.h
vtkAbstractBuffer()=default
bool Reallocate(vtkIdType newsize)
Allocate a new buffer that holds newsize elements.
Definition vtkBuffer.h:206
ScalarType * Pointer
Definition vtkBuffer.h:108
ScalarType * GetBuffer()
Access the buffer as a scalar pointer.
Definition vtkBuffer.h:42
vtkFreeingFunction DeleteFunction
Definition vtkBuffer.h:112
vtkMallocingFunction MallocFunction
Definition vtkBuffer.h:110
const ScalarType * GetBuffer() const
Definition vtkBuffer.h:43
void * GetVoidBuffer() override
vtkAbstractBuffer interface implementation for Python buffer protocol support.
Definition vtkBuffer.h:79
vtkIdType Size
Definition vtkBuffer.h:109
vtkTemplateTypeMacro(vtkBuffer< ScalarTypeT >, vtkAbstractBuffer)
void SetReallocFunction(vtkReallocingFunction reallocFunction=realloc)
Set the realloc function to be used when allocating space inside this object.
Definition vtkBuffer.h:154
ScalarTypeT ScalarType
Definition vtkBuffer.h:34
static vtkBuffer< ScalarTypeT > * ExtendedNew()
Definition vtkBuffer.h:126
void SetMallocFunction(vtkMallocingFunction mallocFunction=malloc)
Set the malloc function to be used when allocating space inside this object.
Definition vtkBuffer.h:148
vtkIdType GetSize() const
Return the number of elements the current buffer can hold.
Definition vtkBuffer.h:73
int GetDataType() const override
vtkAbstractBuffer interface implementation for Python buffer protocol support.
Definition vtkBuffer.h:81
~vtkBuffer() override
Definition vtkBuffer.h:106
void SetFreeFunction(bool noFreeFunction, vtkFreeingFunction deleteFunction=free)
Set the free function to be used when releasing this object.
Definition vtkBuffer.h:161
int GetDataTypeSize() const override
vtkAbstractBuffer interface implementation for Python buffer protocol support.
Definition vtkBuffer.h:82
vtkReallocingFunction ReallocFunction
Definition vtkBuffer.h:111
bool Allocate(vtkIdType size)
Allocate a new buffer that holds size elements.
Definition vtkBuffer.h:175
static vtkBuffer< ScalarTypeT > * New()
Definition vtkBuffer.h:120
vtkIdType GetNumberOfElements() const override
vtkAbstractBuffer interface implementation for Python buffer protocol support.
Definition vtkBuffer.h:80
void SetBuffer(ScalarType *array, vtkIdType size)
Set the memory buffer that this vtkBuffer object will manage.
Definition vtkBuffer.h:134
A class to help modify and restore the global UsingMemkind state, like SetUsingMemkind(newValue),...
static vtkFreeingFunction GetCurrentFreeFunction()
static vtkMallocingFunction GetCurrentMallocFunction()
static vtkReallocingFunction GetCurrentReallocFunction()
Template defining traits of native types used by VTK.
void *(* vtkMallocingFunction)(size_t)
void *(* vtkReallocingFunction)(void *, size_t)
void(* vtkFreeingFunction)(void *)
#define VTK_STANDARD_NEW_BODY(thisClass)
int vtkIdType
Definition vtkType.h:354