generation of uint implemented but not tested
[strongswan.git] / Source / charon / allocator.h
1 /**
2 * @file allocator.c
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 <stddef.h>
28
29
30 /**
31 * Function to allocate a special type
32 *
33 * @param thing object on it a sizeof is performed
34 */
35 #define allocator_alloc_thing(thing) (allocator_alloc(sizeof(thing)))
36
37 #ifdef LEAK_DETECTIVE
38 /**
39 * Allocates memory with LEAK_DETECTION and
40 * returns an empty data area filled with zeros
41 *
42 * @warning use this function not directly, only with assigned macros
43 * allocator_alloc and allocator_alloc_thing
44 *
45 * @param bytes number of bytes to allocate
46 * @param file filename from which the memory is allocated
47 * @param line line number in specific file
48 * @return allocated memory area
49 */
50 void * allocate(size_t bytes, char * file,int line);
51
52 /**
53 * Reallocates memory with LEAK_DETECTION and
54 * returns an empty data area filled with zeros
55 *
56 * @warning use this function not directly, only with assigned macro
57 * allocator_realloc
58 *
59 * @param old pointer to the old data area
60 * @param bytes number of bytes to allocate
61 * @param file filename from which the memory is allocated
62 * @param line line number in specific file
63 * @return reallocated memory area
64 */
65 void * reallocate(void * old, size_t bytes, char * file, int line);
66 /**
67 * Frees memory with LEAK_DETECTION
68 *
69 * @warning use this function not directly, only with assigned macro
70 * allocator_free
71 *
72 * @param pointer pointer to the data area to free
73 */
74 void free_pointer(void * pointer);
75
76 #define allocator_alloc(bytes) (allocate(bytes,__FILE__,__LINE__))
77 #define allocator_realloc(old,bytes) (reallocate(old,bytes,__FILE__, __LINE__))
78 #define allocator_free(pointer) (free_pointer(pointer))
79 #define allocator_free_chunk(chunk){ \
80 free_pointer(chunk.ptr); \
81 chunk.ptr = NULL; \
82 chunk.len = 0; \
83 }
84 /**
85 * Report memory leaks to stderr
86 */
87 void report_memory_leaks(void);
88 #else
89 #define allocator_alloc(bytes) (malloc(bytes))
90 #define allocator_realloc(old,bytes) (realloc(old,bytes))
91 #define allocator_free(pointer) (free(pointer))
92 #define allocator_free_chunk(chunk){ \
93 free(chunk.ptr); \
94 chunk.ptr = NULL; \
95 chunk.len = 0; \
96 }
97 #define report_memory_leaks(void) {}
98 #endif
99
100 #endif /*ALLOCATOR_H_*/