- implemented sa_config
[strongswan.git] / Source / charon / utils / allocator.h
1 /**
2 * @file allocator.h
3 *
4 * @brief Interface of allocator_t.
5 */
6
7 /*
8 * Copyright (C) 2005 Jan Hutter, Martin Willi
9 * Hochschule fuer Technik Rapperswil
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 */
21
22 #ifndef ALLOCATOR_H_
23 #define ALLOCATOR_H_
24
25 #include <stdlib.h>
26 #include <stddef.h>
27 #include <string.h>
28
29 #include <types.h>
30
31
32 /**
33 * Macro to allocate a special type.
34 *
35 * @param thing object on which a sizeof is performed
36 * @return
37 * - Pointer to allocated memory
38 * - NULL if out of ressources.
39 *
40 * @ingroup utils
41 */
42 #define allocator_alloc_thing_as_chunk(thing) (allocator_alloc_as_chunk(sizeof(thing)))
43
44 /**
45 * Macro to allocate a special type as chunk_t.
46 *
47 * @param thing object on which a sizeof is performed
48 * @return
49 * - chunk_t pointing to allocated memory if successful
50 * - chunk_t containing empty pointer if out of ressources
51 *
52 * @ingroup utils
53 */
54 #define allocator_alloc_thing(thing) (allocator_alloc(sizeof(thing)))
55
56 #ifdef LEAK_DETECTIVE
57
58 typedef struct allocator_t allocator_t;
59
60 /**
61 *@brief Allocater object use to detect memory leaks.
62 *
63 * @ingroup utils
64 */
65 struct allocator_t {
66
67 /**
68 * Allocates memory with LEAK_DETECTION and
69 * returns an empty data area filled with zeros.
70 *
71 * @warning Use this function not directly, only with assigned macros
72 * #allocator_alloc and #allocator_alloc_thing.
73 *
74 * @param this allocator_t object
75 * @param bytes number of bytes to allocate
76 * @param file filename from which the memory is allocated
77 * @param line line number in specific file
78 * @return
79 * - pointer to allocated memory area
80 * - NULL if out of ressources
81 */
82 void * (*allocate) (allocator_t *this,size_t bytes, char * file,int line);
83
84 /**
85 * Allocates memory with LEAK_DETECTION and
86 * returns an chunk pointing to an empy data area filled with zeros.
87 *
88 * @warning Use this function not directly, only with assigned
89 * macros #allocator_alloc_as_chunk and
90 * #allocator_alloc_thing_as_chunk.
91 *
92 * @param this allocator_t object
93 * @param bytes number of bytes to allocate
94 * @param file filename from which the memory is allocated
95 * @param line line number in specific file
96 * @return
97 * - pointer to allocated memory area
98 * - NULL if out of ressources
99 */
100 chunk_t (*allocate_as_chunk) (allocator_t *this,size_t bytes, char * file,int line);
101
102 /**
103 * Reallocates memory with LEAK_DETECTION and
104 * returns an empty data area filled with zeros.
105 *
106 * @warning Use this function not directly, only with assigned macro
107 * #allocator_realloc.
108 *
109 * @param this allocator_t object
110 * @param old pointer to the old data area
111 * @param bytes number of bytes to allocate
112 * @param file filename from which the memory is allocated
113 * @param line line number in specific file
114 * @return
115 * - pointer to reallocated memory area
116 * - NULL if out of ressources
117 */
118 void * (*reallocate) (allocator_t *this,void * old, size_t bytes, char * file, int line);
119
120 /**
121 * Clones memory with LEAK_DETECTION and returns a cloned data area.
122 *
123 * @warning Use this function not directly, only with assigned macro
124 * #allocator_clone_bytes.
125 *
126 * @param this allocator_t object
127 * @param old pointer to the old data area
128 * @param bytes number of bytes to allocate
129 * @param file filename from which the memory is allocated
130 * @param line line number in specific file
131 * @return
132 * - pointer to reallocated memory area if successful
133 * - NULL if out of ressources
134 */
135 void * (*clone_bytes) (allocator_t *this,void * to_clone, size_t bytes, char * file, int line);
136
137 /**
138 * Clones a chunk with LEAK_DETECTION and returns a cloned chunk.
139 *
140 * @warning Use this function not directly, only with assigned macro
141 * #allocator_clone_chunk-
142 *
143 * @param this allocator_t object
144 * @param chunk chunk to clone
145 * @param file filename from which the memory is allocated
146 * @param line line number in specific file
147 * @return
148 * - pointer to reallocated memory area if successful
149 * - NULL if out of ressources
150 */
151 chunk_t (*clone_chunk) (allocator_t *this, chunk_t chunk, char * file, int line);
152
153 /**
154 * Frees memory with LEAK_DETECTION.
155 *
156 * @warning Use this function not directly, only with assigned macro
157 * #allocator_free.
158 *
159 * @param this allocator_t object
160 * @param pointer pointer to the data area to free
161 */
162 void (*free_pointer) (allocator_t *this,void * pointer);
163
164 /**
165 * Report memory leaks to stderr.
166 *
167 * @warning Use this function not directly, only with assigned macro
168 * #report_memory_leaks
169 *
170 * @param this allocator_t object
171 */
172 void (*report_memory_leaks) (allocator_t *this);
173 };
174
175
176 /**
177 * @brief Global allocater_t object.
178 *
179 * Only accessed over macros.
180 */
181 extern allocator_t *global_allocator;
182
183
184 /**
185 * Macro to allocate some memory.
186 *
187 * See #allocator_t.allocate for description.
188 *
189 * @ingroup utils
190 */
191 #define allocator_alloc(bytes) (global_allocator->allocate(global_allocator,bytes,__FILE__,__LINE__))
192
193 /**
194 * Macro to allocate some memory for a chunk_t.
195 *
196 * See #allocator_t.allocate_as_chunk for description.
197 *
198 * @ingroup utils
199 */
200 #define allocator_alloc_as_chunk(bytes) (global_allocator->allocate_as_chunk(global_allocator,bytes,__FILE__,__LINE__))
201
202 /**
203 * Macro to reallocate some memory.
204 *
205 * See #allocator_t.reallocate for description.
206 *
207 * @ingroup utils
208 */
209 #define allocator_realloc(old,bytes) (global_allocator->reallocate(global_allocator,old,bytes,__FILE__, __LINE__))
210
211 /**
212 * Macro to clone some memory.
213 *
214 * See #allocator_t.*clone_bytes for description.
215 *
216 * @ingroup utils
217 */
218 #define allocator_clone_bytes(old,bytes) (global_allocator->clone_bytes(global_allocator,old,bytes,__FILE__, __LINE__))
219
220
221 /**
222 * Macro to clone a chunk and its contents
223 *
224 * See #allocator_t.clone_chunk for description.
225 *
226 * @ingroup utils
227 */
228 #define allocator_clone_chunk(chunk) (global_allocator->clone_chunk(global_allocator,chunk,__FILE__, __LINE__))
229
230 /**
231 * Macro to free some memory.
232 *
233 * See #allocator_t.free_pointer for description.
234 *
235 * @ingroup utils
236 */
237 #define allocator_free(pointer) (global_allocator->free_pointer(global_allocator,pointer))
238 /**
239 * Macro to free a chunk.
240 */
241 #define allocator_free_chunk(chunk){ \
242 global_allocator->free_pointer(global_allocator,(chunk)->ptr); \
243 (chunk)->ptr = NULL; \
244 (chunk)->len = 0; \
245 }
246 /**
247 * Macro to report memory leaks.
248 *
249 * See #allocator_s.report_memory_leaks for description.
250 *
251 * @ingroup utils
252 */
253 #define report_memory_leaks(void) (global_allocator->report_memory_leaks(global_allocator))
254 #else
255 /**
256 * Macro to allocate some memory.
257 *
258 * @ingroup utils
259 */
260 #define allocator_alloc(bytes) (malloc(bytes))
261
262 /**
263 * Allocate some memory as chunk.
264 *
265 * @ingroup utils
266 */
267 chunk_t allocator_alloc_as_chunk(size_t bytes);
268
269 /**
270 * Reallocate some memory.
271 *
272 * @ingroup utils
273 */
274 void * allocator_realloc(void * old, size_t newsize);
275
276 /**
277 * Free allocated memory.
278 *
279 * @ingroup utils
280 */
281 #define allocator_free(pointer) (free(pointer))
282
283 /**
284 * Clone bytes.
285 *
286 *
287 * @param pointer pointer to read data from
288 * @param size number of bytes to clone
289 *
290 * @ingroup utils
291 */
292 void * allocator_clone_bytes(void * pointer, size_t size);
293
294 /**
295 * Clone a chunk and its contents.
296 *
297 *
298 * @param chunk chunk to clone
299 * @return cloned chunk
300 *
301 * @ingroup utils
302 */
303 chunk_t allocator_clone_bytes(chunk_t chunk);
304
305 /**
306 * Frees memory used by chunk.
307 *
308 * @param chunk pointer of chunk to free
309 *
310 * @ingroup utils
311 */
312 void allocator_free_chunk(chunk_t *chunk);
313
314 /**
315 * Report memory leaks.
316 *
317 * @ingroup utils
318 */
319 #define report_memory_leaks(void) {}
320 #endif
321
322 #endif /*ALLOCATOR_H_*/