Mercurial > hg > x
diff arrayalloc.h @ 1:6422640a802f
first upload
author | Wen X <xue.wen@elec.qmul.ac.uk> |
---|---|
date | Tue, 05 Oct 2010 10:45:57 +0100 |
parents | |
children | 5f3c32dc6e17 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/arrayalloc.h Tue Oct 05 10:45:57 2010 +0100 @@ -0,0 +1,88 @@ +#ifndef ARRAYALLOC +#define ARRAYALLOC + +/* + arrayalloc.h - 2D, 3D and 4D array memory allocation routines. + + An x-dimensional array (x=2, 3, 4, ...) is managed as a single memory block hosting all records, plus + an index block which is a (x-1)D array itself. Therefore a 2D array is allocated as two 1D arrays, a + 3D array is allocated as three 1D arrays, etc. + + Examples: + Alloc2(4, N, x) declares an array x[4][N] of double-precision floating points. + Allocate3(int, K, L, M, x) allocates an array x[K][L][M] of integers and returns x as int***. + + This file also includes a garbage collector class MList that works with arrays allcoated in this way. +*/ + + +#include <stdlib.h> + +//2D array allocation macros for declaring an array of double +#define Alloc2(M, N, x) \ + double **x=new double*[M]; x[0]=new double[(M)*(N)]; for (int _z=1; _z<M; _z++) x[_z]=&x[0][_z*(N)]; +#define Alloc2L(M, N, x, LIST) Alloc2(M, N, x); if (LIST) LIST->Add(x, 2); +//2D array allocation macros for all data types +#define Allocate2(INT, M, N, x) \ + x=new INT*[M]; x[0]=new INT[(M)*(N)]; for (int _z=1; _z<M; _z++) x[_z]=&x[0][_z*(N)]; + #define Allocate2L(INT, M, N, x, LIST) Allocate2(INT, M, N, x); if (LIST) LIST->Add(x, 2); +//2D array deallocation macro +#define DeAlloc2(x) {if (x) {delete[] (char*)(x[0]); delete[] x; x=0;}} + +//3D array allocation macro for declaring an array of double +#define Alloc3(L, M, N, x) \ + double*** x=new double**[L]; x[0]=new double*[(L)*(M)]; x[0][0]=new double[(L)*(M)*(N)]; \ + for (int _z=0; _z<L; _z++) {x[_z]=&x[0][_z*(M)]; x[_z][0]=&x[0][0][_z*(M)*(N)]; \ + for (int __z=1; __z<M; __z++) x[_z][__z]=&x[_z][0][__z*(N)]; } +//3D array allocation macros for all data types +#define Allocate3(INT, L, M, N, x) \ + x=new INT**[L]; x[0]=new INT*[(L)*(M)]; x[0][0]=new INT[(L)*(M)*(N)]; \ + for (int _z=0; _z<L; _z++) {x[_z]=&x[0][_z*(M)]; x[_z][0]=&x[0][0][_z*(M)*(N)]; \ + for (int __z=1; __z<M; __z++) x[_z][__z]=&x[_z][0][__z*(N)]; } + #define Allocate3L(INT, M, N, O, x, LIST) Allocate3(INT, M, N, O, x); if (LIST) LIST->Add(x, 3); +//3D array deallocation macro +#define DeAlloc3(x) {if (x) {delete[] (char*)(x[0][0]); delete[] x[0]; delete[] x; x=0;}} + +//4D array allocation macro for declaring an array of double +#define Alloc4(L, M, N, O, x) \ + double**** x=new double***[L]; x[0]=new double**[(L)*(M)]; \ + x[0][0]=new double*[(L)*(M)*(N)]; x[0][0][0]=new double[(L)*(M)*(N)*(O)]; \ + for (int _z=0; _z<L; _z++){ \ + 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)]; \ + for (int __z=0; __z<M; __z++){ \ + x[_z][__z]=&x[_z][0][__z*(N)]; x[_z][__z][0]=&x[_z][0][0][__z*(N)*(O)]; \ + for (int ___z=1; ___z<N; ___z++) x[_z][__z][___z]=&d[_z][__z][0][___z*(O)]; }} +//4D array deallocation macro +#define DeAlloc4(x) {if (x) {delete[] (char*)(x[0][0][0]); delete[] x[0][0]; delete[] x[0]; delete[] x; x=0;}} + + +/* + MList is a garbage collector for arrays created using Alloc* or Allocate* (*=2, 3, 4). After being + added to the list the arrays will be automatically freed when MList is deleted. + + Using MList: + Create an MList object and add all buffers (1D, 2D, 3D or 4D) to be recycled to the MList using + MList::Add(...). Deleting the MList will recycle all the added buffers. + + Example: + Alloc2L(4, N, x, mlist) declares 2D array x and registers it with garbage collector mlist,so that x is + freed when mlist is deleted. +*/ +class MList +{ +public: + int cap[4]; + int count[4]; + void** List[4]; + MList(){for (int i=0; i<4; i++) cap[i]=64, count[i]=0, List[i]=(void**)malloc(sizeof(void*)*cap[i]);} + ~MList(){ + for (int i=0; i<count[0]; i++){delete[] (char*)(List[0][i]); List[0][i]=0;} free(List[0]); + for (int i=0; i<count[1]; i++){void** tmp=(void**)List[1][i]; DeAlloc2(tmp); List[1][i]=0;} free(List[1]); + for (int i=0; i<count[2]; i++){void*** tmp=(void***)List[2][i]; DeAlloc3(tmp); List[2][i]=0;} free(List[2]); + for (int i=0; i<count[3]; i++){void**** tmp=(void****)List[3][i]; DeAlloc4(tmp); List[3][i]=0;} free(List[3]);} + void __fastcall Add(void* item, int Dim){ + int Gr=Dim-1; if (count[Gr]==cap[Gr]) IncCap(Gr); List[Gr][count[Gr]++]=item;} + void IncCap(int Gr){cap[Gr]+=64; List[Gr]=(void**)realloc(List[Gr], sizeof(void*)*cap[Gr]);} +}; + +#endif