File: vtkBuffer.h

package info (click to toggle)
paraview 5.11.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 497,236 kB
  • sloc: cpp: 3,171,290; ansic: 1,315,072; python: 134,290; xml: 103,324; sql: 65,887; sh: 5,286; javascript: 4,901; yacc: 4,383; java: 3,977; perl: 2,363; lex: 1,909; f90: 1,255; objc: 143; makefile: 119; tcl: 59; pascal: 50; fortran: 29
file content (268 lines) | stat: -rw-r--r-- 7,739 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkBuffer.h

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
/**
 * @class   vtkBuffer
 * @brief   internal storage class used by vtkSOADataArrayTemplate,
 * vtkAOSDataArrayTemplate, and others.
 *
 * vtkBuffer makes it easier to keep data pointers in vtkDataArray subclasses.
 * This is an internal class and not intended for direct use expect when writing
 * new types of vtkDataArray subclasses.
 */

#ifndef vtkBuffer_h
#define vtkBuffer_h

#include "vtkObject.h"
#include "vtkObjectFactory.h" // New() implementation

#include <algorithm> // for std::min and std::copy

VTK_ABI_NAMESPACE_BEGIN
template <class ScalarTypeT>
class vtkBuffer : public vtkObject
{
public:
  vtkTemplateTypeMacro(vtkBuffer<ScalarTypeT>, vtkObject);
  typedef ScalarTypeT ScalarType;

  static vtkBuffer<ScalarTypeT>* New();
  static vtkBuffer<ScalarTypeT>* ExtendedNew();

  /**
   * Access the buffer as a scalar pointer.
   */
  inline ScalarType* GetBuffer() { return this->Pointer; }
  inline const ScalarType* GetBuffer() const { return this->Pointer; }

  /**
   * Set the memory buffer that this vtkBuffer object will manage. @a array
   * is a pointer to the buffer data and @a size is the size of the buffer (in
   * number of elements).
   */
  void SetBuffer(ScalarType* array, vtkIdType size);

  /**
   * Set the malloc function to be used when allocating space inside this object.
   **/
  void SetMallocFunction(vtkMallocingFunction mallocFunction = malloc);

  /**
   * Set the realloc function to be used when allocating space inside this object.
   **/
  void SetReallocFunction(vtkReallocingFunction reallocFunction = realloc);

  /**
   * Set the free function to be used when releasing this object.
   * If @a noFreeFunction is true, the buffer will not be freed when
   * this vtkBuffer object is deleted or resize -- otherwise, @a deleteFunction
   * will be called to free the buffer
   **/
  void SetFreeFunction(bool noFreeFunction, vtkFreeingFunction deleteFunction = free);

  /**
   * Return the number of elements the current buffer can hold.
   */
  inline vtkIdType GetSize() const { return this->Size; }

  /**
   * Allocate a new buffer that holds @a size elements. Old data is not saved.
   */
  bool Allocate(vtkIdType size);

  /**
   * Allocate a new buffer that holds @a newsize elements. Old data is
   * preserved.
   */
  bool Reallocate(vtkIdType newsize);

protected:
  vtkBuffer()
    : Pointer(nullptr)
    , Size(0)
  {
    this->SetMallocFunction(vtkObjectBase::GetCurrentMallocFunction());
    this->SetReallocFunction(vtkObjectBase::GetCurrentReallocFunction());
    this->SetFreeFunction(false, vtkObjectBase::GetCurrentFreeFunction());
  }

  ~vtkBuffer() override { this->SetBuffer(nullptr, 0); }

  ScalarType* Pointer;
  vtkIdType Size;
  vtkMallocingFunction MallocFunction;
  vtkReallocingFunction ReallocFunction;
  vtkFreeingFunction DeleteFunction;

private:
  vtkBuffer(const vtkBuffer&) = delete;
  void operator=(const vtkBuffer&) = delete;
};

template <class ScalarT>
inline vtkBuffer<ScalarT>* vtkBuffer<ScalarT>::New()
{
  VTK_STANDARD_NEW_BODY(vtkBuffer<ScalarT>);
}

template <class ScalarT>
inline vtkBuffer<ScalarT>* vtkBuffer<ScalarT>::ExtendedNew()
{
  auto mkhold = vtkMemkindRAII(true);
  return vtkBuffer<ScalarT>::New();
}

//------------------------------------------------------------------------------
template <typename ScalarT>
void vtkBuffer<ScalarT>::SetBuffer(typename vtkBuffer<ScalarT>::ScalarType* array, vtkIdType size)
{
  if (this->Pointer != array)
  {
    if (this->DeleteFunction)
    {
      this->DeleteFunction(this->Pointer);
    }
    this->Pointer = array;
  }
  this->Size = size;
}
//------------------------------------------------------------------------------
template <typename ScalarT>
void vtkBuffer<ScalarT>::SetMallocFunction(vtkMallocingFunction mallocFunction)
{
  this->MallocFunction = mallocFunction;
}
//------------------------------------------------------------------------------
template <typename ScalarT>
void vtkBuffer<ScalarT>::SetReallocFunction(vtkReallocingFunction reallocFunction)
{
  this->ReallocFunction = reallocFunction;
}

//------------------------------------------------------------------------------
template <typename ScalarT>
void vtkBuffer<ScalarT>::SetFreeFunction(bool noFreeFunction, vtkFreeingFunction deleteFunction)
{
  if (noFreeFunction)
  {
    this->DeleteFunction = nullptr;
  }
  else
  {
    this->DeleteFunction = deleteFunction;
  }
}

//------------------------------------------------------------------------------
template <typename ScalarT>
bool vtkBuffer<ScalarT>::Allocate(vtkIdType size)
{
  // release old memory.
  this->SetBuffer(nullptr, 0);
  if (size > 0)
  {
    ScalarType* newArray;
    if (this->MallocFunction)
    {
      newArray = static_cast<ScalarType*>(this->MallocFunction(size * sizeof(ScalarType)));
    }
    else
    {
      newArray = static_cast<ScalarType*>(malloc(size * sizeof(ScalarType)));
    }
    if (newArray)
    {
      this->SetBuffer(newArray, size);
      if (!this->MallocFunction)
      {
        this->DeleteFunction = free;
      }
      return true;
    }
    return false;
  }
  return true; // size == 0
}

//------------------------------------------------------------------------------
template <typename ScalarT>
bool vtkBuffer<ScalarT>::Reallocate(vtkIdType newsize)
{
  if (newsize == 0)
  {
    return this->Allocate(0);
  }

  if (this->Pointer && this->DeleteFunction != free)
  {
    ScalarType* newArray;
    bool forceFreeFunction = false;
    if (this->MallocFunction)
    {
      newArray = static_cast<ScalarType*>(this->MallocFunction(newsize * sizeof(ScalarType)));
      if (this->MallocFunction == malloc)
      {
        // This must be done because the array passed in may have been
        // allocated outside of the memory management of `vtkBuffer` and
        // therefore have been registered with a `DeleteFunction` such as
        // `delete` or `delete[]`. Since the memory is now allocated with
        // `malloc` here, we must also reset `DeleteFunction` to something
        // which matches.
        forceFreeFunction = true;
      }
    }
    else
    {
      newArray = static_cast<ScalarType*>(malloc(newsize * sizeof(ScalarType)));
    }
    if (!newArray)
    {
      return false;
    }
    std::copy(this->Pointer, this->Pointer + (std::min)(this->Size, newsize), newArray);
    // now save the new array and release the old one too.
    this->SetBuffer(newArray, newsize);
    if (!this->MallocFunction || forceFreeFunction)
    {
      this->DeleteFunction = free;
    }
  }
  else
  {
    // Try to reallocate with minimal memory usage and possibly avoid
    // copying.
    ScalarType* newArray = nullptr;
    if (this->ReallocFunction)
    {
      newArray = static_cast<ScalarType*>(
        this->ReallocFunction(this->Pointer, newsize * sizeof(ScalarType)));
    }
    else
    {
      newArray = static_cast<ScalarType*>(realloc(this->Pointer, newsize * sizeof(ScalarType)));
    }
    if (!newArray)
    {
      return false;
    }
    this->Pointer = newArray;
    this->Size = newsize;
  }
  return true;
}

VTK_ABI_NAMESPACE_END
#endif
// VTK-HeaderTest-Exclude: vtkBuffer.h