Xmipp  v3.23.11-Nereus
xmipp_memory.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar S. Sorzano (coss@cnb.csic.es)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #ifndef _CORE_XMIPP_MEMORY
27 #define _CORE_XMIPP_MEMORY
28 
29 #include <stdlib.h>
30 #include "xmipp_error.h"
31 
32 /* Memory managing --------------------------------------------------------- */
35 
36 
40 template <class T> void ask_Tmatrix(T ** &m, int nrl, int nrh,
41  int ncl, int nch)
42 {
43  if (nrh - nrl + 1 >= 1 && nch - ncl + 1 >= 1)
44  {
45  m = (T **) malloc((unsigned)(nrh - nrl + 1) * sizeof(T*));
46  if (!m) REPORT_ERROR(ERR_MEM_NOTENOUGH, "allocation failure 1 in matrix()");
47  m -= nrl;
48 
49  for (int i = nrl;i <= nrh;i++)
50  {
51  m[i] = (T *) malloc((unsigned)(nch - ncl + 1) * sizeof(T));
52  if (!m[i]) REPORT_ERROR(ERR_MEM_NOTENOUGH, "allocation failure 2 in matrix()");
53  m[i] -= ncl;
54  }
55  }
56  else m = NULL;
57 }
58 
61 template <class T> void free_Tmatrix(T ** &m, int nrl, int nrh,
62  int ncl, int nch)
63 {
64  if (m != NULL)
65  {
66  for (int i = nrh;i >= nrl;i--) {
67  free((char*)(m[i] + nrl));
68  }
69  free((char*)(m));
70  m = NULL;
71  }
72 }
73 
78 template <class T> void ask_Tvolume(T *** &m, int nsl, int nsh, int nrl,
79  int nrh, int ncl, int nch)
80 {
81  if (nsh - nsl + 1 >= 1 && nrh - nrl + 1 >= 1 && nch - ncl + 1 >= 1)
82  {
83  m = (T ***) malloc((unsigned)(nsh - nsl + 1) * sizeof(T**));
84  if (!m) REPORT_ERROR(ERR_MEM_NOTENOUGH, "allocation failure 1 in matrix()");
85  m -= nsl;
86 
87  for (int k = nsl;k <= nsh;k++)
88  {
89  m[k] = (T **) malloc((unsigned)(nrh - nrl + 1) * sizeof(T*));
90  if (!m[k]) REPORT_ERROR(ERR_MEM_NOTENOUGH, "allocation failure 2 in matrix()");
91  m[k] -= nrl;
92 
93  for (int i = nrl;i <= nrh;i++)
94  {
95  m[k][i] = (T *) malloc((unsigned)(nch - ncl + 1) * sizeof(T));
96  if (!m[k][i]) REPORT_ERROR(ERR_MEM_NOTENOUGH, "allocation failure 2 in matrix()");
97  m[k][i] -= ncl;
98  }
99  }
100  }
101  else m = NULL;
102 }
103 
106 template <class T> void free_Tvolume(T *** &m, int nsl, int nsh,
107  int nrl, int nrh, int ncl, int nch)
108 {
109  if (m != NULL)
110  {
111  for (int k = nsh;k >= nsl;k--)
112  {
113  for (int i = nrh;i >= nrl;i--) {
114  free((char*)(m[k][i] + ncl));
115  }
116  free((char*)(m[k] + nrl));
117  }
118  free((char*)(m + nsl));
119  m = NULL;
120  }
121 }
122 
123 
137 char* askMemory(size_t size);
138 
148 int freeMemory(void* ptr, size_t memsize);
149 
151 #endif
void free_Tvolume(T ***&m, int nsl, int nsh, int nrl, int nrh, int ncl, int nch)
Definition: xmipp_memory.h:106
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
There is not enough memory for allocation.
Definition: xmipp_error.h:166
void free_Tmatrix(T **&m, int nrl, int nrh, int ncl, int nch)
Definition: xmipp_memory.h:61
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
int freeMemory(void *ptr, size_t memsize)
free((char *) ob)
char * askMemory(size_t size)
int m
void ask_Tvolume(T ***&m, int nsl, int nsh, int nrl, int nrh, int ncl, int nch)
Definition: xmipp_memory.h:78
void ask_Tmatrix(T **&m, int nrl, int nrh, int ncl, int nch)
Definition: xmipp_memory.h:40