0024081: Memory corruption when projecting point on surface of revolution
[occt.git] / src / math / math_DoubleTab.gxx
CommitLineData
b311480e 1// Copyright (c) 1997-1999 Matra Datavision
2// Copyright (c) 1999-2012 OPEN CASCADE SAS
3//
4// The content of this file is subject to the Open CASCADE Technology Public
5// License Version 6.5 (the "License"). You may not use the content of this file
6// except in compliance with the License. Please obtain a copy of the License
7// at http://www.opencascade.org and read it completely before using this file.
8//
9// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11//
12// The Original Code and all software distributed under the License is
13// distributed on an "AS IS" basis, without warranty of any kind, and the
14// Initial Developer hereby disclaims all such warranties, including without
15// limitation, any warranties of merchantability, fitness for a particular
16// purpose or non-infringement. Please see the License for the specific terms
17// and conditions governing the rights and limitations under the License.
18
7fd59977 19// Lpa, le 7/02/92
20
21
22#include <math_Memory.hxx>
23#include <Standard_OutOfRange.hxx>
24#include <Standard_Failure.hxx>
25#include <Standard_Integer.hxx>
26
27void math_DoubleTab::Allocate()
28{
29 Standard_Integer RowNumber = UppR - LowR + 1;
30 Standard_Integer ColNumber = UppC - LowC + 1;
31
32 Item** TheAddr = (Item**) Standard::Allocate(RowNumber * sizeof(Item*));
33 Item* Address;
34 if(isAllocated)
35 Address = (Item*) Standard::Allocate(RowNumber * ColNumber * sizeof(Item));
36 else
37 Address = (Item*) Addr;
38 Address -= LowC;
39
40 for (Standard_Integer Index = 0; Index < RowNumber; Index++) {
41 TheAddr[Index] = Address;
42 Address += ColNumber;
43 }
44
45 TheAddr -= LowR;
46 Addr = (Standard_Address) TheAddr;
47}
48
49math_DoubleTab::math_DoubleTab(const Standard_Integer LowerRow,
50 const Standard_Integer UpperRow,
51 const Standard_Integer LowerCol,
52 const Standard_Integer UpperCol) :
53 isAllocated(Standard_True),
54 LowR(LowerRow),
55 UppR(UpperRow),
56 LowC(LowerCol),
57 UppC(UpperCol)
58{
59 Allocate();
60}
61
62
63math_DoubleTab::math_DoubleTab(const Item& Tab,
64 const Standard_Integer LowerRow,
65 const Standard_Integer UpperRow,
66 const Standard_Integer LowerCol,
67 const Standard_Integer UpperCol) :
68 Addr((void *) &Tab),
69 isAllocated(Standard_False),
70 LowR(LowerRow),
71 UppR(UpperRow),
72 LowC(LowerCol),
73 UppC(UpperCol)
74{
75 Allocate();
76}
77
78void math_DoubleTab::Init(const Item& InitValue)
79{
80 for (Standard_Integer i = LowR; i <= UppR; i++) {
81 for (Standard_Integer j = LowC; j <= UppC; j++) {
82 ((Item**) Addr)[i][j] = InitValue;
83 }
84 }
85}
86
87
88
89math_DoubleTab::math_DoubleTab(const math_DoubleTab& Other) :
90 isAllocated(Standard_True),
91 LowR(Other.LowR),
92 UppR(Other.UppR),
93 LowC(Other.LowC),
94 UppC(Other.UppC)
95{
96 Allocate();
97
98 Standard_Address target = (Standard_Address) &Value(LowR,LowC);
99 Standard_Address source = (Standard_Address) &Other.Value(LowR,LowC);
100
101 memmove(target,source,
102 (int)((UppR - LowR + 1) * (UppC - LowC + 1) * sizeof(Item)));
103
104}
105
106
107void math_DoubleTab::Free()
108{
7fd59977 109 // free the data
110 if(isAllocated) {
111 Standard_Address it = (Standard_Address)&Value(LowR,LowC);
112 Standard::Free(it);
113 }
114 // free the pointers
115 Standard_Address it = (Standard_Address)(((Item**)Addr) + LowR);
116 Standard::Free (it);
117 Addr = 0;
118}
119
120
121
122void math_DoubleTab::SetLowerRow(const Standard_Integer LowerRow)
123{
124 Item** TheAddr = (Item**)Addr;
125 Addr = (Standard_Address) (TheAddr + LowR - LowerRow);
126 UppR = UppR - LowR + LowerRow;
127 LowR = LowerRow;
128}
129
130
131void math_DoubleTab::SetLowerCol(const Standard_Integer LowerCol)
132{
133 Item** TheAddr = (Item**) Addr;
134 for (Standard_Integer Index = LowR; Index <= UppR; Index++) {
135 TheAddr[Index] = TheAddr[Index] + LowC - LowerCol;
136 }
137
138 UppC = UppC - LowC + LowerCol;
139 LowC = LowerCol;
140}
141