xue@1
|
1 #ifndef ARRAYALLOC
|
xue@1
|
2 #define ARRAYALLOC
|
xue@1
|
3
|
xue@1
|
4 /*
|
xue@1
|
5 arrayalloc.h - 2D, 3D and 4D array memory allocation routines.
|
xue@1
|
6
|
xue@1
|
7 An x-dimensional array (x=2, 3, 4, ...) is managed as a single memory block hosting all records, plus
|
xue@1
|
8 an index block which is a (x-1)D array itself. Therefore a 2D array is allocated as two 1D arrays, a
|
xue@1
|
9 3D array is allocated as three 1D arrays, etc.
|
xue@1
|
10
|
xue@1
|
11 Examples:
|
xue@1
|
12 Alloc2(4, N, x) declares an array x[4][N] of double-precision floating points.
|
xue@1
|
13 Allocate3(int, K, L, M, x) allocates an array x[K][L][M] of integers and returns x as int***.
|
xue@1
|
14
|
xue@1
|
15 This file also includes a garbage collector class MList that works with arrays allcoated in this way.
|
xue@1
|
16 */
|
xue@1
|
17
|
xue@1
|
18
|
xue@1
|
19 #include <stdlib.h>
|
xue@1
|
20
|
xue@1
|
21 //2D array allocation macros for declaring an array of double
|
xue@1
|
22 #define Alloc2(M, N, x) \
|
xue@1
|
23 double **x=new double*[M]; x[0]=new double[(M)*(N)]; for (int _z=1; _z<M; _z++) x[_z]=&x[0][_z*(N)];
|
xue@1
|
24 #define Alloc2L(M, N, x, LIST) Alloc2(M, N, x); if (LIST) LIST->Add(x, 2);
|
xue@1
|
25 //2D array allocation macros for all data types
|
xue@1
|
26 #define Allocate2(INT, M, N, x) \
|
xue@1
|
27 x=new INT*[M]; x[0]=new INT[(M)*(N)]; for (int _z=1; _z<M; _z++) x[_z]=&x[0][_z*(N)];
|
xue@1
|
28 #define Allocate2L(INT, M, N, x, LIST) Allocate2(INT, M, N, x); if (LIST) LIST->Add(x, 2);
|
xue@1
|
29 //2D array deallocation macro
|
xue@1
|
30 #define DeAlloc2(x) {if (x) {delete[] (char*)(x[0]); delete[] x; x=0;}}
|
xue@1
|
31
|
xue@1
|
32 //3D array allocation macro for declaring an array of double
|
xue@1
|
33 #define Alloc3(L, M, N, x) \
|
xue@1
|
34 double*** x=new double**[L]; x[0]=new double*[(L)*(M)]; x[0][0]=new double[(L)*(M)*(N)]; \
|
xue@1
|
35 for (int _z=0; _z<L; _z++) {x[_z]=&x[0][_z*(M)]; x[_z][0]=&x[0][0][_z*(M)*(N)]; \
|
xue@1
|
36 for (int __z=1; __z<M; __z++) x[_z][__z]=&x[_z][0][__z*(N)]; }
|
xue@1
|
37 //3D array allocation macros for all data types
|
xue@1
|
38 #define Allocate3(INT, L, M, N, x) \
|
xue@1
|
39 x=new INT**[L]; x[0]=new INT*[(L)*(M)]; x[0][0]=new INT[(L)*(M)*(N)]; \
|
xue@1
|
40 for (int _z=0; _z<L; _z++) {x[_z]=&x[0][_z*(M)]; x[_z][0]=&x[0][0][_z*(M)*(N)]; \
|
xue@1
|
41 for (int __z=1; __z<M; __z++) x[_z][__z]=&x[_z][0][__z*(N)]; }
|
xue@1
|
42 #define Allocate3L(INT, M, N, O, x, LIST) Allocate3(INT, M, N, O, x); if (LIST) LIST->Add(x, 3);
|
xue@1
|
43 //3D array deallocation macro
|
xue@1
|
44 #define DeAlloc3(x) {if (x) {delete[] (char*)(x[0][0]); delete[] x[0]; delete[] x; x=0;}}
|
xue@1
|
45
|
xue@1
|
46 //4D array allocation macro for declaring an array of double
|
xue@1
|
47 #define Alloc4(L, M, N, O, x) \
|
xue@1
|
48 double**** x=new double***[L]; x[0]=new double**[(L)*(M)]; \
|
xue@1
|
49 x[0][0]=new double*[(L)*(M)*(N)]; x[0][0][0]=new double[(L)*(M)*(N)*(O)]; \
|
xue@1
|
50 for (int _z=0; _z<L; _z++){ \
|
xue@1
|
51 x[_z]=&x[0][_z*(M)]; x[_z][0]=&x[0][0][_z*(M)*(N)]; x[_z][0][0]=&x[0][0][0][_z*(M)*(N)*(O)]; \
|
xue@1
|
52 for (int __z=0; __z<M; __z++){ \
|
xue@1
|
53 x[_z][__z]=&x[_z][0][__z*(N)]; x[_z][__z][0]=&x[_z][0][0][__z*(N)*(O)]; \
|
xue@1
|
54 for (int ___z=1; ___z<N; ___z++) x[_z][__z][___z]=&d[_z][__z][0][___z*(O)]; }}
|
xue@1
|
55 //4D array deallocation macro
|
xue@1
|
56 #define DeAlloc4(x) {if (x) {delete[] (char*)(x[0][0][0]); delete[] x[0][0]; delete[] x[0]; delete[] x; x=0;}}
|
xue@1
|
57
|
xue@1
|
58
|
xue@1
|
59 /*
|
xue@1
|
60 MList is a garbage collector for arrays created using Alloc* or Allocate* (*=2, 3, 4). After being
|
xue@1
|
61 added to the list the arrays will be automatically freed when MList is deleted.
|
xue@1
|
62
|
xue@1
|
63 Using MList:
|
xue@1
|
64 Create an MList object and add all buffers (1D, 2D, 3D or 4D) to be recycled to the MList using
|
xue@1
|
65 MList::Add(...). Deleting the MList will recycle all the added buffers.
|
xue@1
|
66
|
xue@1
|
67 Example:
|
xue@1
|
68 Alloc2L(4, N, x, mlist) declares 2D array x and registers it with garbage collector mlist,so that x is
|
xue@1
|
69 freed when mlist is deleted.
|
xue@1
|
70 */
|
xue@1
|
71 class MList
|
xue@1
|
72 {
|
xue@1
|
73 public:
|
xue@1
|
74 int cap[4];
|
xue@1
|
75 int count[4];
|
xue@1
|
76 void** List[4];
|
xue@1
|
77 MList(){for (int i=0; i<4; i++) cap[i]=64, count[i]=0, List[i]=(void**)malloc(sizeof(void*)*cap[i]);}
|
xue@1
|
78 ~MList(){
|
xue@1
|
79 for (int i=0; i<count[0]; i++){delete[] (char*)(List[0][i]); List[0][i]=0;} free(List[0]);
|
xue@1
|
80 for (int i=0; i<count[1]; i++){void** tmp=(void**)List[1][i]; DeAlloc2(tmp); List[1][i]=0;} free(List[1]);
|
xue@1
|
81 for (int i=0; i<count[2]; i++){void*** tmp=(void***)List[2][i]; DeAlloc3(tmp); List[2][i]=0;} free(List[2]);
|
xue@1
|
82 for (int i=0; i<count[3]; i++){void**** tmp=(void****)List[3][i]; DeAlloc4(tmp); List[3][i]=0;} free(List[3]);}
|
xue@1
|
83 void __fastcall Add(void* item, int Dim){
|
xue@1
|
84 int Gr=Dim-1; if (count[Gr]==cap[Gr]) IncCap(Gr); List[Gr][count[Gr]++]=item;}
|
xue@1
|
85 void IncCap(int Gr){cap[Gr]+=64; List[Gr]=(void**)realloc(List[Gr], sizeof(void*)*cap[Gr]);}
|
xue@1
|
86 };
|
xue@1
|
87
|
xue@1
|
88 #endif
|