From 133816b7ffee7d9d8d8a30e8c9a72a4630e82c00 Mon Sep 17 00:00:00 2001
From: "David C. Lonie" <david.lonie@kitware.com>
Date: Thu, 3 Mar 2016 11:08:54 -0500
Subject: [PATCH] Install and document the vtkBuffer header.

---
 Common/Core/CMakeLists.txt |   2 +
 Common/Core/vtkBuffer.h    | 205 +++++++++++++++++++++----------------
 2 files changed, 120 insertions(+), 87 deletions(-)

diff --git a/Common/Core/CMakeLists.txt b/Common/Core/CMakeLists.txt
index de22571f828..ed144345209 100644
--- a/Common/Core/CMakeLists.txt
+++ b/Common/Core/CMakeLists.txt
@@ -290,6 +290,7 @@ set(${vtk-module}_HDRS
   vtkAtomicTypeConcepts.h
   vtkAtomicTypes.h
   vtkAutoInit.h
+  vtkBuffer.h
   vtkDataArrayAccessor.h
   vtkDataArrayIteratorMacro.h
   vtkDataArrayTemplate.h
@@ -587,6 +588,7 @@ set_source_files_properties(
   vtkArrayRange.cxx
   vtkArraySort.cxx
   vtkArrayWeights.cxx
+  vtkBuffer.h
   vtkBoundingBox.cxx
   vtkBreakPoint.cxx
   vtkCallbackCommand.cxx
diff --git a/Common/Core/vtkBuffer.h b/Common/Core/vtkBuffer.h
index a61d3f9fa19..cdf87bef0c1 100644
--- a/Common/Core/vtkBuffer.h
+++ b/Common/Core/vtkBuffer.h
@@ -31,7 +31,7 @@ class vtkBuffer : public vtkObject
 public:
   vtkTypeMacro(vtkBuffer, vtkObject)
   typedef ScalarTypeT ScalarType;
-  enum DeleteMethod
+  enum
     {
     VTK_DATA_ARRAY_FREE,
     VTK_DATA_ARRAY_DELETE
@@ -39,93 +39,32 @@ public:
 
   static vtkBuffer<ScalarTypeT>* New();
 
-  inline ScalarType* GetBuffer() const
-    { return this->Pointer; }
+  // Description:
+  // Access the buffer as a scalar pointer.
+  inline ScalarType* GetBuffer() { return this->Pointer; }
+  inline const ScalarType* GetBuffer() const { return this->Pointer; }
+
+  // Description:
+  // 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 bufffer (in
+  // number of elements). If @a save is true, the buffer will not be freed when
+  // this vtkBuffer object is deleted or resize -- otherwise, @a deleteMethod
+  // specifies how the buffer will be freed.
   void SetBuffer(ScalarType* array, vtkIdType size, bool save=false,
-                 int deleteMethod=VTK_DATA_ARRAY_FREE)
-    {
-    if (this->Pointer != array)
-      {
-      if (!this->Save)
-        {
-        if (this->DeleteMethod == VTK_DATA_ARRAY_FREE)
-          {
-          free(this->Pointer);
-          }
-        else
-          {
-          delete [] this->Pointer;
-          }
-        }
-      this->Pointer = array;
-      }
-    this->Size = size;
-    this->Save = save;
-    this->DeleteMethod = deleteMethod;
-    }
+                 int deleteMethod=VTK_DATA_ARRAY_FREE);
 
+  // Description:
+  // Return the number of elements the current buffer can hold.
   inline vtkIdType GetSize() const { return this->Size; }
 
-  bool Allocate(vtkIdType size)
-    {
-    // release old memory.
-    this->SetBuffer(NULL, 0);
-    if (size > 0)
-      {
-      ScalarType* newArray =
-          static_cast<ScalarType*>(malloc(size * sizeof(ScalarType)));
-      if (newArray)
-        {
-        this->SetBuffer(newArray, size, false, VTK_DATA_ARRAY_FREE);
-        return true;
-        }
-      return false;
-      }
-    return true; // size == 0
-    }
-
-  bool Reallocate(vtkIdType newsize)
-    {
-    if (newsize == 0) { return this->Allocate(0); }
-
-    // OS X's realloc does not free memory if the new block is smaller.  This
-    // is a very serious problem and causes huge amount of memory to be
-    // wasted. Do not use realloc on the Mac.
-    bool dontUseRealloc=false;
-#if defined __APPLE__
-    dontUseRealloc=true;
-#endif
+  // Description:
+  // Allocate a new buffer that holds @a size elements. Old data is not saved.
+  bool Allocate(vtkIdType size);
 
-    if (this->Pointer &&
-      (this->Save || this->DeleteMethod == VTK_DATA_ARRAY_DELETE ||
-       dontUseRealloc))
-      {
-      ScalarType* 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, false, VTK_DATA_ARRAY_FREE);
-      }
-    else
-      {
-      // Try to reallocate with minimal memory usage and possibly avoid
-      // copying.
-      ScalarType* newArray = static_cast<ScalarType*>(
-            realloc(this->Pointer, newsize * sizeof(ScalarType)));
-      if (!newArray)
-        {
-        return false;
-        }
-      this->Pointer = newArray;
-      this->Size = newsize;
-      }
-    return true;
-    }
+  // Description:
+  // Allocate a new buffer that holds @a newsize elements. Old data is
+  // preserved.
+  bool Reallocate(vtkIdType newsize);
 
 protected:
   vtkBuffer()
@@ -133,18 +72,19 @@ protected:
       Size(0),
       Save(false),
       DeleteMethod(VTK_DATA_ARRAY_FREE)
-    {
-    }
+  {
+  }
 
   ~vtkBuffer()
-    {
+  {
     this->SetBuffer(NULL, 0);
-    }
+  }
 
   ScalarType *Pointer;
   vtkIdType Size;
   bool Save;
   int DeleteMethod;
+
 private:
   vtkBuffer(const vtkBuffer&);  // Not implemented.
   void operator=(const vtkBuffer&);  // Not implemented.
@@ -156,5 +96,96 @@ inline vtkBuffer<ScalarT> *vtkBuffer<ScalarT>::New()
   VTK_STANDARD_NEW_BODY(vtkBuffer<ScalarT>)
 }
 
+//------------------------------------------------------------------------------
+template <typename ScalarT>
+void vtkBuffer<ScalarT>::SetBuffer(
+    typename vtkBuffer<ScalarT>::ScalarType *array,
+    vtkIdType size, bool save, int deleteMethod)
+{
+  if (this->Pointer != array)
+    {
+    if (!this->Save)
+      {
+      if (this->DeleteMethod == VTK_DATA_ARRAY_FREE)
+        {
+        free(this->Pointer);
+        }
+      else
+        {
+        delete [] this->Pointer;
+        }
+      }
+    this->Pointer = array;
+    }
+  this->Size = size;
+  this->Save = save;
+  this->DeleteMethod = deleteMethod;
+}
+
+//------------------------------------------------------------------------------
+template <typename ScalarT>
+bool vtkBuffer<ScalarT>::Allocate(vtkIdType size)
+{
+  // release old memory.
+  this->SetBuffer(NULL, 0);
+  if (size > 0)
+    {
+    ScalarType* newArray =
+        static_cast<ScalarType*>(malloc(size * sizeof(ScalarType)));
+    if (newArray)
+      {
+      this->SetBuffer(newArray, size, false, VTK_DATA_ARRAY_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); }
+
+  // OS X's realloc does not free memory if the new block is smaller.  This
+  // is a very serious problem and causes huge amount of memory to be
+  // wasted. Do not use realloc on the Mac.
+  bool dontUseRealloc=false;
+#if defined __APPLE__
+  dontUseRealloc=true;
+#endif
+
+  if (this->Pointer &&
+      (this->Save || this->DeleteMethod == VTK_DATA_ARRAY_DELETE ||
+       dontUseRealloc))
+    {
+    ScalarType* 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, false, VTK_DATA_ARRAY_FREE);
+    }
+  else
+    {
+    // Try to reallocate with minimal memory usage and possibly avoid
+    // copying.
+    ScalarType* newArray = static_cast<ScalarType*>(
+          realloc(this->Pointer, newsize * sizeof(ScalarType)));
+    if (!newArray)
+      {
+      return false;
+      }
+    this->Pointer = newArray;
+    this->Size = newsize;
+    }
+  return true;
+}
+
 #endif
 // VTK-HeaderTest-Exclude: vtkBuffer.h
-- 
GitLab