Fast Auxiliary Space Preconditioning 2.7.7 Aug/28/2022
AuxMemory.c
Go to the documentation of this file.
1
13/*---------------------------------*/
14/*-- Declare External Functions --*/
15/*---------------------------------*/
16
17#include "fasp.h"
18#include "fasp_functs.h"
19
20#if DLMALLOC
21#include "dlmalloc.h"
22#elif NEDMALLOC
23#include "nedmalloc.h"
24#ifdef __cplusplus
25extern "C" {
26#endif
27 void * nedcalloc(size_t no, size_t size);
28 void * nedrealloc(void *mem, size_t size);
29 void nedfree(void *mem);
30#ifdef __cplusplus
31}
32#endif
33#endif
34
35#if DEBUG_MODE > 1
36extern unsigned long total_alloc_mem;
37extern unsigned long total_alloc_count;
38#endif
39
40/*---------------------------------*/
41/*-- Global Variables --*/
42/*---------------------------------*/
43
44const int Million = 1048576;
46/*---------------------------------*/
47/*-- Public Functions --*/
48/*---------------------------------*/
49
65void * fasp_mem_calloc (const unsigned int size,
66 const unsigned int type)
67{
68 const LONGLONG tsize = size*type;
69 void * mem = NULL;
70
71#if DEBUG_MODE > 1
72 printf("### DEBUG: Trying to allocate %.3lfMB RAM!\n", (REAL)tsize/Million);
73#endif
74
75 if ( tsize > 0 ) {
76
77#if DLMALLOC
78 mem = dlcalloc(size,type);
79#elif NEDMALLOC
80 mem = nedcalloc(size,type);
81#else
82 mem = calloc(size,type);
83#endif
84
85#if DEBUG_MODE > 1
86 total_alloc_mem += tsize;
88#endif
89 }
90
91 if ( mem == NULL ) {
92 printf("### WARNING: Trying to allocate %lldB RAM...\n", tsize);
93 printf("### WARNING: Cannot allocate %.4fMB RAM!\n", (REAL)tsize/Million);
94 }
95
96 return mem;
97}
98
114void * fasp_mem_realloc (void *oldmem,
115 const LONGLONG tsize)
116{
117 void * mem = NULL;
118
119#if DEBUG_MODE > 1
120 printf("### DEBUG: Trying to allocate %.3lfMB RAM!\n", (REAL)tsize/Million);
121#endif
122
123 if ( tsize > 0 ) {
124
125#if DLMALLOC
126 mem = dlrealloc(oldmem,tsize);
127#elif NEDMALLOC
128 mem = nedrealloc(oldmem,tsize);
129#else
130 mem = realloc(oldmem,tsize);
131#endif
132
133 }
134
135 if ( mem == NULL ) {
136 printf("### WARNING: Trying to allocate %lldB RAM!\n", tsize);
137 printf("### WARNING: Cannot allocate %.3lfMB RAM!\n", (REAL)tsize/Million);
138 }
139
140 return mem;
141}
142
155void fasp_mem_free (void *mem)
156{
157 if ( mem ) {
158#if DLMALLOC
159 dlfree(mem);
160#elif NEDMALLOC
161 nedfree(mem);
162#else
163 free(mem);
164#endif
165
166#if DEBUG_MODE > 1
168#endif
169 }
170 else {
171#if DEBUG_MODE > 1
172 printf("### WARNING: Trying to free an empty pointer!\n");
173#endif
174 }
175}
176
185void fasp_mem_usage ( void )
186{
187#if DEBUG_MODE > 1
188 printf("### DEBUG: Number of alloc = %ld, allocated memory = %.3fMB.\n",
190#endif
191}
192
206{
207 const INT memneed = 2*iludata->row; // estimated memory usage
208
209 if ( iludata->nwork >= memneed ) {
210 return FASP_SUCCESS;
211 }
212 else {
213 printf("### ERROR: ILU needs %d RAM, only %d available!\n",
214 memneed, iludata->nwork);
215 return ERROR_ALLOC_MEM;
216 }
217}
218
219/*---------------------------------*/
220/*-- End of File --*/
221/*---------------------------------*/
void fasp_mem_free(void *mem)
Free up previous allocated memory body and set pointer to NULL.
Definition: AuxMemory.c:155
void fasp_mem_usage(void)
Show total allocated memory currently.
Definition: AuxMemory.c:185
const int Million
Definition: AuxMemory.c:44
SHORT fasp_mem_iludata_check(const ILU_data *iludata)
Check wether a ILU_data has enough work space.
Definition: AuxMemory.c:205
void * fasp_mem_realloc(void *oldmem, const LONGLONG tsize)
Reallocate, initiate, and check memory.
Definition: AuxMemory.c:114
void * fasp_mem_calloc(const unsigned int size, const unsigned int type)
Allocate, initiate, and check memory.
Definition: AuxMemory.c:65
unsigned long total_alloc_mem
unsigned long total_alloc_count
Main header file for the FASP project.
#define REAL
Definition: fasp.h:67
#define SHORT
FASP integer and floating point numbers.
Definition: fasp.h:63
#define LONGLONG
Definition: fasp.h:66
#define INT
Definition: fasp.h:64
#define FASP_SUCCESS
Definition of return status and error messages.
Definition: fasp_const.h:19
#define ERROR_ALLOC_MEM
Definition: fasp_const.h:30
Data for ILU setup.
Definition: fasp.h:637
INT nwork
work space size
Definition: fasp.h:664
INT row
row number of matrix LU, m
Definition: fasp.h:646