1 /*
2  *  Copyright (C) 2010  Regents of the University of Michigan
3  *
4  *   This program is free software: you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License as published by
6  *   the Free Software Foundation, either version 3 of the License, or
7  *   (at your option) any later version.
8  *
9  *   This program is distributed in the hope that it will be useful,
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *   GNU General Public License for more details.
13  *
14  *   You should have received a copy of the GNU General Public License
15  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "MemoryAllocators.h"
19 
20 #include <stdlib.h>
21 
AllocateCharCube(int n,int rows,int cols)22 char *** AllocateCharCube(int n, int rows, int cols)
23 {
24     char *** cube = new char ** [n];
25 
26     // Stop early if we are out of memory
27     if (cube == NULL)
28         return NULL;
29 
30     for (int i = 0; i < n; i++)
31     {
32         cube[i] = AllocateCharMatrix(rows, cols);
33 
34         // Safely unravel allocation if we run out of memory
35         if (cube[i] == NULL)
36         {
37             while (i--)
38                 FreeCharMatrix(cube[i], rows);
39 
40             delete [] cube;
41 
42             return NULL;
43         }
44     }
45 
46     return cube;
47 }
48 
AllocateIntMatrix(int rows,int cols)49 int ** AllocateIntMatrix(int rows, int cols)
50 {
51     int ** matrix = new int * [rows];
52 
53     // Stop early if we are out of memory
54     if (matrix == NULL)
55         return NULL;
56 
57     for (int i = 0; i < rows; i++)
58     {
59         matrix[i] = new int [cols];
60 
61         // Safely unravel allocation if we run out of memory
62         if (matrix[i] == NULL)
63         {
64             while (i--)
65                 delete [] matrix[i];
66 
67             delete [] matrix;
68 
69             return NULL;
70         }
71     }
72 
73     return matrix;
74 }
75 
AllocateCharMatrix(int rows,int cols)76 char ** AllocateCharMatrix(int rows, int cols)
77 {
78     char ** matrix = new char * [rows];
79 
80     // Stop early if we are out of memory
81     if (matrix == NULL)
82         return NULL;
83 
84     for (int i = 0; i < rows; i++)
85     {
86         matrix[i] = new char [cols];
87 
88         // Safely unravel allocation if we run out of memory
89         if (matrix[i] == NULL)
90         {
91             while (i--)
92                 delete [] matrix[i];
93 
94             delete [] matrix;
95 
96             return NULL;
97         }
98     }
99 
100     return matrix;
101 }
102 
AllocateFloatMatrix(int rows,int cols)103 float ** AllocateFloatMatrix(int rows, int cols)
104 {
105     float ** matrix = new float * [rows];
106 
107     // Stop early if we are out of memory
108     if (matrix == NULL)
109         return NULL;
110 
111     for (int i = 0; i < rows; i++)
112     {
113         matrix[i] = new float [cols];
114 
115         // Safely unravel allocation if we run out of memory
116         if (matrix[i] == NULL)
117         {
118             while (i--)
119                 delete [] matrix[i];
120 
121             delete [] matrix;
122 
123             return NULL;
124         }
125     }
126 
127     return matrix;
128 }
129 
FreeCharCube(char *** & cube,int n,int rows)130 void FreeCharCube(char *** & cube, int n, int rows)
131 {
132     if (cube == NULL)
133         return;
134 
135     for (int i = 0; i < n; i++)
136         FreeCharMatrix(cube[i], rows);
137 
138     delete [] cube;
139 
140     cube = NULL;
141 }
142 
FreeCharMatrix(char ** & matrix,int rows)143 void FreeCharMatrix(char ** & matrix, int rows)
144 {
145     if (matrix == NULL)
146         return;
147 
148     for (int i = 0; i < rows; i++)
149         delete [] matrix[i];
150 
151     delete [] matrix;
152 
153     matrix = NULL;
154 }
155 
FreeFloatMatrix(float ** & matrix,int rows)156 void FreeFloatMatrix(float ** & matrix, int rows)
157 {
158     if (matrix == NULL)
159         return;
160 
161     for (int i = 0; i < rows; i++)
162         delete [] matrix[i];
163 
164     delete [] matrix;
165 
166     matrix = NULL;
167 }
168 
FreeIntMatrix(int ** & matrix,int rows)169 void FreeIntMatrix(int ** & matrix, int rows)
170 {
171     if (matrix == NULL)
172         return;
173 
174     for (int i = 0; i < rows; i++)
175         delete [] matrix[i];
176 
177     delete [] matrix;
178 
179     matrix = NULL;
180 }
181 
AllocateShortMatrix(int rows,int cols)182 short ** AllocateShortMatrix(int rows, int cols)
183 {
184     short ** matrix = new short * [rows];
185 
186     // Stop early if we are out of memory
187     if (matrix == NULL)
188         return NULL;
189 
190     for (int i = 0; i < rows; i++)
191     {
192         matrix[i] = new short [cols];
193 
194         // Safely unravel allocation if we run out of memory
195         if (matrix[i] == NULL)
196         {
197             while (i--)
198                 delete [] matrix[i];
199 
200             delete [] matrix;
201 
202             return NULL;
203         }
204     }
205 
206     return matrix;
207 }
208 
FreeShortMatrix(short ** & matrix,int rows)209 void FreeShortMatrix(short ** & matrix, int rows)
210 {
211     if (matrix == NULL)
212         return;
213 
214     for (int i = 0; i < rows; i++)
215         delete [] matrix[i];
216 
217     delete [] matrix;
218 
219     matrix = NULL;
220 }
221 
AllocateDoubleMatrix(int rows,int cols)222 double ** AllocateDoubleMatrix(int rows, int cols)
223 {
224     double ** matrix = new double * [rows];
225 
226     // Stop early if we are out of memory
227     if (matrix == NULL)
228         return NULL;
229 
230     for (int i = 0; i < rows; i++)
231     {
232         matrix[i] = new double [cols];
233 
234         // Safely unravel allocation if we run out of memory
235         if (matrix[i] == NULL)
236         {
237             while (i--)
238                 delete [] matrix[i];
239 
240             delete [] matrix;
241 
242             return NULL;
243         }
244     }
245 
246     return matrix;
247 }
248 
FreeDoubleMatrix(double ** & matrix,int rows)249 void FreeDoubleMatrix(double ** & matrix, int rows)
250 {
251     for (int i = 0; i < rows; i++)
252         delete [] matrix[i];
253 
254     delete [] matrix;
255 
256     matrix = NULL;
257 }
258 
259 
260