Use backtrace() in stack tracing for prettier output
[silc.git] / lib / silcutil / silcmemory.h
1 /*
2
3   silcmemory.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1999 - 2005 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 /****h* silcutil/SILC Memory Interface
21  *
22  * DESCRIPTION
23  *
24  * Basic utility functions for allocating memory. All SILC routines, and
25  * applications use these functions when they need to allocate, manipulate
26  * and free memory.
27  *
28  ***/
29
30 #ifndef SILCMEMORY_H
31 #define SILCMEMORY_H
32
33 /* Prototypes */
34
35 #ifndef SILC_STACKTRACE
36
37 /****f* silcutil/SilcMemoryAPI/silc_malloc
38  *
39  * SYNOPSIS
40  *
41  *    void *silc_malloc(size_t size);
42  *
43  * DESCRIPTION
44  *
45  *    Allocates memory of `size' bytes and returns pointer to the allocated
46  *    memory area.  Free the memory by calling silc_free.  Returns NULL on
47  *    error.
48  *
49  ***/
50 void *silc_malloc(size_t size);
51
52 /****f* silcutil/SilcMemoryAPI/silc_calloc
53  *
54  * SYNOPSIS
55  *
56  *    void *silc_calloc(size_t items, size_t size);
57  *
58  * DESCRIPTION
59  *
60  *    Allocates memory of for an array of `items' elements of `size' bytes
61  *    and returns pointer to the allocated memory area.  The memory area is
62  *    also zeroed.  Free the memory by calling silc_free.  Returns NULL on
63  *    error.
64  *
65  ***/
66 void *silc_calloc(size_t items, size_t size);
67
68 /****f* silcutil/SilcMemoryAPI/silc_realloc
69  *
70  * SYNOPSIS
71  *
72  *    void *silc_realloc(void *ptr, size_t size);
73  *
74  * DESCRIPTION
75  *
76  *    Change the size of the memory block indicated by `ptr' to the new
77  *    size of `size' bytes.  The contents of `ptr' will not be changed.
78  *    If `ptr' is NULL the call is equivalent to silc_malloc.  If the
79  *    `size' is zero (0) the call is equivalent to silc_free.  Free the
80  *    memory by calling silc_free.
81  *
82  * NOTES
83  *
84  *    The pointer returned to the reallocated memory area might not be
85  *    same as `ptr'.
86  *
87  ***/
88 void *silc_realloc(void *ptr, size_t size);
89
90 /****f* silcutil/SilcMemoryAPI/silc_free
91  *
92  * SYNOPSIS
93  *
94  *    void silc_free(void *ptr);
95  *
96  * DESCRIPTION
97  *
98  *    Frees the memory area indicated by the `ptr'. If `ptr' is NULL no
99  *    operation is performed.
100  *
101  ***/
102 void silc_free(void *ptr);
103
104 /****f* silcutil/SilcMemoryAPI/silc_memdup
105  *
106  * SYNOPSIS
107  *
108  *    void *silc_memdup(const void *ptr, size_t size);
109  *
110  * DESCRIPTION
111  *
112  *    Duplicates the memory area indicated by `ptr' which is of size
113  *    of `size' bytes. Returns pointer to the duplicated memory area.
114  *    This NULL terminates the dupped memory area by allocating `size' + 1
115  *    bytes, so this function can be used to duplicate strings that does
116  *    not have NULL termination.
117  *
118  ***/
119 void *silc_memdup(const void *ptr, size_t size);
120
121 #else
122 #include "stacktrace.h"
123 #endif /* SILC_STACKTRACE */
124
125
126 /* Following functions that use SilcStack as memory source. */
127
128 /****f* silcutil/SilcMemoryAPI/silc_smalloc
129  *
130  * SYNOPSIS
131  *
132  *    void *silc_smalloc(SilcStack stack, SilcUInt32 size);
133  *
134  * DESCRIPTION
135  *
136  *    Allocate memory block of size of `size' from the stack indicated by
137  *    `stack' and return pointer to it.  Returns NULL on error.  This
138  *    function allocates aligned memory so it can be used to allocate
139  *    memory for structures, for example.  If you allocate strings or
140  *    data buffers using silc_smalloc_ua is recommended instead of this
141  *    function.
142  *
143  * NOTES
144  *
145  *    Be careful with this function:  do not free the returned pointer
146  *    explicitly and do not save the returned pointer to a permanent
147  *    location.
148  *
149  *    If `stack' is NULL this function calls silc_malloc.
150  *
151  ***/
152 void *silc_smalloc(SilcStack stack, SilcUInt32 size);
153
154 /****f* silcutil/SilcMemoryAPI/silc_smalloc_ua
155  *
156  * SYNOPSIS
157  *
158  *    void *silc_smalloc_ua(SilcStack stack, SilcUInt32 size);
159  *
160  * DESCRIPTION
161  *
162  *    Allocate unaligned memory block of size of `size' from the stack
163  *    indicated by `stack' and return pointer to it.  Returns NULL on error.
164  *
165  * NOTES
166  *
167  *    This function must not be used to allocate memory for structures.
168  *    Use this function only for strings and data buffers.
169  *
170  *    Be careful with this function:  do not free the returned pointer
171  *    explicitly and do not save the returned pointer to a permanent
172  *    location.
173  *
174  *    If `stack' is NULL this function calls silc_malloc.
175  *
176  ***/
177 void *silc_smalloc_ua(SilcStack stack, SilcUInt32 size);
178
179 /****f* silcutil/SilcMemoryAPI/silc_scalloc
180  *
181  * SYNOPSIS
182  *
183  *    void *silc_scalloc(SilcStack stack, SilcUInt32 items, SilcUInt32 size);
184  *
185  * DESCRIPTION
186  *
187  *    Allocate memory block of size of `size' from the stack indicated by
188  *    `stack', zero the memory area and return pointer to it.  This
189  *    function allocates aligned memory.  Returns NULL on error.
190  *
191  * NOTES
192  *
193  *    Be careful with this function:  do not free the returned pointer
194  *    explicitly and do not save the returned pointer to a permanent
195  *    location.
196  *
197  *    If `stack' is NULL this function calls silc_calloc.
198  *
199  ***/
200 void *silc_scalloc(SilcStack stack, SilcUInt32 items, SilcUInt32 size);
201
202 /****f* silcutil/SilcMemoryAPI/silc_srealloc
203  *
204  * SYNOPSIS
205  *
206  *    void *silc_srealloc(SilcStack stack, SilcUInt32 old_size,
207  *                        void *ptr, SilcUInt32 size);
208  *
209  * DESCRIPTION
210  *
211  *    Change the size of the memory block indicated by `ptr' to the new
212  *    size of `size' bytes.  The contents of `ptr' will not be changed.
213  *    If `ptr' is NULL the call is equivalent to silc_smalloc.  If `size'
214  *    is zero (0) error will occur.  Returns NULL on error and the old
215  *    pointer remain intact.
216  *
217  * NOTES
218  *
219  *    This function reallocates successfully only if the previous allocation
220  *    to `stack' was `ptr'.  If there was another memory allocation between
221  *    allocating `ptr' and this call, this routine will return NULL.  The
222  *    NULL is also returned if the `size' does not fit to current stack
223  *    and allocating new block would require slow copying of the data.  It
224  *    is left to the caller to decide whether to allocate new pointer and
225  *    copy the old data in case this function returns NULL.
226  *
227  *    This function can be used to reallocate only aligned memory allocated
228  *    with silc_smalloc.
229  *
230  *    If `stack' is NULL this function calls silc_realloc.
231  *
232  ***/
233 void *silc_srealloc(SilcStack stack, SilcUInt32 old_size,
234                     void *ptr, SilcUInt32 size);
235
236 /****f* silcutil/SilcMemoryAPI/silc_srealloc_ua
237  *
238  * SYNOPSIS
239  *
240  *    void *silc_srealloc_ua(SilcStack stack, SilcUInt32 old_size,
241  *                           void *ptr, SilcUInt32 size);
242  *
243  * DESCRIPTION
244  *
245  *    Same as silc_srealloc but reallocates unaligned memory.
246  *
247  * NOTES
248  *
249  *    This function can be used to reallocate only unaligned memory
250  *    allocated with silc_smalloc_ua.
251  *
252  *    If `stack' is NULL this function calls silc_realloc.
253  *
254  ***/
255 void *silc_srealloc_ua(SilcStack stack, SilcUInt32 old_size,
256                        void *ptr, SilcUInt32 size);
257
258 /****f* silcutil/SilcMemoryAPI/silc_smemdup
259  *
260  * SYNOPSIS
261  *
262  *    void *silc_smemdup(SilcStack stack, const void *ptr, SilcUInt32 size);
263  *
264  * DESCRIPTION
265  *
266  *    Duplicates the memory area indicated by `ptr' which is the size of
267  *    `size' bytes.  Returns pointer to the duplicated memory area.  This
268  *    NULL terminates the dupped memory area by allocating `size' + 1
269  *    bytes, so this function can be used to duplicate strings that does not
270  *    have NULL termination.  This function allocates aligned memory so
271  *    it can be used to duplicate also structures.  Returns NULL on error.
272  *
273  * NOTES
274  *
275  *    Be careful with this function:  do not free the returned pointer
276  *    explicitly and do not save the returned pointer to a permanent
277  *    location.
278  *
279  *    If `stack' is NULL this function calls silc_memdup.
280  *
281  ***/
282 void *silc_smemdup(SilcStack stack, const void *ptr, SilcUInt32 size);
283
284 /****f* silcutil/SilcMemoryAPI/silc_sstrdup
285  *
286  * SYNOPSIS
287  *
288  *    char *silc_sstrdup(SilcStack stack, const char *str);
289  *
290  * DESCRIPTION
291  *
292  *    Duplicates the string indicated by `str' and returns the duplicated
293  *    string.  This function allocates unaligned memory.  Returns NULL
294  *    on error.
295  *
296  * NOTES
297  *
298  *    Be careful with this function:  do not free the returned pointer
299  *    explicitly and do not save the returned pointer to a permanent
300  *    location.
301  *
302  *    If `stack' is NULL this function calls silc_strdup.
303  *
304  ***/
305 char *silc_sstrdup(SilcStack stack, const char *str);
306
307 #endif /* SILCMEMORY_H */