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 "bft_error.h"
50
/*-----------------------------------------------------------------------------*/
55
} /* Fake brace to force Emacs auto-indentation back to column 0 */
57
#endif /* __cplusplus */
59
/*============================================================================
61
*============================================================================*/
63
/*============================================================================
65
*============================================================================*/
68
* Allocate memory for _ni items of type _type.
70
* This macro calls bft_mem_malloc(), automatically setting the
71
* allocated variable name and source file name and line arguments.
74
* _ptr --> pointer to allocated memory.
75
* _ni <-- number of items.
76
* _type <-- element type.
79
#define BFT_MALLOC(_ptr, _ni, _type) \
80
_ptr = (_type *) bft_mem_malloc(_ni, sizeof(_type), \
81
#_ptr, __FILE__, __LINE__)
84
* Reallocate memory for _ni items of type _type.
86
* This macro calls bft_mem_realloc(), automatically setting the
87
* allocated variable name and source file name and line arguments.
90
* _ptr <-> pointer to allocated memory.
91
* _ni <-- number of items.
92
* _type <-- element type.
95
#define BFT_REALLOC(_ptr, _ni, _type) \
96
_ptr = (_type *) bft_mem_realloc(_ptr, _ni, sizeof(_type), \
97
#_ptr, __FILE__, __LINE__)
100
* Free allocated memory.
102
* This macro calls bft_mem_free(), automatically setting the
103
* allocated variable name and source file name and line arguments.
105
* The freed pointer is set to NULL to avoid accidental reuse.
108
* _ptr <-> pointer to allocated memory.
111
#ifdef __cplusplus /* avoid casting from void for C++ */
113
#define BFT_FREE(_ptr) \
114
bft_mem_free(_ptr, #_ptr, __FILE__, __LINE__), _ptr = NULL
118
#define BFT_FREE(_ptr) \
119
_ptr = bft_mem_free(_ptr, #_ptr, __FILE__, __LINE__)
121
#endif /* __cplusplus */
124
* Allocate aligned memory for _ni items of type _type.
126
* This macro calls bft_mem_memalign(), automatically setting the
127
* allocated variable name and source file name and line arguments.
130
* _ptr --> pointer to allocated memory.
131
* _align <-- alignment.
132
* _ni <-- number of items.
133
* _type <-- element type.
136
#define BFT_MEMALIGN(_ptr, _align, _ni, _type) \
137
_ptr = (_type *) bft_mem_memalign(_align, _ni, sizeof(_type), \
138
#_ptr, __FILE__, __LINE__)
140
/*============================================================================
141
* Public function prototypes
142
*============================================================================*/
145
* Initialize memory handling.
147
* This function should be called before any other bft_mem_...()
148
* function. To activate memory allocation logging, a logfile
149
* name should be given as an argument. The resulting file will
150
* be a regular, local file. If this file cannot be opened for
151
* some reason, logging is silently de-activated.
154
* log_file_name <-- name of optional log_file (if NULL, no log).
158
bft_mem_init(const char *log_file_name);
161
* End memory handling.
163
* This function should be called after all other bft_mem_...()
164
* functions. In case of memory allocation logging, it
165
* writes final information to the log file and closes is.
172
* Initialize memory handling.
174
* This function should be called before any other bft_mem_...()
175
* function. To activate memory allocation logging, a logfile
176
* name should be given as an argument. The resulting file will
177
* be a regular, local file. If this file cannot be opened for
178
* some reason, logging is silently de-activated.
181
* log_file_name <-- name of optional log_file (if NULL, no log).
185
* Indicates if bft_mem_...() functions are initialized.
188
* 1 if bft_mem_init has been called, 0 otherwise.
192
bft_mem_initialized(void);
195
* Allocate memory for ni items of size bytes.
197
* This function calls malloc(), but adds tracing capabilities, and
198
* automatically calls the bft_error() errorhandler if it fails to
199
* allocate the required memory.
202
* ni <-- number of items.
203
* size <-- element size.
204
* var_name <-- allocated variable name string.
205
* file_name <-- name of calling source file.
206
* line_num <-- line number in calling source file.
209
* pointer to allocated memory.
213
bft_mem_malloc(size_t ni,
215
const char *var_name,
216
const char *file_name,
220
* Reallocate memory for ni items of size bytes.
222
* This function calls realloc(), but adds tracing capabilities, and
223
* automatically calls the bft_error() errorhandler if it fails to
224
* allocate the required memory.
227
* ptr <-> pointer to previous memory location
228
* (if NULL, bft_alloc() called).
229
* ni <-- number of items.
230
* size <-- element size.
231
* var_name <-- allocated variable name string.
232
* file_name <-- name of calling source file.
233
* line_num -> line number in calling source file
236
* pointer to allocated memory.
240
bft_mem_realloc(void *ptr,
243
const char *var_name,
244
const char *file_name,
248
* Free allocated memory.
250
* This function calls free(), but adds tracing capabilities, and
251
* automatically calls the bft_error() errorhandler if it fails to
252
* free the corresponding memory. In case of a NULL pointer argument,
253
* the function simply returns.
256
* ptr <-> pointer to previous memory location
257
* (if NULL, bft_alloc() called).
258
* var_name <-- allocated variable name string.
259
* file_name <-- name of calling source file.
260
* line_num <-- line number in calling source file.
267
bft_mem_free(void *ptr,
268
const char *var_name,
269
const char *file_name,
273
* Allocate aligned memory for ni elements of size bytes.
275
* This function calls posix_memalign() if available, but adds tracing
276
* capabilities, and automatically calls the bft_error() errorhandler if
277
* it fails to allocate the required memory.
279
* The associated function bft_mem_have_memalign() indicates if this
280
* type of allocation may be used on this system.
283
* alignment <-- alignent.
284
* ni <-- number of items.
285
* size <-- element size.
286
* var_name <-- allocated variable name string.
287
* file_name <-- name of calling source file.
288
* line_num <-- line number in calling source file.
291
* pointer to allocated memory.
295
bft_mem_memalign(size_t alignment,
298
const char *var_name,
299
const char *file_name,
303
* Indicate if a memory aligned allocation variant is available.
305
* If no such function is available, bft_mem_memalign() will always fail.
308
* 1 if memory aligned allocation is possible, 0 otherwise.
312
bft_mem_have_memalign(void);
315
* Return current theoretical dynamic memory allocated.
318
* current memory handled through bft_mem_...() (in kB).
322
bft_mem_size_current(void);
325
* Return maximum theoretical dynamic memory allocated.
328
* maximum memory handled through bft_mem_...() (in kB).
332
bft_mem_size_max(void);
334
/* Returns the error handler associated with the bft_mem_...() functions.
337
* pointer to the error handler function.
340
bft_error_handler_t *
341
bft_mem_error_handler_get(void);
344
* Associates an error handler with the bft_mem_...() functions.
346
* With the default error handler, an error message is output to stderr,
347
* (after bft_print_flush() is called), and the general error handler used
348
* by bft_error() is then called (which results in the termination of the
349
* current process or process group).
352
* handler <-- pointer to the error handler function.
356
bft_mem_error_handler_set(bft_error_handler_t *handler);
358
/*----------------------------------------------------------------------------*/
362
#endif /* __cplusplus */
364
#endif /* __BFT_MEM_H__ */