0024044: Performance improvements: Foundation Classes (math)
[occt.git] / src / math / math_DoubleTab.gxx
index be1bb36..083ddb4 100755 (executable)
@@ -1,4 +1,21 @@
-// File math_DoubleTab.gxx
+// Copyright (c) 1997-1999 Matra Datavision
+// Copyright (c) 1999-2012 OPEN CASCADE SAS
+//
+// The content of this file is subject to the Open CASCADE Technology Public
+// License Version 6.5 (the "License"). You may not use the content of this file
+// except in compliance with the License. Please obtain a copy of the License
+// at http://www.opencascade.org and read it completely before using this file.
+//
+// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
+// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
+//
+// The Original Code and all software distributed under the License is
+// distributed on an "AS IS" basis, without warranty of any kind, and the
+// Initial Developer hereby disclaims all such warranties, including without
+// limitation, any warranties of merchantability, fitness for a particular
+// purpose or non-infringement. Please see the License for the specific terms
+// and conditions governing the rights and limitations under the License.
+
 // Lpa, le 7/02/92
 
 
 #include <Standard_Failure.hxx>
 #include <Standard_Integer.hxx>
 
+// macro to get size of C array
+#define CARRAY_LENGTH(arr) (sizeof(arr)/sizeof(arr[0]))
+
 void math_DoubleTab::Allocate()
 {
   Standard_Integer RowNumber = UppR - LowR + 1;
   Standard_Integer ColNumber = UppC - LowC + 1;
 
-  Item** TheAddr = (Item**) Standard::Allocate(RowNumber * sizeof(Item*));
+  Item** TheAddr = !isAddrAllocated? (Item**)&AddrBuf : 
+    (Item**) Standard::Allocate(RowNumber * sizeof(Item*));
   Item* Address;
   if(isAllocated) 
     Address = (Item*) Standard::Allocate(RowNumber * ColNumber * sizeof(Item));
@@ -33,7 +54,9 @@ math_DoubleTab::math_DoubleTab(const Standard_Integer LowerRow,
                               const Standard_Integer UpperRow,
                               const Standard_Integer LowerCol,
                               const Standard_Integer UpperCol) :
-  isAllocated(Standard_True),
+  Addr(Buf),
+  isAddrAllocated(UpperRow - LowerRow + 1 > CARRAY_LENGTH(AddrBuf)),
+  isAllocated((UpperRow - LowerRow + 1) * (UpperCol - LowerCol + 1) > CARRAY_LENGTH(Buf)),
   LowR(LowerRow),
   UppR(UpperRow),
   LowC(LowerCol),
@@ -49,6 +72,7 @@ math_DoubleTab::math_DoubleTab(const Item& Tab,
                               const Standard_Integer LowerCol,
                               const Standard_Integer UpperCol) :
   Addr((void *) &Tab),
+  isAddrAllocated(UpperRow - LowerRow + 1 > CARRAY_LENGTH(AddrBuf)),
   isAllocated(Standard_False),
   LowR(LowerRow),
   UppR(UpperRow),
@@ -70,7 +94,10 @@ void math_DoubleTab::Init(const Item& InitValue)
 
 
 math_DoubleTab::math_DoubleTab(const math_DoubleTab& Other) :
-  isAllocated(Standard_True),
+  Addr(Buf),
+  isAddrAllocated(Other.UppR - Other.LowR + 1 > CARRAY_LENGTH(AddrBuf)),
+  isAllocated((Other.UppR - Other.LowR + 1) * 
+              (Other.UppC - Other.LowC + 1) > CARRAY_LENGTH(Buf)),
   LowR(Other.LowR),
   UppR(Other.UppR),
   LowC(Other.LowC),
@@ -95,8 +122,10 @@ void math_DoubleTab::Free()
     Standard::Free(it);
   }
   // free the pointers
-  Standard_Address it = (Standard_Address)(((Item**)Addr) + LowR);
-  Standard::Free (it);
+  if(isAddrAllocated) {
+    Standard_Address it = (Standard_Address)(((Item**)Addr) + LowR);
+    Standard::Free (it);
+  }
   Addr = 0;
 }