修訂 | c800121f2af5510b2cf7ea2fc8ea8547b542a831 (tree) |
---|---|
時間 | 2012-11-14 17:51:32 |
作者 | Mikiya Fujii <mikiya.fujii@gmai...> |
Commiter | Mikiya Fujii |
Refactoring MallocerFreere. ##30083
git-svn-id: https://svn.sourceforge.jp/svnroot/molds/trunk@1118 1136aad2-a195-0410-b898-f5ea1d11b9d8
@@ -29,16 +29,11 @@ public: | ||
29 | 29 | |
30 | 30 | //1d |
31 | 31 | template<typename T> void Malloc(T** matrix, size_t size1) const{ |
32 | - if(*matrix!=NULL){ | |
33 | - return; | |
34 | - } | |
32 | + if(*matrix!=NULL) return; | |
35 | 33 | double requiredMalloc = this->GetMemoryAmount<T>(size1); |
36 | 34 | this->CheckLimitHeap(requiredMalloc); |
37 | - | |
38 | 35 | *matrix = new T[size1]; |
39 | - if(*matrix==NULL){ | |
40 | - throw MolDSException(this->errorMessageMallocFailure); | |
41 | - } | |
36 | + if(*matrix==NULL) throw MolDSException(this->errorMessageMallocFailure); | |
42 | 37 | MallocerFreer::AddCurrentMalloced(requiredMalloc); |
43 | 38 | this->Initialize<T>(*matrix, size1); |
44 | 39 | } |
@@ -50,9 +45,7 @@ public: | ||
50 | 45 | } |
51 | 46 | |
52 | 47 | template<typename T> void Free(T** matrix, size_t size1) const{ |
53 | - if(*matrix==NULL){ | |
54 | - return; | |
55 | - } | |
48 | + if(*matrix==NULL) return; | |
56 | 49 | delete [] *matrix; |
57 | 50 | double freedMalloc = this->GetMemoryAmount<T>(size1); |
58 | 51 | MallocerFreer::AddCurrentMalloced(-1.0*freedMalloc); |
@@ -60,657 +53,286 @@ public: | ||
60 | 53 | } |
61 | 54 | |
62 | 55 | template<typename T> double GetMemoryAmount(size_t size1) const{ |
63 | - double amount = static_cast<double>(sizeof(T)) | |
64 | - *static_cast<double>(size1); | |
65 | - return amount; | |
56 | + return static_cast<double>(sizeof(T))*static_cast<double>(size1); | |
66 | 57 | } |
67 | 58 | |
68 | 59 | // 2d |
69 | 60 | template<typename T> void Malloc(T*** matrix, size_t size1, size_t size2) const{ |
70 | - if(*matrix!=NULL){ | |
71 | - return; | |
72 | - } | |
73 | - double requiredMalloc = this->GetMemoryAmount<T>(size1, size2); | |
61 | + if(*matrix!=NULL) return; | |
62 | + double requiredMalloc = this->GetMemoryAmount<T*>(size1); | |
74 | 63 | this->CheckLimitHeap(requiredMalloc); |
75 | 64 | |
76 | 65 | T *p1d=NULL, **p2d=NULL; |
77 | 66 | try{ |
78 | - p1d = new T[size1*size2]; | |
79 | - if(p1d==NULL){ | |
80 | - throw MolDSException(this->errorMessageMallocFailure); | |
81 | - } | |
67 | + this->Malloc<T>(&p1d, size1*size2); | |
82 | 68 | p2d = new T*[size1]; |
83 | - if(p2d==NULL){ | |
84 | - throw MolDSException(this->errorMessageMallocFailure); | |
85 | - } | |
69 | + if(p2d==NULL) throw MolDSException(this->errorMessageMallocFailure); | |
70 | + | |
71 | + for(size_t i=0;i<size1;i++){p2d[i] = &p1d[i*size2];} | |
86 | 72 | |
87 | - for(size_t i=0;i<size1;i++){ | |
88 | - p2d[i] = &p1d[i*size2]; | |
89 | - } | |
90 | - *matrix = p2d; | |
91 | 73 | MallocerFreer::AddCurrentMalloced(requiredMalloc); |
92 | - this->Initialize<T>(*matrix, size1, size2); | |
74 | + this->Initialize<T>(p2d, size1, size2); | |
75 | + *matrix = p2d; | |
93 | 76 | } |
94 | 77 | catch(MolDSException ex){ |
95 | - if(p1d!=NULL){ | |
96 | - delete[] p1d; | |
97 | - } | |
98 | - if(p2d!=NULL){ | |
99 | - delete[] p2d; | |
100 | - } | |
78 | + this->Free<T>(&p1d, size1*size2); | |
79 | + if(p2d!=NULL) delete[] p2d; | |
101 | 80 | throw ex; |
102 | 81 | } |
103 | 82 | } |
104 | 83 | |
105 | 84 | template<typename T> void Initialize(T** matrix, size_t size1, size_t size2) const{ |
106 | 85 | for(size_t i=0;i<size1;i++){ |
107 | - for(size_t j=0;j<size2;j++){ | |
108 | - matrix[i][j] = (T)0.0; | |
109 | - } | |
86 | + this->Initialize<T>(matrix[i], size2); | |
110 | 87 | } |
111 | 88 | } |
112 | 89 | |
113 | 90 | template<typename T> void Free(T*** matrix, size_t size1, size_t size2) const{ |
114 | - if(*matrix==NULL){ | |
115 | - return; | |
116 | - } | |
117 | - delete [] (*matrix)[0]; | |
118 | - delete [] *matrix; | |
119 | - double freedMalloc = this->GetMemoryAmount<T>(size1, size2); | |
91 | + if(*matrix==NULL) return; | |
92 | + T *p1d=NULL, **p2d=NULL; | |
93 | + p2d = *matrix; | |
94 | + p1d = p2d[0]; | |
95 | + delete [] p2d; | |
96 | + this->Free<T>(&p1d, size1*size2); | |
97 | + double freedMalloc = this->GetMemoryAmount<T*>(size1); | |
120 | 98 | MallocerFreer::AddCurrentMalloced(-1.0*freedMalloc); |
121 | 99 | *matrix = NULL; |
122 | 100 | } |
123 | 101 | |
124 | - template<typename T> double GetMemoryAmount(size_t size1, size_t size2) const{ | |
125 | - double amount = static_cast<double>(sizeof(T)) | |
126 | - *static_cast<double>(size1) | |
127 | - *static_cast<double>(size2); | |
128 | - amount += this->GetMemoryAmount<T*>(size1); | |
129 | - return amount; | |
130 | - } | |
131 | - | |
132 | 102 | // 3d |
133 | 103 | template<typename T> void Malloc(T**** matrix, size_t size1, size_t size2, size_t size3) const{ |
134 | - if(*matrix!=NULL){ | |
135 | - return; | |
136 | - } | |
137 | - double requiredMalloc = this->GetMemoryAmount<T>(size1, size2, size3); | |
104 | + if(*matrix!=NULL) return; | |
105 | + double requiredMalloc = this->GetMemoryAmount<T**>(size1); | |
138 | 106 | this->CheckLimitHeap(requiredMalloc); |
139 | 107 | |
140 | - T *p1d=NULL, **p2d=NULL, ***p3d=NULL; | |
108 | + T **p2d=NULL, ***p3d=NULL; | |
141 | 109 | try{ |
142 | - p1d = new T[size1*size2*size3]; | |
143 | - if(p1d==NULL){ | |
144 | - throw MolDSException(this->errorMessageMallocFailure); | |
145 | - } | |
146 | - p2d = new T*[size1*size2]; | |
147 | - if(p2d==NULL){ | |
148 | - throw MolDSException(this->errorMessageMallocFailure); | |
149 | - } | |
110 | + this->Malloc<T>(&p2d, size1*size2, size3); | |
150 | 111 | p3d = new T**[size1]; |
151 | - if(p3d==NULL){ | |
152 | - throw MolDSException(this->errorMessageMallocFailure); | |
153 | - } | |
154 | - | |
155 | - for(size_t i=0;i<size1;i++){ | |
156 | - p3d[i] = &p2d[i*size2]; | |
157 | - for(size_t j=0;j<size2;j++){ | |
158 | - p3d[i][j] = &p1d[i*size2*size3+j*size3]; | |
159 | - } | |
160 | - } | |
161 | - *matrix = p3d; | |
112 | + if(p3d==NULL) throw MolDSException(this->errorMessageMallocFailure); | |
113 | + | |
114 | + for(size_t i=0;i<size1;i++){p3d[i] = &p2d[i*size2]; | |
115 | + for(size_t j=0;j<size2;j++){p3d[i][j] = &p2d[i*size2][j*size3]; | |
116 | + }} | |
117 | + | |
162 | 118 | MallocerFreer::AddCurrentMalloced(requiredMalloc); |
163 | - this->Initialize<T>(*matrix, size1, size2, size3); | |
119 | + this->Initialize<T>(p3d, size1, size2, size3); | |
120 | + *matrix = p3d; | |
164 | 121 | } |
165 | 122 | catch(MolDSException ex){ |
166 | - if(p1d!=NULL){ | |
167 | - delete[] p1d; | |
168 | - } | |
169 | - if(p2d!=NULL){ | |
170 | - delete[] p2d; | |
171 | - } | |
172 | - if(p3d!=NULL){ | |
173 | - delete[] p3d; | |
174 | - } | |
123 | + this->Free<T>(&p2d, size1*size2, size3); | |
124 | + if(p3d!=NULL) delete[] p3d; | |
175 | 125 | throw ex; |
176 | 126 | } |
177 | 127 | } |
178 | 128 | |
179 | 129 | template<typename T> void Initialize(T*** matrix, size_t size1, size_t size2, size_t size3) const{ |
180 | 130 | for(size_t i=0;i<size1;i++) { |
181 | - for(size_t j=0;j<size2;j++){ | |
182 | - for(size_t k=0;k<size3;k++){ | |
183 | - matrix[i][j][k] = (T)0.0; | |
184 | - } | |
185 | - } | |
131 | + this->Initialize<T>(matrix[i], size2, size3); | |
186 | 132 | } |
187 | 133 | } |
188 | 134 | |
189 | 135 | template<typename T> void Free(T**** matrix, size_t size1, size_t size2, size_t size3) const{ |
190 | - if(*matrix==NULL){ | |
191 | - return; | |
192 | - } | |
193 | - T *p1d=NULL, **p2d=NULL, ***p3d=NULL; | |
136 | + if(*matrix==NULL) return; | |
137 | + T **p2d=NULL, ***p3d=NULL; | |
194 | 138 | p3d = *matrix; |
195 | 139 | p2d = p3d[0]; |
196 | - p1d = p2d[0]; | |
197 | 140 | delete [] p3d; |
198 | - delete [] p2d; | |
199 | - delete [] p1d; | |
200 | - double freedMalloc = this->GetMemoryAmount<T>(size1, size2, size3); | |
141 | + this->Free<T>(&p2d, size1*size2, size3); | |
142 | + double freedMalloc = this->GetMemoryAmount<T**>(size1); | |
201 | 143 | MallocerFreer::AddCurrentMalloced(-1.0*freedMalloc); |
202 | 144 | *matrix = NULL; |
203 | 145 | } |
204 | 146 | |
205 | - template<typename T> double GetMemoryAmount(size_t size1, size_t size2, size_t size3) const{ | |
206 | - double amount = static_cast<double>(sizeof(T)) | |
207 | - *static_cast<double>(size1) | |
208 | - *static_cast<double>(size2) | |
209 | - *static_cast<double>(size3); | |
210 | - amount += this->GetMemoryAmount<T*>(size1, size2); | |
211 | - return amount; | |
212 | - } | |
213 | - | |
214 | 147 | //4d |
215 | 148 | template<typename T> void Malloc(T***** matrix, size_t size1, size_t size2, size_t size3, size_t size4) const{ |
216 | - if(*matrix!=NULL){ | |
217 | - return; | |
218 | - } | |
219 | - double requiredMalloc = this->GetMemoryAmount<T>(size1, size2, size3, size4); | |
149 | + if(*matrix!=NULL) return; | |
150 | + double requiredMalloc = this->GetMemoryAmount<T***>(size1); | |
220 | 151 | this->CheckLimitHeap(requiredMalloc); |
221 | 152 | |
222 | - T *p1d=NULL, **p2d=NULL, ***p3d=NULL, ****p4d=NULL; | |
153 | + T ***p3d=NULL, ****p4d=NULL; | |
223 | 154 | try{ |
224 | - p1d = new T[size1*size2*size3*size4]; | |
225 | - if(p1d==NULL){ | |
226 | - throw MolDSException(this->errorMessageMallocFailure); | |
227 | - } | |
228 | - p2d = new T*[size1*size2*size3]; | |
229 | - if(p2d==NULL){ | |
230 | - throw MolDSException(this->errorMessageMallocFailure); | |
231 | - } | |
232 | - p3d = new T**[size1*size2]; | |
233 | - if(p3d==NULL){ | |
234 | - throw MolDSException(this->errorMessageMallocFailure); | |
235 | - } | |
155 | + this->Malloc<T>(&p3d, size1*size2, size3, size4); | |
236 | 156 | p4d = new T***[size1]; |
237 | - if(p4d==NULL){ | |
238 | - throw MolDSException(this->errorMessageMallocFailure); | |
239 | - } | |
240 | - | |
241 | - for(size_t i=0;i<size1;i++){ | |
242 | - p4d[i] = &p3d[i*size2]; | |
243 | - for(size_t j=0;j<size2;j++){ | |
244 | - p4d[i][j] = &p2d[i*size2*size3+j*size3]; | |
245 | - for(size_t k=0;k<size3;k++){ | |
246 | - p4d[i][j][k] = &p1d[i*size2*size3*size4+j*size3*size4+k*size4]; | |
247 | - } | |
248 | - } | |
249 | - } | |
250 | - *matrix = p4d; | |
157 | + if(p4d==NULL) throw MolDSException(this->errorMessageMallocFailure); | |
158 | + | |
159 | + for(size_t i=0;i<size1;i++){p4d[i] = &p3d[i*size2]; | |
160 | + for(size_t j=0;j<size2;j++){p4d[i][j] = &p3d[i*size2][j*size3]; | |
161 | + for(size_t k=0;k<size3;k++){p4d[i][j][k] = &p3d[i*size2][j*size3][k*size4]; | |
162 | + }}} | |
163 | + | |
251 | 164 | MallocerFreer::AddCurrentMalloced(requiredMalloc); |
252 | - this->Initialize<T>(*matrix, size1, size2, size3, size4); | |
165 | + this->Initialize<T>(p4d, size1, size2, size3, size4); | |
166 | + *matrix = p4d; | |
253 | 167 | } |
254 | 168 | catch(MolDSException ex){ |
255 | - if(p1d!=NULL){ | |
256 | - delete[] p1d; | |
257 | - } | |
258 | - if(p2d!=NULL){ | |
259 | - delete[] p2d; | |
260 | - } | |
261 | - if(p3d!=NULL){ | |
262 | - delete[] p3d; | |
263 | - } | |
264 | - if(p4d!=NULL){ | |
265 | - delete[] p4d; | |
266 | - } | |
169 | + this->Free<T>(&p3d, size1*size2, size3, size4); | |
170 | + if(p4d!=NULL) delete[] p4d; | |
267 | 171 | throw ex; |
268 | 172 | } |
269 | 173 | } |
270 | 174 | |
271 | 175 | template<typename T> void Initialize(T**** matrix, size_t size1, size_t size2, size_t size3, size_t size4) const{ |
272 | 176 | for(size_t i=0;i<size1;i++) { |
273 | - for(size_t j=0;j<size2;j++){ | |
274 | - for(size_t k=0;k<size3;k++){ | |
275 | - for(size_t l=0;l<size4;l++){ | |
276 | - matrix[i][j][k][l] = (T)0.0; | |
277 | - } | |
278 | - } | |
279 | - } | |
177 | + this->Initialize<T>(matrix[i], size2, size3, size4); | |
280 | 178 | } |
281 | 179 | } |
282 | 180 | |
283 | 181 | template<typename T> void Free(T***** matrix, size_t size1, size_t size2, size_t size3, size_t size4) const{ |
284 | - if(*matrix==NULL){ | |
285 | - return; | |
286 | - } | |
182 | + if(*matrix==NULL) return; | |
287 | 183 | T *p1d=NULL, **p2d=NULL, ***p3d=NULL,****p4d=NULL; |
288 | 184 | p4d = *matrix; |
289 | 185 | p3d = p4d[0]; |
290 | - p2d = p3d[0]; | |
291 | - p1d = p2d[0]; | |
292 | 186 | delete [] p4d; |
293 | - delete [] p3d; | |
294 | - delete [] p2d; | |
295 | - delete [] p1d; | |
296 | - double freedMalloc = this->GetMemoryAmount<T>(size1, size2, size3, size4); | |
187 | + this->Free<T>(&p3d, size1*size2, size3, size4); | |
188 | + double freedMalloc = this->GetMemoryAmount<T***>(size1); | |
297 | 189 | MallocerFreer::AddCurrentMalloced(-1.0*freedMalloc); |
298 | 190 | *matrix = NULL; |
299 | 191 | } |
300 | 192 | |
301 | - template<typename T> double GetMemoryAmount(size_t size1, size_t size2, size_t size3, size_t size4) const{ | |
302 | - double amount = static_cast<double>(sizeof(T)) | |
303 | - *static_cast<double>(size1) | |
304 | - *static_cast<double>(size2) | |
305 | - *static_cast<double>(size3) | |
306 | - *static_cast<double>(size4); | |
307 | - amount += this->GetMemoryAmount<T*>(size1, size2, size3); | |
308 | - return amount; | |
309 | - } | |
310 | - | |
311 | 193 | //5d |
312 | 194 | template<typename T> void Malloc(T****** matrix, size_t size1, size_t size2, size_t size3, size_t size4, size_t size5) const{ |
313 | - if(*matrix!=NULL){ | |
314 | - return; | |
315 | - } | |
316 | - double requiredMalloc = this->GetMemoryAmount<T>(size1, size2, size3, size4, size5); | |
195 | + if(*matrix!=NULL) return; | |
196 | + double requiredMalloc = this->GetMemoryAmount<T****>(size1); | |
317 | 197 | this->CheckLimitHeap(requiredMalloc); |
318 | 198 | |
319 | - T *p1d=NULL, **p2d=NULL, ***p3d=NULL, ****p4d=NULL, *****p5d=NULL; | |
199 | + T ****p4d=NULL, *****p5d=NULL; | |
320 | 200 | try{ |
321 | - p1d = new T[size1*size2*size3*size4*size5]; | |
322 | - if(p1d==NULL){ | |
323 | - throw MolDSException(this->errorMessageMallocFailure); | |
324 | - } | |
325 | - p2d = new T*[size1*size2*size3*size4]; | |
326 | - if(p2d==NULL){ | |
327 | - throw MolDSException(this->errorMessageMallocFailure); | |
328 | - } | |
329 | - p3d = new T**[size1*size2*size3]; | |
330 | - if(p3d==NULL){ | |
331 | - throw MolDSException(this->errorMessageMallocFailure); | |
332 | - } | |
333 | - p4d = new T***[size1*size2]; | |
334 | - if(p4d==NULL){ | |
335 | - throw MolDSException(this->errorMessageMallocFailure); | |
336 | - } | |
201 | + this->Malloc<T>(&p4d, size1*size2, size3, size4, size5); | |
337 | 202 | p5d = new T****[size1]; |
338 | - if(p5d==NULL){ | |
339 | - throw MolDSException(this->errorMessageMallocFailure); | |
340 | - } | |
341 | - | |
342 | - for(size_t i=0;i<size1;i++){ | |
343 | - p5d[i] = &p4d[i*size2]; | |
344 | - for(size_t j=0;j<size2;j++){ | |
345 | - p5d[i][j] = &p3d[i*size2*size3+j*size3]; | |
346 | - for(size_t k=0;k<size3;k++){ | |
347 | - p5d[i][j][k] = &p2d[i*size2*size3*size4+j*size3*size4+k*size4]; | |
348 | - for(size_t l=0;l<size4;l++){ | |
349 | - p5d[i][j][k][l] = &p1d[i*size2*size3*size4*size5+ | |
350 | - j*size3*size4*size5+ | |
351 | - k*size4*size5+ | |
352 | - l*size5]; | |
353 | - } | |
354 | - } | |
355 | - } | |
356 | - } | |
357 | - *matrix = p5d; | |
203 | + if(p5d==NULL) throw MolDSException(this->errorMessageMallocFailure); | |
204 | + | |
205 | + for(size_t i=0;i<size1;i++){p5d[i] = &p4d[i*size2]; | |
206 | + for(size_t j=0;j<size2;j++){p5d[i][j] = &p4d[i*size2][j*size3]; | |
207 | + for(size_t k=0;k<size3;k++){p5d[i][j][k] = &p4d[i*size2][j*size3][k*size4]; | |
208 | + for(size_t l=0;l<size4;l++){p5d[i][j][k][l] = &p4d[i*size2][j*size3][k*size4][l*size5]; | |
209 | + }}}} | |
210 | + | |
358 | 211 | MallocerFreer::AddCurrentMalloced(requiredMalloc); |
359 | - this->Initialize<T>(*matrix, size1, size2, size3, size4, size5); | |
212 | + this->Initialize<T>(p5d, size1, size2, size3, size4, size5); | |
213 | + *matrix = p5d; | |
360 | 214 | } |
361 | 215 | catch(MolDSException ex){ |
362 | - if(p1d!=NULL){ | |
363 | - delete[] p1d; | |
364 | - } | |
365 | - if(p2d!=NULL){ | |
366 | - delete[] p2d; | |
367 | - } | |
368 | - if(p3d!=NULL){ | |
369 | - delete[] p3d; | |
370 | - } | |
371 | - if(p4d!=NULL){ | |
372 | - delete[] p4d; | |
373 | - } | |
374 | - if(p5d!=NULL){ | |
375 | - delete[] p5d; | |
376 | - } | |
216 | + this->Free<T>(&p4d, size1*size2, size3, size4, size5); | |
217 | + if(p5d!=NULL) delete[] p5d; | |
377 | 218 | throw ex; |
378 | 219 | } |
379 | 220 | } |
380 | 221 | |
381 | 222 | template<typename T> void Initialize(T***** matrix, size_t size1, size_t size2, size_t size3, size_t size4, size_t size5) const{ |
382 | 223 | for(size_t i=0;i<size1;i++) { |
383 | - for(size_t j=0;j<size2;j++){ | |
384 | - for(size_t k=0;k<size3;k++){ | |
385 | - for(size_t l=0;l<size4;l++){ | |
386 | - for(size_t m=0;m<size5;m++){ | |
387 | - matrix[i][j][k][l][m] = 0.0; | |
388 | - } | |
389 | - } | |
390 | - } | |
391 | - } | |
224 | + this->Initialize<T>(matrix[i], size2, size3, size4, size5); | |
392 | 225 | } |
393 | 226 | } |
394 | 227 | |
395 | 228 | template<typename T> void Free(T****** matrix, size_t size1, size_t size2, size_t size3, size_t size4, size_t size5) const{ |
396 | - if(*matrix==NULL){ | |
397 | - return; | |
398 | - } | |
399 | - | |
400 | - T *p1d=NULL, **p2d=NULL, ***p3d=NULL,****p4d=NULL, *****p5d=NULL; | |
229 | + if(*matrix==NULL) return; | |
230 | + T ****p4d=NULL, *****p5d=NULL; | |
401 | 231 | p5d = *matrix; |
402 | 232 | p4d = p5d[0]; |
403 | - p3d = p4d[0]; | |
404 | - p2d = p3d[0]; | |
405 | - p1d = p2d[0]; | |
406 | 233 | delete [] p5d; |
407 | - delete [] p4d; | |
408 | - delete [] p3d; | |
409 | - delete [] p2d; | |
410 | - delete [] p1d; | |
411 | - double freedMalloc = this->GetMemoryAmount<T>(size1, size2, size3, size4, size5); | |
234 | + this->Free<T>(&p4d, size1*size2, size3, size4, size5); | |
235 | + double freedMalloc = this->GetMemoryAmount<T****>(size1); | |
412 | 236 | MallocerFreer::AddCurrentMalloced(-1.0*freedMalloc); |
413 | 237 | *matrix = NULL; |
414 | 238 | } |
415 | 239 | |
416 | - template<typename T> double GetMemoryAmount(size_t size1, size_t size2, size_t size3, size_t size4, size_t size5) const{ | |
417 | - double amount = static_cast<double>(sizeof(T)) | |
418 | - *static_cast<double>(size1) | |
419 | - *static_cast<double>(size2) | |
420 | - *static_cast<double>(size3) | |
421 | - *static_cast<double>(size4) | |
422 | - *static_cast<double>(size5); | |
423 | - amount += this->GetMemoryAmount<T*>(size1, size2, size3, size4); | |
424 | - return amount; | |
425 | - } | |
426 | - | |
427 | 240 | //6d |
428 | 241 | template<typename T> void Malloc(T******* matrix, size_t size1, size_t size2, size_t size3, size_t size4, size_t size5, size_t size6) const{ |
429 | - if(*matrix!=NULL){ | |
430 | - return; | |
431 | - } | |
432 | - double requiredMalloc = this->GetMemoryAmount<T>(size1, size2, size3, size4, size5, size6); | |
242 | + if(*matrix!=NULL) return; | |
243 | + double requiredMalloc = this->GetMemoryAmount<T*****>(size1); | |
433 | 244 | this->CheckLimitHeap(requiredMalloc); |
434 | 245 | |
435 | - T *p1d=NULL, **p2d=NULL, ***p3d=NULL, ****p4d=NULL, *****p5d=NULL, ******p6d=NULL; | |
246 | + T *****p5d=NULL, ******p6d=NULL; | |
436 | 247 | try{ |
437 | - p1d = new T[size1*size2*size3*size4*size5*size6]; | |
438 | - if(p1d==NULL){ | |
439 | - throw MolDSException(this->errorMessageMallocFailure); | |
440 | - } | |
441 | - p2d = new T*[size1*size2*size3*size4*size5]; | |
442 | - if(p2d==NULL){ | |
443 | - throw MolDSException(this->errorMessageMallocFailure); | |
444 | - } | |
445 | - p3d = new T**[size1*size2*size3*size4]; | |
446 | - if(p3d==NULL){ | |
447 | - throw MolDSException(this->errorMessageMallocFailure); | |
448 | - } | |
449 | - p4d = new T***[size1*size2*size3]; | |
450 | - if(p4d==NULL){ | |
451 | - throw MolDSException(this->errorMessageMallocFailure); | |
452 | - } | |
453 | - p5d = new T****[size1*size2]; | |
454 | - if(p5d==NULL){ | |
455 | - throw MolDSException(this->errorMessageMallocFailure); | |
456 | - } | |
248 | + this->Malloc<T>(&p5d, size1*size2, size3, size4, size5, size6); | |
457 | 249 | p6d = new T*****[size1]; |
458 | - if(p6d==NULL){ | |
459 | - throw MolDSException(this->errorMessageMallocFailure); | |
460 | - } | |
461 | - | |
462 | - for(size_t i=0;i<size1;i++){ | |
463 | - p6d[i] = &p5d[i*size2]; | |
464 | - for(size_t j=0;j<size2;j++){ | |
465 | - p6d[i][j] = &p4d[i*size2*size3+j*size3]; | |
466 | - for(size_t k=0;k<size3;k++){ | |
467 | - p6d[i][j][k] = &p3d[i*size2*size3*size4+j*size3*size4+k*size4]; | |
468 | - for(size_t l=0;l<size4;l++){ | |
469 | - p6d[i][j][k][l] = &p2d[i*size2*size3*size4*size5+ | |
470 | - j*size3*size4*size5+ | |
471 | - k*size4*size5+ | |
472 | - l*size5]; | |
473 | - for(size_t m=0;m<size5;m++){ | |
474 | - p6d[i][j][k][l][m] = &p1d[i*size2*size3*size4*size5*size6+ | |
475 | - j*size3*size4*size5*size6+ | |
476 | - k*size4*size5*size6+ | |
477 | - l*size5*size6+ | |
478 | - m*size6]; | |
479 | - } | |
480 | - } | |
481 | - } | |
482 | - } | |
483 | - } | |
484 | - *matrix = p6d; | |
250 | + if(p6d==NULL) throw MolDSException(this->errorMessageMallocFailure); | |
251 | + | |
252 | + for(size_t i=0;i<size1;i++){p6d[i] = &p5d[i*size2]; | |
253 | + for(size_t j=0;j<size2;j++){p6d[i][j] = &p5d[i*size2][j*size3]; | |
254 | + for(size_t k=0;k<size3;k++){p6d[i][j][k] = &p5d[i*size2][j*size3][k*size4]; | |
255 | + for(size_t l=0;l<size4;l++){p6d[i][j][k][l] = &p5d[i*size2][j*size3][k*size4][l*size5]; | |
256 | + for(size_t m=0;m<size5;m++){p6d[i][j][k][l][m] = &p5d[i*size2][j*size3][k*size4][l*size5][m*size6]; | |
257 | + }}}}} | |
258 | + | |
485 | 259 | MallocerFreer::AddCurrentMalloced(requiredMalloc); |
486 | - this->Initialize<T>(*matrix, size1, size2, size3, size4, size5, size6); | |
260 | + this->Initialize<T>(p6d, size1, size2, size3, size4, size5, size6); | |
261 | + *matrix = p6d; | |
487 | 262 | } |
488 | 263 | catch(MolDSException ex){ |
489 | - if(p1d!=NULL){ | |
490 | - delete[] p1d; | |
491 | - } | |
492 | - if(p2d!=NULL){ | |
493 | - delete[] p2d; | |
494 | - } | |
495 | - if(p3d!=NULL){ | |
496 | - delete[] p3d; | |
497 | - } | |
498 | - if(p4d!=NULL){ | |
499 | - delete[] p4d; | |
500 | - } | |
501 | - if(p5d!=NULL){ | |
502 | - delete[] p5d; | |
503 | - } | |
504 | - if(p6d!=NULL){ | |
505 | - delete[] p6d; | |
506 | - } | |
264 | + this->Free<T>(&p5d, size1*size2, size3, size4, size5, size6); | |
265 | + if(p6d!=NULL) delete[] p6d; | |
507 | 266 | throw ex; |
508 | 267 | } |
509 | 268 | } |
510 | 269 | |
511 | 270 | template<typename T> void Initialize(T****** matrix, size_t size1, size_t size2, size_t size3, size_t size4, size_t size5, size_t size6) const{ |
512 | 271 | for(size_t i=0;i<size1;i++) { |
513 | - for(size_t j=0;j<size2;j++){ | |
514 | - for(size_t k=0;k<size3;k++){ | |
515 | - for(size_t l=0;l<size4;l++){ | |
516 | - for(size_t m=0;m<size5;m++){ | |
517 | - for(size_t n=0;n<size6;n++){ | |
518 | - matrix[i][j][k][l][m][n] = 0.0; | |
519 | - } | |
520 | - } | |
521 | - } | |
522 | - } | |
523 | - } | |
272 | + this->Initialize<T>(matrix[i], size2, size3, size4, size5, size6); | |
524 | 273 | } |
525 | 274 | } |
526 | 275 | |
527 | 276 | template<typename T> void Free(T******* matrix, size_t size1, size_t size2, size_t size3, size_t size4, size_t size5, size_t size6) const{ |
528 | - if(*matrix==NULL){ | |
529 | - return; | |
530 | - } | |
531 | - T *p1d=NULL, **p2d=NULL, ***p3d=NULL,****p4d=NULL, *****p5d=NULL, ******p6d=NULL; | |
277 | + if(*matrix==NULL) return; | |
278 | + T *****p5d=NULL, ******p6d=NULL; | |
532 | 279 | p6d = *matrix; |
533 | 280 | p5d = p6d[0]; |
534 | - p4d = p5d[0]; | |
535 | - p3d = p4d[0]; | |
536 | - p2d = p3d[0]; | |
537 | - p1d = p2d[0]; | |
538 | 281 | delete [] p6d; |
539 | - delete [] p5d; | |
540 | - delete [] p4d; | |
541 | - delete [] p3d; | |
542 | - delete [] p2d; | |
543 | - delete [] p1d; | |
544 | - double freedMalloc = this->GetMemoryAmount<T>(size1, size2, size3, size4, size5, size6); | |
282 | + this->Free<T>(&p5d, size1*size2, size3, size4, size5, size6); | |
283 | + double freedMalloc = this->GetMemoryAmount<T*****>(size1); | |
545 | 284 | MallocerFreer::AddCurrentMalloced(-1.0*freedMalloc); |
546 | 285 | *matrix = NULL; |
547 | 286 | } |
548 | 287 | |
549 | - template<typename T> double GetMemoryAmount(size_t size1, size_t size2, size_t size3, size_t size4, size_t size5, size_t size6) const{ | |
550 | - double amount = static_cast<double>(sizeof(T)) | |
551 | - *static_cast<double>(size1) | |
552 | - *static_cast<double>(size2) | |
553 | - *static_cast<double>(size3) | |
554 | - *static_cast<double>(size4) | |
555 | - *static_cast<double>(size5) | |
556 | - *static_cast<double>(size6); | |
557 | - amount += this->GetMemoryAmount<T*>(size1, size2, size3, size4, size5); | |
558 | - return amount; | |
559 | - } | |
560 | - | |
561 | 288 | //7d |
562 | 289 | template<typename T> void Malloc(T******** matrix, size_t size1, size_t size2, size_t size3, size_t size4, size_t size5, size_t size6, size_t size7) const{ |
563 | - if(*matrix!=NULL){ | |
564 | - return; | |
565 | - } | |
566 | - double requiredMalloc = this->GetMemoryAmount<T>(size1, size2, size3, size4, size5, size6, size7); | |
290 | + if(*matrix!=NULL) return; | |
291 | + double requiredMalloc = this->GetMemoryAmount<T******>(size1); | |
567 | 292 | this->CheckLimitHeap(requiredMalloc); |
568 | 293 | |
569 | - T *p1d=NULL, **p2d=NULL, ***p3d=NULL, ****p4d=NULL, *****p5d=NULL, ******p6d=NULL, *******p7d=NULL; | |
294 | + T ******p6d=NULL, *******p7d=NULL; | |
570 | 295 | try{ |
571 | - p1d = new T[size1*size2*size3*size4*size5*size6*size7]; | |
572 | - if(p1d==NULL){ | |
573 | - throw MolDSException(this->errorMessageMallocFailure); | |
574 | - } | |
575 | - p2d = new T*[size1*size2*size3*size4*size5*size6]; | |
576 | - if(p2d==NULL){ | |
577 | - throw MolDSException(this->errorMessageMallocFailure); | |
578 | - } | |
579 | - p3d = new T**[size1*size2*size3*size4*size5]; | |
580 | - if(p3d==NULL){ | |
581 | - throw MolDSException(this->errorMessageMallocFailure); | |
582 | - } | |
583 | - p4d = new T***[size1*size2*size3*size4]; | |
584 | - if(p4d==NULL){ | |
585 | - throw MolDSException(this->errorMessageMallocFailure); | |
586 | - } | |
587 | - p5d = new T****[size1*size2*size3]; | |
588 | - if(p5d==NULL){ | |
589 | - throw MolDSException(this->errorMessageMallocFailure); | |
590 | - } | |
591 | - p6d = new T*****[size1*size2]; | |
592 | - if(p6d==NULL){ | |
593 | - throw MolDSException(this->errorMessageMallocFailure); | |
594 | - } | |
296 | + this->Malloc<T>(&p6d, size1*size2, size3, size4, size5, size6, size7); | |
595 | 297 | p7d = new T******[size1]; |
596 | - if(p7d==NULL){ | |
597 | - throw MolDSException(this->errorMessageMallocFailure); | |
598 | - } | |
599 | - | |
600 | - for(size_t i=0;i<size1;i++){ | |
601 | - p7d[i] = &p6d[i*size2]; | |
602 | - for(size_t j=0;j<size2;j++){ | |
603 | - p7d[i][j] = &p5d[i*size2*size3+j*size3]; | |
604 | - for(size_t k=0;k<size3;k++){ | |
605 | - p7d[i][j][k] = &p4d[i*size2*size3*size4+j*size3*size4+k*size4]; | |
606 | - for(size_t l=0;l<size4;l++){ | |
607 | - p7d[i][j][k][l] = &p3d[i*size2*size3*size4*size5+ | |
608 | - j*size3*size4*size5+ | |
609 | - k*size4*size5+ | |
610 | - l*size5]; | |
611 | - for(size_t m=0;m<size5;m++){ | |
612 | - p7d[i][j][k][l][m] = &p2d[i*size2*size3*size4*size5*size6+ | |
613 | - j*size3*size4*size5*size6+ | |
614 | - k*size4*size5*size6+ | |
615 | - l*size5*size6+ | |
616 | - m*size6]; | |
617 | - for(size_t n=0;n<size6;n++){ | |
618 | - p7d[i][j][k][l][m][n] = &p1d[i*size2*size3*size4*size5*size6*size7+ | |
619 | - j*size3*size4*size5*size6*size7+ | |
620 | - k*size4*size5*size6*size7+ | |
621 | - l*size5*size6*size7+ | |
622 | - m*size6*size7+ | |
623 | - n*size7]; | |
624 | - } | |
625 | - } | |
626 | - } | |
627 | - } | |
628 | - } | |
629 | - } | |
630 | - *matrix = p7d; | |
298 | + if(p7d==NULL) throw MolDSException(this->errorMessageMallocFailure); | |
299 | + | |
300 | + for(size_t i=0;i<size1;i++){p7d[i] = &p6d[i*size2]; | |
301 | + for(size_t j=0;j<size2;j++){p7d[i][j] = &p6d[i*size2][j*size3]; | |
302 | + for(size_t k=0;k<size3;k++){p7d[i][j][k] = &p6d[i*size2][j*size3][k*size4]; | |
303 | + for(size_t l=0;l<size4;l++){p7d[i][j][k][l] = &p6d[i*size2][j*size3][k*size4][l*size5]; | |
304 | + for(size_t m=0;m<size5;m++){p7d[i][j][k][l][m] = &p6d[i*size2][j*size3][k*size4][l*size5][m*size6]; | |
305 | + for(size_t n=0;n<size6;n++){p7d[i][j][k][l][m][n] = &p6d[i*size2][j*size3][k*size4][l*size5][m*size6][n*size7]; | |
306 | + }}}}}} | |
307 | + | |
631 | 308 | MallocerFreer::AddCurrentMalloced(requiredMalloc); |
632 | - this->Initialize<T>(*matrix, size1, size2, size3, size4, size5, size6, size7); | |
309 | + this->Initialize<T>(p7d, size1, size2, size3, size4, size5, size6, size7); | |
310 | + *matrix = p7d; | |
633 | 311 | } |
634 | 312 | catch(MolDSException ex){ |
635 | - if(p1d!=NULL){ | |
636 | - delete[] p1d; | |
637 | - } | |
638 | - if(p2d!=NULL){ | |
639 | - delete[] p2d; | |
640 | - } | |
641 | - if(p3d!=NULL){ | |
642 | - delete[] p3d; | |
643 | - } | |
644 | - if(p4d!=NULL){ | |
645 | - delete[] p4d; | |
646 | - } | |
647 | - if(p5d!=NULL){ | |
648 | - delete[] p5d; | |
649 | - } | |
650 | - if(p6d!=NULL){ | |
651 | - delete[] p6d; | |
652 | - } | |
653 | - if(p7d!=NULL){ | |
654 | - delete[] p7d; | |
655 | - } | |
313 | + this->Free<T>(&p6d, size1*size2, size3, size4, size5, size6, size7); | |
314 | + if(p7d!=NULL) delete[] p7d; | |
656 | 315 | throw ex; |
657 | 316 | } |
658 | 317 | } |
659 | 318 | |
660 | 319 | template<typename T> void Initialize(T******* matrix, size_t size1, size_t size2, size_t size3, size_t size4, size_t size5, size_t size6, size_t size7) const{ |
661 | 320 | for(size_t i=0;i<size1;i++) { |
662 | - for(size_t j=0;j<size2;j++){ | |
663 | - for(size_t k=0;k<size3;k++){ | |
664 | - for(size_t l=0;l<size4;l++){ | |
665 | - for(size_t m=0;m<size5;m++){ | |
666 | - for(size_t n=0;n<size6;n++){ | |
667 | - for(size_t o=0;o<size7;o++){ | |
668 | - matrix[i][j][k][l][m][n][o] = 0.0; | |
669 | - } | |
670 | - } | |
671 | - } | |
672 | - } | |
673 | - } | |
674 | - } | |
321 | + this->Initialize<T>(matrix[i], size2, size3, size4, size5, size6, size7); | |
675 | 322 | } |
676 | 323 | } |
677 | 324 | |
678 | 325 | template<typename T> void Free(T******** matrix, size_t size1, size_t size2, size_t size3, size_t size4, size_t size5, size_t size6, size_t size7) const{ |
679 | - if(*matrix==NULL){ | |
680 | - return; | |
681 | - } | |
682 | - T *p1d=NULL, **p2d=NULL, ***p3d=NULL,****p4d=NULL, *****p5d=NULL, ******p6d=NULL, *******p7d=NULL; | |
326 | + if(*matrix==NULL) return; | |
327 | + T ******p6d=NULL, *******p7d=NULL; | |
683 | 328 | p7d = *matrix; |
684 | 329 | p6d = p7d[0]; |
685 | - p5d = p6d[0]; | |
686 | - p4d = p5d[0]; | |
687 | - p3d = p4d[0]; | |
688 | - p2d = p3d[0]; | |
689 | - p1d = p2d[0]; | |
690 | 330 | delete [] p7d; |
691 | - delete [] p6d; | |
692 | - delete [] p5d; | |
693 | - delete [] p4d; | |
694 | - delete [] p3d; | |
695 | - delete [] p2d; | |
696 | - delete [] p1d; | |
697 | - double freedMalloc = this->GetMemoryAmount<T>(size1, size2, size3, size4, size5, size6, size7); | |
331 | + this->Free<T>(&p6d, size1*size2, size3, size4, size5, size6, size7); | |
332 | + double freedMalloc = this->GetMemoryAmount<T******>(size1); | |
698 | 333 | MallocerFreer::AddCurrentMalloced(-1.0*freedMalloc); |
699 | 334 | *matrix = NULL; |
700 | 335 | } |
701 | - | |
702 | - template<typename T> double GetMemoryAmount(size_t size1, size_t size2, size_t size3, size_t size4, size_t size5, size_t size6, size_t size7) const{ | |
703 | - double amount = static_cast<double>(sizeof(T)) | |
704 | - *static_cast<double>(size1) | |
705 | - *static_cast<double>(size2) | |
706 | - *static_cast<double>(size3) | |
707 | - *static_cast<double>(size4) | |
708 | - *static_cast<double>(size5) | |
709 | - *static_cast<double>(size6) | |
710 | - *static_cast<double>(size7); | |
711 | - amount += this->GetMemoryAmount<T*>(size1, size2, size3, size4, size5, size6); | |
712 | - return amount; | |
713 | - } | |
714 | 336 | private: |
715 | 337 | MallocerFreer(); |
716 | 338 | ~MallocerFreer(); |