Xmipp  v3.23.11-Nereus
Functions
Memory management for numerical recipes
Collaboration diagram for Memory management for numerical recipes:

Functions

template<class T >
void ask_Tmatrix (T **&m, int nrl, int nrh, int ncl, int nch)
 
template<class T >
void free_Tmatrix (T **&m, int nrl, int nrh, int ncl, int nch)
 
template<class T >
void ask_Tvolume (T ***&m, int nsl, int nsh, int nrl, int nrh, int ncl, int nch)
 
template<class T >
void free_Tvolume (T ***&m, int nsl, int nsh, int nrl, int nrh, int ncl, int nch)
 
char * askMemory (size_t size)
 
int freeMemory (void *ptr, size_t memsize)
 

Detailed Description

Function Documentation

◆ ask_Tmatrix()

template<class T >
void ask_Tmatrix ( T **&  m,
int  nrl,
int  nrh,
int  ncl,
int  nch 
)

Ask memory for any type matrix. The valid values range from v[nrl][ncl] to v[nrh][nch]. If no memory is available an exception is thrown. NULL is returned if any nh is not greater than its nl

Definition at line 40 of file xmipp_memory.h.

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 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
There is not enough memory for allocation.
Definition: xmipp_error.h:166
#define i
int m

◆ ask_Tvolume()

template<class T >
void ask_Tvolume ( T ***&  m,
int  nsl,
int  nsh,
int  nrl,
int  nrh,
int  ncl,
int  nch 
)

Ask memory for any type voliume. The valid values range from v[nsl][nrl][ncl] to v[nsh][nrh][nch]. If no memory is available an exception is thrown. NULL is returned if any nh is not greater than its nl.

Definition at line 78 of file xmipp_memory.h.

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 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
There is not enough memory for allocation.
Definition: xmipp_error.h:166
#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 m

◆ askMemory()

char* askMemory ( size_t  size)

Allocates memory. Adapted from Bsofts bfree

It is called exactly like malloc, with the following enhancements:

  • If allocation of zero bytes are requested it notifies the user.
  • NO LONGER TRUE: Successfully allocated memory is zeroed
  • Allocation is attempted and an error message is printed on failure.
  • All failures return a NULL pointer to allow error handling from calling functions.

returns char* : a pointer to the memory (NULL on failure)

Definition at line 29 of file xmipp_memory.cpp.

30 {
31  char* ptr = NULL;
32 
33  if ( memsize == 0 )
34  {
35  REPORT_ERROR(ERR_MEM_BADREQUEST, "Error in askMemory: Memory allocation size requested is zero!");
36  return(NULL);
37  }
38 
39  if ( ( ptr = (char *) calloc(1,memsize*sizeof(char)) ) == NULL )
40  {
41  REPORT_ERROR(ERR_MEM_NOTENOUGH, formatString("askMemory:: Memory allocation of %lu bytes failed",memsize));
42  return(NULL);
43  }
44 
45  //memset(ptr, 0, memsize);
46 
47  return(ptr);
48 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
There is not enough memory for allocation.
Definition: xmipp_error.h:166
Bad amount of memory requested.
Definition: xmipp_error.h:165
String formatString(const char *format,...)

◆ free_Tmatrix()

template<class T >
void free_Tmatrix ( T **&  m,
int  nrl,
int  nrh,
int  ncl,
int  nch 
)

Free memory associated to any type matrix. After freeing v=NULL

Definition at line 61 of file xmipp_memory.h.

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 }
#define i
free((char *) ob)
int m

◆ free_Tvolume()

template<class T >
void free_Tvolume ( T ***&  m,
int  nsl,
int  nsh,
int  nrl,
int  nrh,
int  ncl,
int  nch 
)

Free memory associated to any type volume. After freeing v=NULL

Definition at line 106 of file xmipp_memory.h.

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 }
#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
free((char *) ob)
int m

◆ freeMemory()

int freeMemory ( void *  ptr,
size_t  memsize 
)

Frees allocated memory. Adapted from Bsofts bfree

It is called exactly like free, with the following enhancements:

  • If freeing fails an error message is printed.
  • the pointer is reset to NULL

returns int: 0 = success, -1 = failure.

Definition at line 50 of file xmipp_memory.cpp.

51 {
52  if ( ptr == NULL )
53  return(0);
54 
55  if ( memsize < 1 )
56  {
57  return(-1);
58  }
59 
60  free(ptr);
61  ptr = NULL;
62  return(0);
63 }
free((char *) ob)