Fast Auxiliary Space Preconditioning 2.7.7 Aug/28/2022
Loading...
Searching...
No Matches
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
27void* nedcalloc(size_t no, size_t size);
28void* nedrealloc(void* mem, size_t size);
29void 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, const unsigned int type)
66{
67 const LONGLONG tsize = size * type;
68 void* mem = NULL;
69
70#if DEBUG_MODE > 1
71 printf("### DEBUG: Trying to allocate %.3lfMB RAM!\n", (REAL)tsize / Million);
72#endif
73
74 if (tsize > 0) {
75
76#if DLMALLOC
77 mem = dlcalloc(size, type);
78#elif NEDMALLOC
79 mem = nedcalloc(size, type);
80#else
81 mem = calloc(size, type);
82#endif
83
84#if DEBUG_MODE > 1
85 total_alloc_mem += tsize;
87#endif
88 }
89
90 if (mem == NULL) {
91 printf("### WARNING: Trying to allocate %lldB RAM...\n", tsize);
92 printf("### WARNING: Cannot allocate %.4fMB RAM!\n", (REAL)tsize / Million);
93 }
94
95 return mem;
96}
97
113void* fasp_mem_realloc(void* oldmem, const LONGLONG tsize)
114{
115 void* mem = NULL;
116
117#if DEBUG_MODE > 1
118 printf("### DEBUG: Trying to allocate %.3lfMB RAM!\n", (REAL)tsize / Million);
119#endif
120
121 if (tsize > 0) {
122
123#if DLMALLOC
124 mem = dlrealloc(oldmem, tsize);
125#elif NEDMALLOC
126 mem = nedrealloc(oldmem, tsize);
127#else
128 mem = realloc(oldmem, tsize);
129#endif
130 }
131
132 if (mem == NULL) {
133 printf("### WARNING: Trying to allocate %lldB RAM!\n", tsize);
134 printf("### WARNING: Cannot allocate %.3lfMB RAM!\n", (REAL)tsize / Million);
135 }
136
137 return mem;
138}
139
152void fasp_mem_free(void* mem)
153{
154 if (mem) {
155#if DLMALLOC
156 dlfree(mem);
157#elif NEDMALLOC
158 nedfree(mem);
159#else
160 free(mem);
161#endif
162
163 mem = NULL;
164
165#if DEBUG_MODE > 1
167#endif
168 } else {
169#if DEBUG_MODE > 1
170 printf("### WARNING: Trying to free an empty pointer!\n");
171#endif
172 }
173}
174
184{
185#if DEBUG_MODE > 1
186 printf("### DEBUG: Number of alloc = %ld, allocated memory = %.3fMB.\n",
188#endif
189}
190
204{
205 const INT memneed = 2 * iludata->row; // estimated memory usage
206
207 if (iludata->nwork >= memneed) {
208 return FASP_SUCCESS;
209 } else {
210 printf("### ERROR: ILU needs %d RAM, only %d available!\n", memneed,
211 iludata->nwork);
212 return ERROR_ALLOC_MEM;
213 }
214}
215
216/*---------------------------------*/
217/*-- End of File --*/
218/*---------------------------------*/
void fasp_mem_free(void *mem)
Free up previous allocated memory body and set pointer to NULL.
Definition: AuxMemory.c:152
void fasp_mem_usage(void)
Show total allocated memory currently.
Definition: AuxMemory.c:183
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:203
void * fasp_mem_realloc(void *oldmem, const LONGLONG tsize)
Reallocate, initiate, and check memory.
Definition: AuxMemory.c:113
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:75
#define SHORT
FASP integer and floating point numbers.
Definition: fasp.h:71
#define LONGLONG
Definition: fasp.h:74
#define INT
Definition: fasp.h:72
#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:651
INT nwork
work space size
Definition: fasp.h:678
INT row
row number of matrix LU, m
Definition: fasp.h:660