~ubuntu-branches/ubuntu/precise/code-saturne/precise

« back to all changes in this revision

Viewing changes to preprocessor/util/ecs_mem.h

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2011-11-01 17:43:32 UTC
  • mto: (6.1.7 sid)
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: package-import@ubuntu.com-20111101174332-tl4vk45no0x3emc3
Tags: upstream-2.1.0
ImportĀ upstreamĀ versionĀ 2.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef __ECS_MEM_H__
 
2
#define __ECS_MEM_H__
 
3
 
 
4
/*============================================================================
 
5
 * Base memory allocation wrappers with optional tracing
 
6
 *============================================================================*/
 
7
 
 
8
/*
 
9
  This file is part of Code_Saturne, a general-purpose CFD tool.
 
10
 
 
11
  Copyright (C) 1998-2011 EDF S.A.
 
12
 
 
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
 
16
  version.
 
17
 
 
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
 
21
  details.
 
22
 
 
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.
 
26
*/
 
27
 
 
28
/*----------------------------------------------------------------------------*/
 
29
 
 
30
/*
 
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.
 
34
 */
 
35
 
 
36
#if defined(__STDC_VERSION__)
 
37
#  if (__STDC_VERSION__ == 199901L)
 
38
#    include <stddef.h>
 
39
#  else
 
40
#    include <stdlib.h>
 
41
#  endif
 
42
#else
 
43
#  include <stdlib.h>
 
44
#endif
 
45
 
 
46
/* ECS headers */
 
47
 
 
48
#include "cs_config.h"
 
49
#include "ecs_def.h"
 
50
 
 
51
/*-----------------------------------------------------------------------------*/
 
52
 
 
53
#ifdef __cplusplus
 
54
extern "C" {
 
55
#if 0
 
56
} /* Fake brace to force Emacs auto-indentation back to column 0 */
 
57
#endif
 
58
#endif /* __cplusplus */
 
59
 
 
60
/*============================================================================
 
61
 * Public types
 
62
 *============================================================================*/
 
63
 
 
64
/*============================================================================
 
65
 * Public macros
 
66
 *============================================================================*/
 
67
 
 
68
/*
 
69
 * Allocate memory for _ni items of type _type.
 
70
 *
 
71
 * This macro calls ecs_mem_malloc(), automatically setting the
 
72
 * allocated variable name and source file name and line arguments.
 
73
 *
 
74
 * parameters:
 
75
 *   _ptr  --> pointer to allocated memory.
 
76
 *   _ni   <-- number of items.
 
77
 *   _type <-- element type.
 
78
 */
 
79
 
 
80
#define ECS_MALLOC(_ptr, _ni, _type) \
 
81
_ptr = (_type *) ecs_mem_malloc(_ni, sizeof(_type), \
 
82
                                #_ptr, __FILE__, __LINE__)
 
83
 
 
84
/*
 
85
 * Reallocate memory for _ni items of type _type.
 
86
 *
 
87
 * This macro calls ecs_mem_realloc(), automatically setting the
 
88
 * allocated variable name and source file name and line arguments.
 
89
 *
 
90
 * parameters:
 
91
 *   _ptr  <->  pointer to allocated memory.
 
92
 *   _ni   <-- number of items.
 
93
 *   _type <-- element type.
 
94
 */
 
95
 
 
96
#define ECS_REALLOC(_ptr, _ni, _type) \
 
97
_ptr = (_type *) ecs_mem_realloc(_ptr, _ni, sizeof(_type), \
 
98
                                 #_ptr, __FILE__, __LINE__)
 
99
 
 
100
/*
 
101
 * Free allocated memory.
 
102
 *
 
103
 * This macro calls ecs_mem_free(), automatically setting the
 
104
 * allocated variable name and source file name and line arguments.
 
105
 *
 
106
 * The freed pointer is set to NULL to avoid accidental reuse.
 
107
 *
 
108
 * parameters:
 
109
 *   _ptr  <->  pointer to allocated memory.
 
110
 */
 
111
 
 
112
#ifdef __cplusplus /* avoid casting from void for C++ */
 
113
 
 
114
#define ECS_FREE(_ptr) \
 
115
ecs_mem_free(_ptr, #_ptr, __FILE__, __LINE__), _ptr = NULL
 
116
 
 
117
#else
 
118
 
 
119
#define ECS_FREE(_ptr) \
 
120
_ptr = ecs_mem_free(_ptr, #_ptr, __FILE__, __LINE__)
 
121
 
 
122
#endif /* __cplusplus */
 
123
 
 
124
/*
 
125
 * Allocate aligned memory for _ni items of type _type.
 
126
 *
 
127
 * This macro calls ecs_mem_memalign(), automatically setting the
 
128
 * allocated variable name and source file name and line arguments.
 
129
 *
 
130
 * parameters:
 
131
 *   _ptr    --> pointer to allocated memory.
 
132
 *   _align <-- alignment.
 
133
 *   _ni    <-- number of items.
 
134
 *   _type  <-- element type.
 
135
 */
 
136
 
 
137
#define ECS_MEMALIGN(_ptr, _align, _ni, _type) \
 
138
_ptr = (_type *) ecs_mem_memalign(_align, _ni, sizeof(_type), \
 
139
                                  #_ptr, __FILE__, __LINE__)
 
140
 
 
141
/*============================================================================
 
142
 * Public function prototypes
 
143
 *============================================================================*/
 
144
 
 
145
/*
 
146
 * Initialize memory handling.
 
147
 *
 
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.
 
153
 *
 
154
 * parameter:
 
155
 *   log_file_name <-- name of optional log_file (if NULL, no log).
 
156
 */
 
157
 
 
158
void
 
159
ecs_mem_init(const char  *log_file_name);
 
160
 
 
161
/*
 
162
 * End memory handling.
 
163
 *
 
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.
 
167
 */
 
168
 
 
169
void
 
170
ecs_mem_end(void);
 
171
 
 
172
/*
 
173
 * Initialize memory handling.
 
174
 *
 
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.
 
180
 *
 
181
 * parameter:
 
182
 *   log_file_name <-- name of optional log_file (if NULL, no log).
 
183
 */
 
184
 
 
185
/*
 
186
 * Indicates if ecs_mem_...() functions are initialized.
 
187
 *
 
188
 * returns:
 
189
 *   1 if ecs_mem_init has been called, 0 otherwise.
 
190
 */
 
191
 
 
192
int
 
193
ecs_mem_initialized(void);
 
194
 
 
195
/*
 
196
 * Allocate memory for ni items of size bytes.
 
197
 *
 
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.
 
201
 *
 
202
 * parameters:
 
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.
 
208
 *
 
209
 * returns:
 
210
 *   pointer to allocated memory.
 
211
 */
 
212
 
 
213
void *
 
214
ecs_mem_malloc(size_t       ni,
 
215
               size_t       size,
 
216
               const char  *var_name,
 
217
               const char  *file_name,
 
218
               int          line_num);
 
219
 
 
220
/*
 
221
 * Reallocate memory for ni items of size bytes.
 
222
 *
 
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.
 
226
 *
 
227
 * parameters:
 
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
 
235
 *
 
236
 * returns:
 
237
 *   pointer to allocated memory.
 
238
 */
 
239
 
 
240
void *
 
241
ecs_mem_realloc(void        *ptr,
 
242
                size_t       ni,
 
243
                size_t       size,
 
244
                const char  *var_name,
 
245
                const char  *file_name,
 
246
                int          line_num);
 
247
 
 
248
/*
 
249
 * Free allocated memory.
 
250
 *
 
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.
 
255
 *
 
256
 * parameters:
 
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.
 
262
 *
 
263
 * returns:
 
264
 *   NULL pointer.
 
265
 */
 
266
 
 
267
void *
 
268
ecs_mem_free(void        *ptr,
 
269
             const char  *var_name,
 
270
             const char  *file_name,
 
271
             int          line_num);
 
272
 
 
273
/*
 
274
 * Return current theoretical dynamic memory allocated.
 
275
 *
 
276
 * returns:
 
277
 *   current memory handled through ecs_mem_...() (in kB).
 
278
 */
 
279
 
 
280
size_t
 
281
ecs_mem_size_current(void);
 
282
 
 
283
/*
 
284
 * Return maximum theoretical dynamic memory allocated.
 
285
 *
 
286
 * returns:
 
287
 *   maximum memory handled through ecs_mem_...() (in kB).
 
288
 */
 
289
 
 
290
size_t
 
291
ecs_mem_size_max(void);
 
292
 
 
293
/*----------------------------------------------------------------------------*/
 
294
 
 
295
#ifdef __cplusplus
 
296
}
 
297
#endif /* __cplusplus */
 
298
 
 
299
#endif /* __ECS_MEM_H__ */