fdc2b1f9ef32d2180525403bd9bb26dc1ac9a66b
[strongswan.git] / Source / charon / allocator.h
1 /**
2 * @file allocator.h
3 *
4 * @brief Memory allocation with LEAK_DETECTION support
5 *
6 * Thread-save implementation
7 */
8
9 /*
10 * Copyright (C) 2005 Jan Hutter, Martin Willi
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #ifndef ALLOCATOR_H_
25 #define ALLOCATOR_H_
26
27 #include <stdlib.h>
28 #include <stddef.h>
29
30 #include "types.h"
31
32
33 /**
34 * Macro to allocate a special type
35 *
36 * @param thing object on which a sizeof is performed
37 * @return
38 * - Pointer to allocated memory if successful
39 * - NULL otherwise
40 */
41 #define allocator_alloc_thing_as_chunk(thing) (allocator_alloc_as_chunk(sizeof(thing)))
42
43 /**
44 * Macro to allocate a special type as chunk_t
45 *
46 * @param thing object on which a sizeof is performed
47 * @return
48 * - chunk_t pointing to allocated memory if successful
49 * - chunk_t containing empty pointer
50 */
51 #define allocator_alloc_thing(thing) (allocator_alloc(sizeof(thing)))
52
53 #ifdef LEAK_DETECTIVE
54
55 /**
56 * @brief Allocater object use to detect memory leaks.
57 *
58 */
59 typedef struct allocator_s allocator_t;
60
61 struct allocator_s {
62
63 /**
64 * Allocates memory with LEAK_DETECTION and
65 * returns an empty data area filled with zeros.
66 *
67 * @warning Use this function not directly, only with assigned macros
68 * #allocator_alloc and #allocator_alloc_thing.
69 *
70 * @param this allocator_t object
71 * @param bytes number of bytes to allocate
72 * @param file filename from which the memory is allocated
73 * @param line line number in specific file
74 * @return
75 * - pointer to allocated memory area if successful
76 * - NULL otherwise
77 */
78 void * (*allocate) (allocator_t *this,size_t bytes, char * file,int line);
79
80 /**
81 * Allocates memory with LEAK_DETECTION and
82 * returns an chunk pointing to an empy data area filled with zeros.
83 *
84 * @warning Use this function not directly, only with assigned macros
85 * #allocator_alloc_as_chunk and #allocator_alloc_thing_as_chunk.
86 *
87 * @param this allocator_t object
88 * @param bytes number of bytes to allocate
89 * @param file filename from which the memory is allocated
90 * @param line line number in specific file
91 * @return
92 * - pointer to allocated memory area if successful
93 * - NULL otherwise
94 */
95 chunk_t (*allocate_as_chunk) (allocator_t *this,size_t bytes, char * file,int line);
96
97 /**
98 * Reallocates memory with LEAK_DETECTION and
99 * returns an empty data area filled with zeros
100 *
101 * @warning Use this function not directly, only with assigned macro
102 * #allocator_realloc
103 *
104 * @param this allocator_t object
105 * @param old pointer to the old data area
106 * @param bytes number of bytes to allocate
107 * @param file filename from which the memory is allocated
108 * @param line line number in specific file
109 * @return - pointer to reallocated memory area if successful
110 * - NULL otherwise
111 */
112 void * (*reallocate) (allocator_t *this,void * old, size_t bytes, char * file, int line);
113
114 /**
115 * Clones memory with LEAK_DETECTION and
116 * returns a cloned data area.
117 *
118 * @warning Use this function not directly, only with assigned macro
119 * #allocator_clone_bytes
120 *
121 * @param this allocator_t object
122 * @param old pointer to the old data area
123 * @param bytes number of bytes to allocate
124 * @param file filename from which the memory is allocated
125 * @param line line number in specific file
126 * @return - pointer to reallocated memory area if successful
127 * - NULL otherwise
128 */
129 void * (*clone_bytes) (allocator_t *this,void * to_clone, size_t bytes, char * file, int line);
130
131 /**
132 * Frees memory with LEAK_DETECTION
133 *
134 * @warning Use this function not directly, only with assigned macro
135 * #allocator_free
136 *
137 * @param this allocator_t object
138 * @param pointer pointer to the data area to free
139 */
140 void (*free_pointer) (allocator_t *this,void * pointer);
141
142 /**
143 * Report memory leaks to stderr
144 *
145 * @warning Use this function not directly, only with assigned macro
146 * #report_memory_leaks
147 *
148 * @param this allocator_t object
149 */
150 void (*report_memory_leaks) (allocator_t *this);
151 };
152
153 #ifndef ALLOCATOR_C_
154
155 /**
156 * @brief Global allocater_t object.
157 *
158 * Only accessed over macros.
159 */
160 extern allocator_t *global_allocator;
161 #endif
162
163 /**
164 * Macro to allocate some memory
165 *
166 * @see #allocator_s.allocate for description
167 */
168 #define allocator_alloc(bytes) (global_allocator->allocate(global_allocator,bytes,__FILE__,__LINE__))
169
170 /**
171 * Macro to allocate some memory for a chunk_t
172 *
173 * @see #allocator_s.allocate_as_chunk for description
174 */
175 #define allocator_alloc_as_chunk(bytes) (global_allocator->allocate_as_chunk(global_allocator,bytes,__FILE__,__LINE__))
176
177 /**
178 * Macro to reallocate some memory
179 *
180 * @see #allocator_s.reallocate for description
181 */
182 #define allocator_realloc(old,bytes) (global_allocator->reallocate(global_allocator,old,bytes,__FILE__, __LINE__))
183
184 /**
185 * Macro to clone some memory
186 *
187 * @see #allocator_s.*clone_bytes for description
188 */
189 #define allocator_clone_bytes(old,bytes) (global_allocator->clone_bytes(global_allocator,old,bytes,__FILE__, __LINE__))
190
191 /**
192 * Macro to free some memory
193 *
194 * @see #allocator_s.free for description
195 */
196 #define allocator_free(pointer) (global_allocator->free_pointer(global_allocator,pointer))
197 /**
198 * Macro to free a chunk
199 */
200 #define allocator_free_chunk(chunk){ \
201 global_allocator->free_pointer(global_allocator,chunk.ptr); \
202 chunk.ptr = NULL; \
203 chunk.len = 0; \
204 }
205 /**
206 * Macro to report memory leaks
207 *
208 * @see #allocator_s.report_memory_leaks for description
209 */
210 #define report_memory_leaks(void) (global_allocator->report_memory_leaks(global_allocator))
211 #else
212 #define allocator_alloc(bytes) (malloc(bytes))
213 #define allocator_alloc_as_chunk(bytes){\
214 chunk_t new_chunk; \
215 new_chunk.ptr = malloc(bytes); \
216 new_chunk.len = (new_chunk.ptr == NULL) ? 0 : bytes; \
217 return new_chunk; \
218 }
219 #define allocator_realloc(old,bytes) (realloc(old,bytes))
220 #define allocator_free(pointer) (free(pointer))
221 #define allocator_clone_bytes(pointer,size){\
222 void *new_data = malloc(size)\
223 if (new_data == NULL) return NULL; \
224 memcpy(new_data,pointer,size)\
225 return new_data; \
226 }
227 #define allocator_free_chunk(chunk){ \
228 free(chunk.ptr); \
229 chunk.ptr = NULL; \
230 chunk.len = 0; \
231 }
232 #define report_memory_leaks(void) {}
233 #endif
234
235 #endif /*ALLOCATOR_H_*/