5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1999 - 2008 Pekka Riikonen
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.
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.
20 /****h* silcutil/Memory Interface
24 * Routines for allocating and freeing memory.
35 /****f* silcutil/silc_malloc
39 * void *silc_malloc(size_t size);
43 * Allocates memory of `size' bytes and returns pointer to the allocated
44 * memory area. Free the memory by calling silc_free. Returns NULL on
45 * error and sets silc_errno.
48 void *silc_malloc(size_t size);
50 /****f* silcutil/silc_calloc
54 * void *silc_calloc(size_t items, size_t size);
58 * Allocates memory of for an array of `items' elements of `size' bytes
59 * and returns pointer to the allocated memory area. The memory area is
60 * also zeroed. Free the memory by calling silc_free. Returns NULL on
61 * error and sets silc_errno.
64 void *silc_calloc(size_t items, size_t size);
66 /****f* silcutil/silc_realloc
70 * void *silc_realloc(void *ptr, size_t size);
74 * Change the size of the memory block indicated by `ptr' to the new
75 * size of `size' bytes. The contents of `ptr' will not be changed.
76 * If `ptr' is NULL the call is equivalent to silc_malloc. If the
77 * `size' is zero (0) the call is equivalent to silc_free. Free the
78 * memory by calling silc_free.
82 * The pointer returned to the reallocated memory area might not be
86 void *silc_realloc(void *ptr, size_t size);
88 /****f* silcutil/silc_free
92 * void silc_free(void *ptr);
96 * Frees the memory area indicated by the `ptr'. If `ptr' is NULL no
97 * operation is performed.
100 void silc_free(void *ptr);
102 /****f* silcutil/silc_memdup
106 * void *silc_memdup(const void *ptr, size_t size);
110 * Duplicates the memory area indicated by `ptr' which is of size
111 * of `size' bytes. Returns pointer to the duplicated memory area.
112 * This NULL terminates the dupped memory area by allocating `size' + 1
113 * bytes, so this function can be used to duplicate strings that does
114 * not have NULL termination.
117 void *silc_memdup(const void *ptr, size_t size);
119 /****f* silcutil/silc_strdup
123 * char *silc_strdup(const char *str);
127 * Duplicates the string indicated by `str' and returns the duplicated
128 * string. Returns NULL on error and sets silc_errno.
131 char *silc_strdup(const char *str);
134 #include "memtrace.h"
135 #endif /* SILC_MEMTRACE */
138 /* Following functions that use SilcStack as memory source. */
140 /****f* silcutil/silc_smalloc
144 * void *silc_smalloc(SilcStack stack, SilcUInt32 size);
148 * Allocate memory block of size of `size' from the stack indicated by
149 * `stack' and return pointer to it. Returns NULL on error. This
150 * function allocates aligned memory so it can be used to allocate
151 * memory for structures, for example.
155 * Be careful with this function: do not free the returned pointer
156 * explicitly and do not save the returned pointer to a permanent
159 * If `stack' is NULL this function calls silc_malloc.
162 void *silc_smalloc(SilcStack stack, SilcUInt32 size);
164 /****f* silcutil/silc_scalloc
168 * void *silc_scalloc(SilcStack stack, SilcUInt32 items, SilcUInt32 size);
172 * Allocate memory block of size of `size' from the stack indicated by
173 * `stack', zero the memory area and return pointer to it. This
174 * function allocates aligned memory. Returns NULL on error.
178 * Be careful with this function: do not free the returned pointer
179 * explicitly and do not save the returned pointer to a permanent
182 * If `stack' is NULL this function calls silc_calloc.
185 void *silc_scalloc(SilcStack stack, SilcUInt32 items, SilcUInt32 size);
187 /****f* silcutil/silc_srealloc
191 * void *silc_srealloc(SilcStack stack, SilcUInt32 old_size,
192 * void *ptr, SilcUInt32 size);
196 * Change the size of the memory block indicated by `ptr' to the new
197 * size of `size' bytes. The contents of `ptr' will not be changed.
198 * If `ptr' is NULL the call is equivalent to silc_smalloc. If `size'
199 * is zero (0) error will occur. Returns NULL on error and the old
200 * pointer remain intact. This may return different pointer from `ptr'
204 * If the reallocation from `stack' fails, this function will allocate
205 * new block of size of `size' bytes from `stack' and copy the data from
206 * `ptr' to the new memory block.
208 * If `stack' is NULL this function calls silc_realloc.
211 void *silc_srealloc(SilcStack stack, SilcUInt32 old_size,
212 void *ptr, SilcUInt32 size);
214 /****f* silcutil/silc_smemdup
218 * void *silc_smemdup(SilcStack stack, const void *ptr, SilcUInt32 size);
222 * Duplicates the memory area indicated by `ptr' which is the size of
223 * `size' bytes. Returns pointer to the duplicated memory area. This
224 * NULL terminates the dupped memory area by allocating `size' + 1
225 * bytes, so this function can be used to duplicate strings that does not
226 * have NULL termination. This function allocates aligned memory so
227 * it can be used to duplicate also structures. Returns NULL on error.
231 * Be careful with this function: do not free the returned pointer
232 * explicitly and do not save the returned pointer to a permanent
235 * If `stack' is NULL this function calls silc_memdup.
238 void *silc_smemdup(SilcStack stack, const void *ptr, SilcUInt32 size);
240 /****f* silcutil/silc_sfree
244 * void silc_scalloc(SilcStack stack, void *ptr);
248 * This function can be used to free the `ptr' if `stack' is NULL. This
249 * function does nothing if `stack' is non-NULL. When `stack' is NULL
250 * this function calls silc_free.
253 void silc_sfree(SilcStack stack, void *ptr);
255 /****f* silcutil/silc_sstrdup
259 * char *silc_sstrdup(SilcStack stack, const char *str);
263 * Duplicates the string indicated by `str' and returns the duplicated
264 * string. Returns NULL on error.
268 * Be careful with this function: do not free the returned pointer
269 * explicitly and do not save the returned pointer to a permanent
272 * If `stack' is NULL this function calls silc_strdup.
275 char *silc_sstrdup(SilcStack stack, const char *str);
277 #endif /* SILCMEMORY_H */