b011121621b697a1d98116f00a0f4cee93c26179
[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 * Frees memory with LEAK_DETECTION.
139 *
140 * @warning Use this function not directly, only with assigned macro
141 * #allocator_free.
142 *
143 * @param this allocator_t object
144 * @param pointer pointer to the data area to free
145 */
146 void (*free_pointer) (allocator_t *this,void * pointer);
147
148 /**
149 * Report memory leaks to stderr.
150 *
151 * @warning Use this function not directly, only with assigned macro
152 * #report_memory_leaks
153 *
154 * @param this allocator_t object
155 */
156 void (*report_memory_leaks) (allocator_t *this);
157 };
158
159
160 /**
161 * @brief Global allocater_t object.
162 *
163 * Only accessed over macros.
164 */
165 extern allocator_t *global_allocator;
166
167
168 /**
169 * Macro to allocate some memory.
170 *
171 * See #allocator_t.allocate for description.
172 *
173 * @ingroup utils
174 */
175 #define allocator_alloc(bytes) (global_allocator->allocate(global_allocator,bytes,__FILE__,__LINE__))
176
177 /**
178 * Macro to allocate some memory for a chunk_t.
179 *
180 * See #allocator_t.allocate_as_chunk for description.
181 *
182 * @ingroup utils
183 */
184 #define allocator_alloc_as_chunk(bytes) (global_allocator->allocate_as_chunk(global_allocator,bytes,__FILE__,__LINE__))
185
186 /**
187 * Macro to reallocate some memory.
188 *
189 * See #allocator_s.reallocate for description.
190 *
191 * @ingroup utils
192 */
193 #define allocator_realloc(old,bytes) (global_allocator->reallocate(global_allocator,old,bytes,__FILE__, __LINE__))
194
195 /**
196 * Macro to clone some memory.
197 *
198 * See #allocator_s.*clone_bytes for description.
199 *
200 * @ingroup utils
201 */
202 #define allocator_clone_bytes(old,bytes) (global_allocator->clone_bytes(global_allocator,old,bytes,__FILE__, __LINE__))
203
204 /**
205 * Macro to free some memory.
206 *
207 * See #allocator_s.free for description.
208 *
209 * @ingroup utils
210 */
211 #define allocator_free(pointer) (global_allocator->free_pointer(global_allocator,pointer))
212 /**
213 * Macro to free a chunk.
214 */
215 #define allocator_free_chunk(chunk){ \
216 global_allocator->free_pointer(global_allocator,(chunk)->ptr); \
217 (chunk)->ptr = NULL; \
218 (chunk)->len = 0; \
219 }
220 /**
221 * Macro to report memory leaks.
222 *
223 * See #allocator_s.report_memory_leaks for description.
224 *
225 * @ingroup utils
226 */
227 #define report_memory_leaks(void) (global_allocator->report_memory_leaks(global_allocator))
228 #else
229 /**
230 * Macro to allocate some memory.
231 *
232 * @ingroup utils
233 */
234 #define allocator_alloc(bytes) (malloc(bytes))
235
236 /**
237 * Allocate some memory as chunk.
238 *
239 * @ingroup utils
240 */
241 chunk_t allocator_alloc_as_chunk(size_t bytes);
242
243 /**
244 * Reallocate some memory.
245 *
246 * @ingroup utils
247 */
248 void * allocator_realloc(void * old, size_t newsize);
249
250 /**
251 * Free allocated memory.
252 *
253 * @ingroup utils
254 */
255 #define allocator_free(pointer) (free(pointer))
256
257 /**
258 * Clone bytes.
259 *
260 *
261 * @param pointer pointer to read data from
262 * @param size number of bytes to clone
263 *
264 * @ingroup utils
265 */
266 void * allocator_clone_bytes(void * pointer, size_t size);
267
268 /**
269 * Frees memory used by chunk.
270 *
271 * @param chunk pointer of chunk to free
272 *
273 * @ingroup utils
274 */
275 void allocator_free_chunk(chunk_t *chunk);
276
277 /**
278 * Report memory leaks.
279 *
280 * @ingroup utils
281 */
282 #define report_memory_leaks(void) {}
283 #endif
284
285 #endif /*ALLOCATOR_H_*/