arrayalloc.h
Go to the documentation of this file.
1 /*
2  Harmonic sinusoidal modelling and tools
3 
4  C++ code package for harmonic sinusoidal modelling and relevant signal processing.
5  Centre for Digital Music, Queen Mary, University of London.
6  This file copyright 2011 Wen Xue.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12 */
13 #ifndef ARRAYALLOC
14 #define ARRAYALLOC
15 
31 #include <stdlib.h>
32 
33 //2D array allocation macros for declaring an array of double
34 #define Alloc2(M, N, x) \
35  double **x=new double*[M]; x[0]=new double[(M)*(N)]; for (int _z=1; _z<M; _z++) x[_z]=&x[0][_z*(N)];
36 #define Alloc2L(M, N, x, LIST) Alloc2(M, N, x); if (LIST) LIST->Add(x, 2);
37 //2D array allocation macros for all data types
38 #define Allocate2(INT, M, N, x) \
39  x=new INT*[M]; x[0]=new INT[(M)*(N)]; for (int _z=1; _z<M; _z++) x[_z]=&x[0][_z*(N)];
40  #define Allocate2L(INT, M, N, x, LIST) Allocate2(INT, M, N, x); if (LIST) LIST->Add(x, 2);
41 //2D array deallocation macro
42 #define DeAlloc2(x) {if (x) {delete[] (char*)(x[0]); delete[] x; x=0;}}
43 
44 //3D array allocation macro for declaring an array of double
45 #define Alloc3(L, M, N, x) \
46  double*** x=new double**[L]; x[0]=new double*[(L)*(M)]; x[0][0]=new double[(L)*(M)*(N)]; \
47  for (int _z=0; _z<L; _z++) {x[_z]=&x[0][_z*(M)]; x[_z][0]=&x[0][0][_z*(M)*(N)]; \
48  for (int __z=1; __z<M; __z++) x[_z][__z]=&x[_z][0][__z*(N)]; }
49 //3D array allocation macros for all data types
50 #define Allocate3(INT, L, M, N, x) \
51  x=new INT**[L]; x[0]=new INT*[(L)*(M)]; x[0][0]=new INT[(L)*(M)*(N)]; \
52  for (int _z=0; _z<L; _z++) {x[_z]=&x[0][_z*(M)]; x[_z][0]=&x[0][0][_z*(M)*(N)]; \
53  for (int __z=1; __z<M; __z++) x[_z][__z]=&x[_z][0][__z*(N)]; }
54  #define Allocate3L(INT, M, N, O, x, LIST) Allocate3(INT, M, N, O, x); if (LIST) LIST->Add(x, 3);
55 //3D array deallocation macro
56 #define DeAlloc3(x) {if (x) {delete[] (char*)(x[0][0]); delete[] x[0]; delete[] x; x=0;}}
57 
58 //4D array allocation macro for declaring an array of double
59 #define Alloc4(L, M, N, O, x) \
60  double**** x=new double***[L]; x[0]=new double**[(L)*(M)]; \
61  x[0][0]=new double*[(L)*(M)*(N)]; x[0][0][0]=new double[(L)*(M)*(N)*(O)]; \
62  for (int _z=0; _z<L; _z++){ \
63  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)]; \
64  for (int __z=0; __z<M; __z++){ \
65  x[_z][__z]=&x[_z][0][__z*(N)]; x[_z][__z][0]=&x[_z][0][0][__z*(N)*(O)]; \
66  for (int ___z=1; ___z<N; ___z++) x[_z][__z][___z]=&d[_z][__z][0][___z*(O)]; }}
67 //4D array deallocation macro
68 #define DeAlloc4(x) {if (x) {delete[] (char*)(x[0][0][0]); delete[] x[0][0]; delete[] x[0]; delete[] x; x=0;}}
69 
70 
71 /*
72  MList is a garbage collector for arrays created using Alloc* or Allocate* (*=2, 3, 4). After being
73  added to the list the arrays will be automatically freed when MList is deleted.
74 
75  Using MList:
76  Create an MList object and add all buffers (1D, 2D, 3D or 4D) to be recycled to the MList using
77  MList::Add(...). Deleting the MList will recycle all the added buffers.
78 
79  Example:
80  Alloc2L(4, N, x, mlist) declares 2D array x and registers it with garbage collector mlist,so that x is
81  freed when mlist is deleted.
82 */
83 class MList
84 {
85 public:
86  int cap[4];
87  int count[4];
88  void** List[4];
89  MList(){for (int i=0; i<4; i++) cap[i]=64, count[i]=0, List[i]=(void**)malloc(sizeof(void*)*cap[i]);}
90  ~MList(){
91  for (int i=0; i<count[0]; i++){delete[] (char*)(List[0][i]); List[0][i]=0;} free(List[0]);
92  for (int i=0; i<count[1]; i++){void** tmp=(void**)List[1][i]; DeAlloc2(tmp); List[1][i]=0;} free(List[1]);
93  for (int i=0; i<count[2]; i++){void*** tmp=(void***)List[2][i]; DeAlloc3(tmp); List[2][i]=0;} free(List[2]);
94  for (int i=0; i<count[3]; i++){void**** tmp=(void****)List[3][i]; DeAlloc4(tmp); List[3][i]=0;} free(List[3]);}
95  void __fastcall Add(void* item, int Dim){
96  int Gr=Dim-1; if (count[Gr]==cap[Gr]) IncCap(Gr); List[Gr][count[Gr]++]=item;}
97  void IncCap(int Gr){cap[Gr]+=64; List[Gr]=(void**)realloc(List[Gr], sizeof(void*)*cap[Gr]);}
98 };
99 
100 #endif
Definition: arrayalloc.h:83