- replaced long macros with functions
[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 #include <string.h>
30
31 #include "types.h"
32
33
34 /**
35 * Macro to allocate a special type
36 *
37 * @param thing object on which a sizeof is performed
38 * @return
39 * - Pointer to allocated memory if successful
40 * - NULL otherwise
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
51 */
52 #define allocator_alloc_thing(thing) (allocator_alloc(sizeof(thing)))
53
54 #ifdef LEAK_DETECTIVE
55
56 /**
57 * @brief Allocater object use to detect memory leaks.
58 *
59 */
60 typedef struct allocator_s allocator_t;
61
62 struct allocator_s {
63
64 /**
65 * Allocates memory with LEAK_DETECTION and
66 * returns an empty data area filled with zeros.
67 *
68 * @warning Use this function not directly, only with assigned macros
69 * #allocator_alloc and #allocator_alloc_thing.
70 *
71 * @param this allocator_t object
72 * @param bytes number of bytes to allocate
73 * @param file filename from which the memory is allocated
74 * @param line line number in specific file
75 * @return
76 * - pointer to allocated memory area if successful
77 * - NULL otherwise
78 */
79 void * (*allocate) (allocator_t *this,size_t bytes, char * file,int line);
80
81 /**
82 * Allocates memory with LEAK_DETECTION and
83 * returns an chunk pointing to an empy data area filled with zeros.
84 *
85 * @warning Use this function not directly, only with assigned macros
86 * #allocator_alloc_as_chunk and #allocator_alloc_thing_as_chunk.
87 *
88 * @param this allocator_t object
89 * @param bytes number of bytes to allocate
90 * @param file filename from which the memory is allocated
91 * @param line line number in specific file
92 * @return
93 * - pointer to allocated memory area if successful
94 * - NULL otherwise
95 */
96 chunk_t (*allocate_as_chunk) (allocator_t *this,size_t bytes, char * file,int line);
97
98 /**
99 * Reallocates memory with LEAK_DETECTION and
100 * returns an empty data area filled with zeros
101 *
102 * @warning Use this function not directly, only with assigned macro
103 * #allocator_realloc
104 *
105 * @param this allocator_t object
106 * @param old pointer to the old data area
107 * @param bytes number of bytes to allocate
108 * @param file filename from which the memory is allocated
109 * @param line line number in specific file
110 * @return - pointer to reallocated memory area if successful
111 * - NULL otherwise
112 */
113 void * (*reallocate) (allocator_t *this,void * old, size_t bytes, char * file, int line);
114
115 /**
116 * Clones memory with LEAK_DETECTION and
117 * returns a cloned data area.
118 *
119 * @warning Use this function not directly, only with assigned macro
120 * #allocator_clone_bytes
121 *
122 * @param this allocator_t object
123 * @param old pointer to the old data area
124 * @param bytes number of bytes to allocate
125 * @param file filename from which the memory is allocated
126 * @param line line number in specific file
127 * @return - pointer to reallocated memory area if successful
128 * - NULL otherwise
129 */
130 void * (*clone_bytes) (allocator_t *this,void * to_clone, size_t bytes, char * file, int line);
131
132 /**
133 * Frees memory with LEAK_DETECTION
134 *
135 * @warning Use this function not directly, only with assigned macro
136 * #allocator_free
137 *
138 * @param this allocator_t object
139 * @param pointer pointer to the data area to free
140 */
141 void (*free_pointer) (allocator_t *this,void * pointer);
142
143 /**
144 * Report memory leaks to stderr
145 *
146 * @warning Use this function not directly, only with assigned macro
147 * #report_memory_leaks
148 *
149 * @param this allocator_t object
150 */
151 void (*report_memory_leaks) (allocator_t *this);
152 };
153
154 #ifndef ALLOCATOR_C_
155
156 /**
157 * @brief Global allocater_t object.
158 *
159 * Only accessed over macros.
160 */
161 extern allocator_t *global_allocator;
162 #endif
163
164 /**
165 * Macro to allocate some memory
166 *
167 * @see #allocator_s.allocate for description
168 */
169 #define allocator_alloc(bytes) (global_allocator->allocate(global_allocator,bytes,__FILE__,__LINE__))
170
171 /**
172 * Macro to allocate some memory for a chunk_t
173 *
174 * @see #allocator_s.allocate_as_chunk for description
175 */
176 #define allocator_alloc_as_chunk(bytes) (global_allocator->allocate_as_chunk(global_allocator,bytes,__FILE__,__LINE__))
177
178 /**
179 * Macro to reallocate some memory
180 *
181 * @see #allocator_s.reallocate for description
182 */
183 #define allocator_realloc(old,bytes) (global_allocator->reallocate(global_allocator,old,bytes,__FILE__, __LINE__))
184
185 /**
186 * Macro to clone some memory
187 *
188 * @see #allocator_s.*clone_bytes for description
189 */
190 #define allocator_clone_bytes(old,bytes) (global_allocator->clone_bytes(global_allocator,old,bytes,__FILE__, __LINE__))
191
192 /**
193 * Macro to free some memory
194 *
195 * @see #allocator_s.free for description
196 */
197 #define allocator_free(pointer) (global_allocator->free_pointer(global_allocator,pointer))
198 /**
199 * Macro to free a chunk
200 */
201 #define allocator_free_chunk(chunk){ \
202 global_allocator->free_pointer(global_allocator,chunk.ptr); \
203 chunk.ptr = NULL; \
204 chunk.len = 0; \
205 }
206 /**
207 * Macro to report memory leaks
208 *
209 * @see #allocator_s.report_memory_leaks for description
210 */
211 #define report_memory_leaks(void) (global_allocator->report_memory_leaks(global_allocator))
212 #else
213 #define allocator_alloc(bytes) (malloc(bytes))
214
215 chunk_t allocator_alloc_as_chunk(size_t bytes);
216
217 #define allocator_realloc(old,bytes) (realloc(old,bytes))
218 #define allocator_free(pointer) (free(pointer))
219 void * allocator_clone_bytes(void * pointer, size_t size);
220 void allocator_free_chunk(chunk_t chunk);
221 #define report_memory_leaks(void) {}
222 #endif
223
224 #endif /*ALLOCATOR_H_*/