libStatGen Software  1
MemoryAllocators.cpp
00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include "MemoryAllocators.h"
00019 
00020 #include <stdlib.h>
00021 
00022 char *** AllocateCharCube(int n, int rows, int cols)
00023 {
00024     char *** cube = new char ** [n];
00025 
00026     // Stop early if we are out of memory
00027     if (cube == NULL)
00028         return NULL;
00029 
00030     for (int i = 0; i < n; i++)
00031     {
00032         cube[i] = AllocateCharMatrix(rows, cols);
00033 
00034         // Safely unravel allocation if we run out of memory
00035         if (cube[i] == NULL)
00036         {
00037             while (i--)
00038                 FreeCharMatrix(cube[i], rows);
00039 
00040             delete [] cube;
00041 
00042             return NULL;
00043         }
00044     }
00045 
00046     return cube;
00047 }
00048 
00049 int ** AllocateIntMatrix(int rows, int cols)
00050 {
00051     int ** matrix = new int * [rows];
00052 
00053     // Stop early if we are out of memory
00054     if (matrix == NULL)
00055         return NULL;
00056 
00057     for (int i = 0; i < rows; i++)
00058     {
00059         matrix[i] = new int [cols];
00060 
00061         // Safely unravel allocation if we run out of memory
00062         if (matrix[i] == NULL)
00063         {
00064             while (i--)
00065                 delete [] matrix[i];
00066 
00067             delete [] matrix;
00068 
00069             return NULL;
00070         }
00071     }
00072 
00073     return matrix;
00074 }
00075 
00076 char ** AllocateCharMatrix(int rows, int cols)
00077 {
00078     char ** matrix = new char * [rows];
00079 
00080     // Stop early if we are out of memory
00081     if (matrix == NULL)
00082         return NULL;
00083 
00084     for (int i = 0; i < rows; i++)
00085     {
00086         matrix[i] = new char [cols];
00087 
00088         // Safely unravel allocation if we run out of memory
00089         if (matrix[i] == NULL)
00090         {
00091             while (i--)
00092                 delete [] matrix[i];
00093 
00094             delete [] matrix;
00095 
00096             return NULL;
00097         }
00098     }
00099 
00100     return matrix;
00101 }
00102 
00103 float ** AllocateFloatMatrix(int rows, int cols)
00104 {
00105     float ** matrix = new float * [rows];
00106 
00107     // Stop early if we are out of memory
00108     if (matrix == NULL)
00109         return NULL;
00110 
00111     for (int i = 0; i < rows; i++)
00112     {
00113         matrix[i] = new float [cols];
00114 
00115         // Safely unravel allocation if we run out of memory
00116         if (matrix[i] == NULL)
00117         {
00118             while (i--)
00119                 delete [] matrix[i];
00120 
00121             delete [] matrix;
00122 
00123             return NULL;
00124         }
00125     }
00126 
00127     return matrix;
00128 }
00129 
00130 void FreeCharCube(char *** & cube, int n, int rows)
00131 {
00132     if (cube == NULL)
00133         return;
00134 
00135     for (int i = 0; i < n; i++)
00136         FreeCharMatrix(cube[i], rows);
00137 
00138     delete [] cube;
00139 
00140     cube = NULL;
00141 }
00142 
00143 void FreeCharMatrix(char ** & matrix, int rows)
00144 {
00145     if (matrix == NULL)
00146         return;
00147 
00148     for (int i = 0; i < rows; i++)
00149         delete [] matrix[i];
00150 
00151     delete [] matrix;
00152 
00153     matrix = NULL;
00154 }
00155 
00156 void FreeFloatMatrix(float ** & matrix, int rows)
00157 {
00158     if (matrix == NULL)
00159         return;
00160 
00161     for (int i = 0; i < rows; i++)
00162         delete [] matrix[i];
00163 
00164     delete [] matrix;
00165 
00166     matrix = NULL;
00167 }
00168 
00169 void FreeIntMatrix(int ** & matrix, int rows)
00170 {
00171     if (matrix == NULL)
00172         return;
00173 
00174     for (int i = 0; i < rows; i++)
00175         delete [] matrix[i];
00176 
00177     delete [] matrix;
00178 
00179     matrix = NULL;
00180 }
00181 
00182 short ** AllocateShortMatrix(int rows, int cols)
00183 {
00184     short ** matrix = new short * [rows];
00185 
00186     // Stop early if we are out of memory
00187     if (matrix == NULL)
00188         return NULL;
00189 
00190     for (int i = 0; i < rows; i++)
00191     {
00192         matrix[i] = new short [cols];
00193 
00194         // Safely unravel allocation if we run out of memory
00195         if (matrix[i] == NULL)
00196         {
00197             while (i--)
00198                 delete [] matrix[i];
00199 
00200             delete [] matrix;
00201 
00202             return NULL;
00203         }
00204     }
00205 
00206     return matrix;
00207 }
00208 
00209 void FreeShortMatrix(short ** & matrix, int rows)
00210 {
00211     if (matrix == NULL)
00212         return;
00213 
00214     for (int i = 0; i < rows; i++)
00215         delete [] matrix[i];
00216 
00217     delete [] matrix;
00218 
00219     matrix = NULL;
00220 }
00221 
00222 double ** AllocateDoubleMatrix(int rows, int cols)
00223 {
00224     double ** matrix = new double * [rows];
00225 
00226     // Stop early if we are out of memory
00227     if (matrix == NULL)
00228         return NULL;
00229 
00230     for (int i = 0; i < rows; i++)
00231     {
00232         matrix[i] = new double [cols];
00233 
00234         // Safely unravel allocation if we run out of memory
00235         if (matrix[i] == NULL)
00236         {
00237             while (i--)
00238                 delete [] matrix[i];
00239 
00240             delete [] matrix;
00241 
00242             return NULL;
00243         }
00244     }
00245 
00246     return matrix;
00247 }
00248 
00249 void FreeDoubleMatrix(double ** & matrix, int rows)
00250 {
00251     for (int i = 0; i < rows; i++)
00252         delete [] matrix[i];
00253 
00254     delete [] matrix;
00255 
00256     matrix = NULL;
00257 }
00258 
00259 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends