4 * @brief Interface of allocator_t.
8 * Copyright (C) 2005 Jan Hutter, Martin Willi
9 * Hochschule fuer Technik Rapperswil
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>.
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
33 * Macro to allocate a special type.
35 * @param thing object on which a sizeof is performed
37 * - Pointer to allocated memory
38 * - NULL if out of ressources.
42 #define allocator_alloc_thing_as_chunk(thing) (allocator_alloc_as_chunk(sizeof(thing)))
45 * Macro to allocate a special type as chunk_t.
47 * @param thing object on which a sizeof is performed
49 * - chunk_t pointing to allocated memory if successful
50 * - chunk_t containing empty pointer if out of ressources
54 #define allocator_alloc_thing(thing) (allocator_alloc(sizeof(thing)))
58 typedef struct allocator_t allocator_t
;
61 *@brief Allocater object use to detect memory leaks.
68 * Allocates memory with LEAK_DETECTION and
69 * returns an empty data area filled with zeros.
71 * @warning Use this function not directly, only with assigned macros
72 * #allocator_alloc and #allocator_alloc_thing.
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
79 * - pointer to allocated memory area
80 * - NULL if out of ressources
82 void * (*allocate
) (allocator_t
*this,size_t bytes
, char * file
,int line
);
85 * Allocates memory with LEAK_DETECTION and
86 * returns an chunk pointing to an empy data area filled with zeros.
88 * @warning Use this function not directly, only with assigned
89 * macros #allocator_alloc_as_chunk and
90 * #allocator_alloc_thing_as_chunk.
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
97 * - pointer to allocated memory area
98 * - NULL if out of ressources
100 chunk_t (*allocate_as_chunk
) (allocator_t
*this,size_t bytes
, char * file
,int line
);
103 * Reallocates memory with LEAK_DETECTION and
104 * returns an empty data area filled with zeros.
106 * @warning Use this function not directly, only with assigned macro
107 * #allocator_realloc.
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
115 * - pointer to reallocated memory area
116 * - NULL if out of ressources
118 void * (*reallocate
) (allocator_t
*this,void * old
, size_t bytes
, char * file
, int line
);
121 * Clones memory with LEAK_DETECTION and returns a cloned data area.
123 * @warning Use this function not directly, only with assigned macro
124 * #allocator_clone_bytes.
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
132 * - pointer to reallocated memory area if successful
133 * - NULL if out of ressources
135 void * (*clone_bytes
) (allocator_t
*this,void * to_clone
, size_t bytes
, char * file
, int line
);
138 * Frees memory with LEAK_DETECTION.
140 * @warning Use this function not directly, only with assigned macro
143 * @param this allocator_t object
144 * @param pointer pointer to the data area to free
146 void (*free_pointer
) (allocator_t
*this,void * pointer
);
149 * Report memory leaks to stderr.
151 * @warning Use this function not directly, only with assigned macro
152 * #report_memory_leaks
154 * @param this allocator_t object
156 void (*report_memory_leaks
) (allocator_t
*this);
161 * @brief Global allocater_t object.
163 * Only accessed over macros.
165 extern allocator_t
*global_allocator
;
169 * Macro to allocate some memory.
171 * See #allocator_t.allocate for description.
175 #define allocator_alloc(bytes) (global_allocator->allocate(global_allocator,bytes,__FILE__,__LINE__))
178 * Macro to allocate some memory for a chunk_t.
180 * See #allocator_t.allocate_as_chunk for description.
184 #define allocator_alloc_as_chunk(bytes) (global_allocator->allocate_as_chunk(global_allocator,bytes,__FILE__,__LINE__))
187 * Macro to reallocate some memory.
189 * See #allocator_t.reallocate for description.
193 #define allocator_realloc(old,bytes) (global_allocator->reallocate(global_allocator,old,bytes,__FILE__, __LINE__))
196 * Macro to clone some memory.
198 * See #allocator_t.*clone_bytes for description.
202 #define allocator_clone_bytes(old,bytes) (global_allocator->clone_bytes(global_allocator,old,bytes,__FILE__, __LINE__))
205 * Macro to free some memory.
207 * See #allocator_t.free_pointer for description.
211 #define allocator_free(pointer) (global_allocator->free_pointer(global_allocator,pointer))
213 * Macro to free a chunk.
215 #define allocator_free_chunk(chunk){ \
216 global_allocator->free_pointer(global_allocator,(chunk)->ptr); \
217 (chunk)->ptr = NULL; \
221 * Macro to report memory leaks.
223 * See #allocator_s.report_memory_leaks for description.
227 #define report_memory_leaks(void) (global_allocator->report_memory_leaks(global_allocator))
230 * Macro to allocate some memory.
234 #define allocator_alloc(bytes) (malloc(bytes))
237 * Allocate some memory as chunk.
241 chunk_t
allocator_alloc_as_chunk(size_t bytes
);
244 * Reallocate some memory.
248 void * allocator_realloc(void * old
, size_t newsize
);
251 * Free allocated memory.
255 #define allocator_free(pointer) (free(pointer))
261 * @param pointer pointer to read data from
262 * @param size number of bytes to clone
266 void * allocator_clone_bytes(void * pointer
, size_t size
);
269 * Frees memory used by chunk.
271 * @param chunk pointer of chunk to free
275 void allocator_free_chunk(chunk_t
*chunk
);
278 * Report memory leaks.
282 #define report_memory_leaks(void) {}
285 #endif /*ALLOCATOR_H_*/