4
/*============================================================================
5
* Base memory allocation wrappers with optional tracing
6
*============================================================================*/
9
This file is part of Code_Saturne, a general-purpose CFD tool.
11
Copyright (C) 1998-2011 EDF S.A.
13
This program is free software; you can redistribute it and/or modify it under
14
the terms of the GNU General Public License as published by the Free Software
15
Foundation; either version 2 of the License, or (at your option) any later
18
This program is distributed in the hope that it will be useful, but WITHOUT
19
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
23
You should have received a copy of the GNU General Public License along with
24
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
25
Street, Fifth Floor, Boston, MA 02110-1301, USA.
28
/*----------------------------------------------------------------------------*/
31
* Obtain definitions such as that of size_t through stddef.h (C99 standard)
32
* if available (preferred method), or through stdlib.h (which defines
33
* malloc() and family and so must define size_t some way) otherwise.
36
#if defined(__STDC_VERSION__)
37
# if (__STDC_VERSION__ == 199901L)
48
#include "cs_config.h"
51
/*-----------------------------------------------------------------------------*/
56
} /* Fake brace to force Emacs auto-indentation back to column 0 */
58
#endif /* __cplusplus */
60
/*============================================================================
62
*============================================================================*/
64
/*============================================================================
66
*============================================================================*/
69
* Allocate memory for _ni items of type _type.
71
* This macro calls ecs_mem_malloc(), automatically setting the
72
* allocated variable name and source file name and line arguments.
75
* _ptr --> pointer to allocated memory.
76
* _ni <-- number of items.
77
* _type <-- element type.
80
#define ECS_MALLOC(_ptr, _ni, _type) \
81
_ptr = (_type *) ecs_mem_malloc(_ni, sizeof(_type), \
82
#_ptr, __FILE__, __LINE__)
85
* Reallocate memory for _ni items of type _type.
87
* This macro calls ecs_mem_realloc(), automatically setting the
88
* allocated variable name and source file name and line arguments.
91
* _ptr <-> pointer to allocated memory.
92
* _ni <-- number of items.
93
* _type <-- element type.
96
#define ECS_REALLOC(_ptr, _ni, _type) \
97
_ptr = (_type *) ecs_mem_realloc(_ptr, _ni, sizeof(_type), \
98
#_ptr, __FILE__, __LINE__)
101
* Free allocated memory.
103
* This macro calls ecs_mem_free(), automatically setting the
104
* allocated variable name and source file name and line arguments.
106
* The freed pointer is set to NULL to avoid accidental reuse.
109
* _ptr <-> pointer to allocated memory.
112
#ifdef __cplusplus /* avoid casting from void for C++ */
114
#define ECS_FREE(_ptr) \
115
ecs_mem_free(_ptr, #_ptr, __FILE__, __LINE__), _ptr = NULL
119
#define ECS_FREE(_ptr) \
120
_ptr = ecs_mem_free(_ptr, #_ptr, __FILE__, __LINE__)
122
#endif /* __cplusplus */
125
* Allocate aligned memory for _ni items of type _type.
127
* This macro calls ecs_mem_memalign(), automatically setting the
128
* allocated variable name and source file name and line arguments.
131
* _ptr --> pointer to allocated memory.
132
* _align <-- alignment.
133
* _ni <-- number of items.
134
* _type <-- element type.
137
#define ECS_MEMALIGN(_ptr, _align, _ni, _type) \
138
_ptr = (_type *) ecs_mem_memalign(_align, _ni, sizeof(_type), \
139
#_ptr, __FILE__, __LINE__)
141
/*============================================================================
142
* Public function prototypes
143
*============================================================================*/
146
* Initialize memory handling.
148
* This function should be called before any other ecs_mem_...()
149
* function. To activate memory allocation logging, a logfile
150
* name should be given as an argument. The resulting file will
151
* be a regular, local file. If this file cannot be opened for
152
* some reason, logging is silently de-activated.
155
* log_file_name <-- name of optional log_file (if NULL, no log).
159
ecs_mem_init(const char *log_file_name);
162
* End memory handling.
164
* This function should be called after all other ecs_mem_...()
165
* functions. In case of memory allocation logging, it
166
* writes final information to the log file and closes is.
173
* Initialize memory handling.
175
* This function should be called before any other ecs_mem_...()
176
* function. To activate memory allocation logging, a logfile
177
* name should be given as an argument. The resulting file will
178
* be a regular, local file. If this file cannot be opened for
179
* some reason, logging is silently de-activated.
182
* log_file_name <-- name of optional log_file (if NULL, no log).
186
* Indicates if ecs_mem_...() functions are initialized.
189
* 1 if ecs_mem_init has been called, 0 otherwise.
193
ecs_mem_initialized(void);
196
* Allocate memory for ni items of size bytes.
198
* This function calls malloc(), but adds tracing capabilities, and
199
* automatically calls the ecs_error() errorhandler if it fails to
200
* allocate the required memory.
203
* ni <-- number of items.
204
* size <-- element size.
205
* var_name <-- allocated variable name string.
206
* file_name <-- name of calling source file.
207
* line_num <-- line number in calling source file.
210
* pointer to allocated memory.
214
ecs_mem_malloc(size_t ni,
216
const char *var_name,
217
const char *file_name,
221
* Reallocate memory for ni items of size bytes.
223
* This function calls realloc(), but adds tracing capabilities, and
224
* automatically calls the ecs_error() errorhandler if it fails to
225
* allocate the required memory.
228
* ptr <-> pointer to previous memory location
229
* (if NULL, ecs_alloc() called).
230
* ni <-- number of items.
231
* size <-- element size.
232
* var_name <-- allocated variable name string.
233
* file_name <-- name of calling source file.
234
* line_num -> line number in calling source file
237
* pointer to allocated memory.
241
ecs_mem_realloc(void *ptr,
244
const char *var_name,
245
const char *file_name,
249
* Free allocated memory.
251
* This function calls free(), but adds tracing capabilities, and
252
* automatically calls the ecs_error() errorhandler if it fails to
253
* free the corresponding memory. In case of a NULL pointer argument,
254
* the function simply returns.
257
* ptr <-> pointer to previous memory location
258
* (if NULL, ecs_alloc() called).
259
* var_name <-- allocated variable name string.
260
* file_name <-- name of calling source file.
261
* line_num <-- line number in calling source file.
268
ecs_mem_free(void *ptr,
269
const char *var_name,
270
const char *file_name,
274
* Return current theoretical dynamic memory allocated.
277
* current memory handled through ecs_mem_...() (in kB).
281
ecs_mem_size_current(void);
284
* Return maximum theoretical dynamic memory allocated.
287
* maximum memory handled through ecs_mem_...() (in kB).
291
ecs_mem_size_max(void);
293
/*----------------------------------------------------------------------------*/
297
#endif /* __cplusplus */
299
#endif /* __ECS_MEM_H__ */