• R/O
  • HTTP
  • SSH
  • HTTPS

提交

標籤
無標籤

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

修訂c800121f2af5510b2cf7ea2fc8ea8547b542a831 (tree)
時間2012-11-14 17:51:32
作者Mikiya Fujii <mikiya.fujii@gmai...>
CommiterMikiya Fujii

Log Message

Refactoring MallocerFreere. ##30083

git-svn-id: https://svn.sourceforge.jp/svnroot/molds/trunk@1118 1136aad2-a195-0410-b898-f5ea1d11b9d8

Change Summary

差異

--- a/src/base/MallocerFreer.h
+++ b/src/base/MallocerFreer.h
@@ -29,16 +29,11 @@ public:
2929
3030 //1d
3131 template<typename T> void Malloc(T** matrix, size_t size1) const{
32- if(*matrix!=NULL){
33- return;
34- }
32+ if(*matrix!=NULL) return;
3533 double requiredMalloc = this->GetMemoryAmount<T>(size1);
3634 this->CheckLimitHeap(requiredMalloc);
37-
3835 *matrix = new T[size1];
39- if(*matrix==NULL){
40- throw MolDSException(this->errorMessageMallocFailure);
41- }
36+ if(*matrix==NULL) throw MolDSException(this->errorMessageMallocFailure);
4237 MallocerFreer::AddCurrentMalloced(requiredMalloc);
4338 this->Initialize<T>(*matrix, size1);
4439 }
@@ -50,9 +45,7 @@ public:
5045 }
5146
5247 template<typename T> void Free(T** matrix, size_t size1) const{
53- if(*matrix==NULL){
54- return;
55- }
48+ if(*matrix==NULL) return;
5649 delete [] *matrix;
5750 double freedMalloc = this->GetMemoryAmount<T>(size1);
5851 MallocerFreer::AddCurrentMalloced(-1.0*freedMalloc);
@@ -60,657 +53,286 @@ public:
6053 }
6154
6255 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);
6657 }
6758
6859 // 2d
6960 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);
7463 this->CheckLimitHeap(requiredMalloc);
7564
7665 T *p1d=NULL, **p2d=NULL;
7766 try{
78- p1d = new T[size1*size2];
79- if(p1d==NULL){
80- throw MolDSException(this->errorMessageMallocFailure);
81- }
67+ this->Malloc<T>(&p1d, size1*size2);
8268 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];}
8672
87- for(size_t i=0;i<size1;i++){
88- p2d[i] = &p1d[i*size2];
89- }
90- *matrix = p2d;
9173 MallocerFreer::AddCurrentMalloced(requiredMalloc);
92- this->Initialize<T>(*matrix, size1, size2);
74+ this->Initialize<T>(p2d, size1, size2);
75+ *matrix = p2d;
9376 }
9477 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;
10180 throw ex;
10281 }
10382 }
10483
10584 template<typename T> void Initialize(T** matrix, size_t size1, size_t size2) const{
10685 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);
11087 }
11188 }
11289
11390 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);
12098 MallocerFreer::AddCurrentMalloced(-1.0*freedMalloc);
12199 *matrix = NULL;
122100 }
123101
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-
132102 // 3d
133103 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);
138106 this->CheckLimitHeap(requiredMalloc);
139107
140- T *p1d=NULL, **p2d=NULL, ***p3d=NULL;
108+ T **p2d=NULL, ***p3d=NULL;
141109 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);
150111 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+
162118 MallocerFreer::AddCurrentMalloced(requiredMalloc);
163- this->Initialize<T>(*matrix, size1, size2, size3);
119+ this->Initialize<T>(p3d, size1, size2, size3);
120+ *matrix = p3d;
164121 }
165122 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;
175125 throw ex;
176126 }
177127 }
178128
179129 template<typename T> void Initialize(T*** matrix, size_t size1, size_t size2, size_t size3) const{
180130 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);
186132 }
187133 }
188134
189135 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;
194138 p3d = *matrix;
195139 p2d = p3d[0];
196- p1d = p2d[0];
197140 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);
201143 MallocerFreer::AddCurrentMalloced(-1.0*freedMalloc);
202144 *matrix = NULL;
203145 }
204146
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-
214147 //4d
215148 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);
220151 this->CheckLimitHeap(requiredMalloc);
221152
222- T *p1d=NULL, **p2d=NULL, ***p3d=NULL, ****p4d=NULL;
153+ T ***p3d=NULL, ****p4d=NULL;
223154 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);
236156 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+
251164 MallocerFreer::AddCurrentMalloced(requiredMalloc);
252- this->Initialize<T>(*matrix, size1, size2, size3, size4);
165+ this->Initialize<T>(p4d, size1, size2, size3, size4);
166+ *matrix = p4d;
253167 }
254168 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;
267171 throw ex;
268172 }
269173 }
270174
271175 template<typename T> void Initialize(T**** matrix, size_t size1, size_t size2, size_t size3, size_t size4) const{
272176 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);
280178 }
281179 }
282180
283181 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;
287183 T *p1d=NULL, **p2d=NULL, ***p3d=NULL,****p4d=NULL;
288184 p4d = *matrix;
289185 p3d = p4d[0];
290- p2d = p3d[0];
291- p1d = p2d[0];
292186 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);
297189 MallocerFreer::AddCurrentMalloced(-1.0*freedMalloc);
298190 *matrix = NULL;
299191 }
300192
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-
311193 //5d
312194 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);
317197 this->CheckLimitHeap(requiredMalloc);
318198
319- T *p1d=NULL, **p2d=NULL, ***p3d=NULL, ****p4d=NULL, *****p5d=NULL;
199+ T ****p4d=NULL, *****p5d=NULL;
320200 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);
337202 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+
358211 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;
360214 }
361215 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;
377218 throw ex;
378219 }
379220 }
380221
381222 template<typename T> void Initialize(T***** matrix, size_t size1, size_t size2, size_t size3, size_t size4, size_t size5) const{
382223 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);
392225 }
393226 }
394227
395228 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;
401231 p5d = *matrix;
402232 p4d = p5d[0];
403- p3d = p4d[0];
404- p2d = p3d[0];
405- p1d = p2d[0];
406233 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);
412236 MallocerFreer::AddCurrentMalloced(-1.0*freedMalloc);
413237 *matrix = NULL;
414238 }
415239
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-
427240 //6d
428241 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);
433244 this->CheckLimitHeap(requiredMalloc);
434245
435- T *p1d=NULL, **p2d=NULL, ***p3d=NULL, ****p4d=NULL, *****p5d=NULL, ******p6d=NULL;
246+ T *****p5d=NULL, ******p6d=NULL;
436247 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);
457249 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+
485259 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;
487262 }
488263 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;
507266 throw ex;
508267 }
509268 }
510269
511270 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{
512271 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);
524273 }
525274 }
526275
527276 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;
532279 p6d = *matrix;
533280 p5d = p6d[0];
534- p4d = p5d[0];
535- p3d = p4d[0];
536- p2d = p3d[0];
537- p1d = p2d[0];
538281 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);
545284 MallocerFreer::AddCurrentMalloced(-1.0*freedMalloc);
546285 *matrix = NULL;
547286 }
548287
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-
561288 //7d
562289 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);
567292 this->CheckLimitHeap(requiredMalloc);
568293
569- T *p1d=NULL, **p2d=NULL, ***p3d=NULL, ****p4d=NULL, *****p5d=NULL, ******p6d=NULL, *******p7d=NULL;
294+ T ******p6d=NULL, *******p7d=NULL;
570295 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);
595297 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+
631308 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;
633311 }
634312 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;
656315 throw ex;
657316 }
658317 }
659318
660319 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{
661320 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);
675322 }
676323 }
677324
678325 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;
683328 p7d = *matrix;
684329 p6d = p7d[0];
685- p5d = p6d[0];
686- p4d = p5d[0];
687- p3d = p4d[0];
688- p2d = p3d[0];
689- p1d = p2d[0];
690330 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);
698333 MallocerFreer::AddCurrentMalloced(-1.0*freedMalloc);
699334 *matrix = NULL;
700335 }
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- }
714336 private:
715337 MallocerFreer();
716338 ~MallocerFreer();