silc_srealloc now allocates new block if reallocation fails.
[crypto.git] / lib / silcutil / silcmemory.h
1 /*
2
3   silcmemory.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1999 - 2007 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 and sets silc_errno.
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 and sets silc_errno.
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 /****f* silcutil/SilcMemoryAPI/silc_strdup
122  *
123  * SYNOPSIS
124  *
125  *    char *silc_strdup(const char *str);
126  *
127  * DESCRIPTION
128  *
129  *    Duplicates the string indicated by `str' and returns the duplicated
130  *    string.  Returns NULL on error and sets silc_errno.
131  *
132  ***/
133 char *silc_strdup(const char *str);
134
135 #else
136 #ifndef SILC_DIST_TOOLKIT
137 #error "The stack trace is not supported in this distribution"
138 #endif /* SILC_DIST_TOOLKIT */
139
140 #include "stacktrace.h"
141 #endif /* SILC_STACKTRACE */
142
143
144 /* Following functions that use SilcStack as memory source. */
145
146 /****f* silcutil/SilcMemoryAPI/silc_smalloc
147  *
148  * SYNOPSIS
149  *
150  *    void *silc_smalloc(SilcStack stack, SilcUInt32 size);
151  *
152  * DESCRIPTION
153  *
154  *    Allocate memory block of size of `size' from the stack indicated by
155  *    `stack' and return pointer to it.  Returns NULL on error.  This
156  *    function allocates aligned memory so it can be used to allocate
157  *    memory for structures, for example.
158  *
159  * NOTES
160  *
161  *    Be careful with this function:  do not free the returned pointer
162  *    explicitly and do not save the returned pointer to a permanent
163  *    location.
164  *
165  *    If `stack' is NULL this function calls silc_malloc.
166  *
167  ***/
168 void *silc_smalloc(SilcStack stack, SilcUInt32 size);
169
170 /****f* silcutil/SilcMemoryAPI/silc_scalloc
171  *
172  * SYNOPSIS
173  *
174  *    void *silc_scalloc(SilcStack stack, SilcUInt32 items, SilcUInt32 size);
175  *
176  * DESCRIPTION
177  *
178  *    Allocate memory block of size of `size' from the stack indicated by
179  *    `stack', zero the memory area and return pointer to it.  This
180  *    function allocates aligned memory.  Returns NULL on error.
181  *
182  * NOTES
183  *
184  *    Be careful with this function:  do not free the returned pointer
185  *    explicitly and do not save the returned pointer to a permanent
186  *    location.
187  *
188  *    If `stack' is NULL this function calls silc_calloc.
189  *
190  ***/
191 void *silc_scalloc(SilcStack stack, SilcUInt32 items, SilcUInt32 size);
192
193 /****f* silcutil/SilcMemoryAPI/silc_srealloc
194  *
195  * SYNOPSIS
196  *
197  *    void *silc_srealloc(SilcStack stack, SilcUInt32 old_size,
198  *                        void *ptr, SilcUInt32 size);
199  *
200  * DESCRIPTION
201  *
202  *    Change the size of the memory block indicated by `ptr' to the new
203  *    size of `size' bytes.  The contents of `ptr' will not be changed.
204  *    If `ptr' is NULL the call is equivalent to silc_smalloc.  If `size'
205  *    is zero (0) error will occur.  Returns NULL on error and the old
206  *    pointer remain intact.  This may return different pointer from `ptr'
207  *
208  * NOTES
209  *
210  *    If the reallocation from `stack' fails, this function will allocate
211  *    new block of size of `size' bytes from `stack' and copy the data from
212  *    `ptr' to the new memory block.
213  *
214  *    If `stack' is NULL this function calls silc_realloc.
215  *
216  ***/
217 void *silc_srealloc(SilcStack stack, SilcUInt32 old_size,
218                     void *ptr, SilcUInt32 size);
219
220 /****f* silcutil/SilcMemoryAPI/silc_smemdup
221  *
222  * SYNOPSIS
223  *
224  *    void *silc_smemdup(SilcStack stack, const void *ptr, SilcUInt32 size);
225  *
226  * DESCRIPTION
227  *
228  *    Duplicates the memory area indicated by `ptr' which is the size of
229  *    `size' bytes.  Returns pointer to the duplicated memory area.  This
230  *    NULL terminates the dupped memory area by allocating `size' + 1
231  *    bytes, so this function can be used to duplicate strings that does not
232  *    have NULL termination.  This function allocates aligned memory so
233  *    it can be used to duplicate also structures.  Returns NULL on error.
234  *
235  * NOTES
236  *
237  *    Be careful with this function:  do not free the returned pointer
238  *    explicitly and do not save the returned pointer to a permanent
239  *    location.
240  *
241  *    If `stack' is NULL this function calls silc_memdup.
242  *
243  ***/
244 void *silc_smemdup(SilcStack stack, const void *ptr, SilcUInt32 size);
245
246 /****f* silcutil/SilcMemoryAPI/silc_sfree
247  *
248  * SYNOPSIS
249  *
250  *    void silc_scalloc(SilcStack stack, void *ptr);
251  *
252  * DESCRIPTION
253  *
254  *    This function can be used to free the `ptr' if `stack' is NULL.  This
255  *    function does nothing if `stack' is non-NULL.  When `stack' is NULL
256  *    this function calls silc_free.
257  *
258  ***/
259 void silc_sfree(SilcStack stack, void *ptr);
260
261 /****f* silcutil/SilcMemoryAPI/silc_sstrdup
262  *
263  * SYNOPSIS
264  *
265  *    char *silc_sstrdup(SilcStack stack, const char *str);
266  *
267  * DESCRIPTION
268  *
269  *    Duplicates the string indicated by `str' and returns the duplicated
270  *    string.  Returns NULL on error.
271  *
272  * NOTES
273  *
274  *    Be careful with this function:  do not free the returned pointer
275  *    explicitly and do not save the returned pointer to a permanent
276  *    location.
277  *
278  *    If `stack' is NULL this function calls silc_strdup.
279  *
280  ***/
281 char *silc_sstrdup(SilcStack stack, const char *str);
282
283 #endif /* SILCMEMORY_H */