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 #include "silcruntime.h"
22 #ifndef SILC_STACKTRACE
24 #define SILC_MAX_ALLOC (1024 * 1024L * 1024L)
26 void *silc_malloc(size_t size)
30 if (silc_unlikely(size <= 0 || size >= SILC_MAX_ALLOC)) {
32 silc_set_errno_nofail(SILC_ERR_ZERO_ALLOCATION);
34 silc_set_errno_reason_nofail(SILC_ERR_TOO_LARGE_ALLOCATION,
35 "Allocation by %d", size);
40 if (silc_unlikely(!addr))
41 silc_set_errno_nofail(SILC_ERR_OUT_OF_MEMORY);
46 void *silc_calloc(size_t items, size_t size)
50 if (silc_unlikely(size * items <= 0 || size * items >= SILC_MAX_ALLOC)) {
52 silc_set_errno_nofail(SILC_ERR_ZERO_ALLOCATION);
54 silc_set_errno_reason_nofail(SILC_ERR_TOO_LARGE_ALLOCATION,
55 "Allocation by %d", size);
59 addr = calloc(items, size);
60 if (silc_unlikely(!addr))
61 silc_set_errno_nofail(SILC_ERR_OUT_OF_MEMORY);
66 void *silc_realloc(void *ptr, size_t size)
69 if (silc_unlikely(size <= 0 || size >= SILC_MAX_ALLOC)) {
71 silc_set_errno_nofail(SILC_ERR_ZERO_ALLOCATION);
73 silc_set_errno_reason_nofail(SILC_ERR_TOO_LARGE_ALLOCATION,
74 "Allocation by %d", size);
78 addr = realloc(ptr, size);
79 if (silc_unlikely(!addr))
80 silc_set_errno_nofail(SILC_ERR_OUT_OF_MEMORY);
85 void silc_free(void *ptr)
90 void *silc_memdup(const void *ptr, size_t size)
94 addr = silc_malloc(size + 1);
95 if (silc_unlikely(!addr)) {
96 silc_set_errno_nofail(SILC_ERR_OUT_OF_MEMORY);
99 memcpy((void *)addr, ptr, size);
104 char *silc_strdup(const char *str)
106 return silc_memdup(str, strlen(str));
109 #endif /* !SILC_STACKTRACE */
111 /* SilcStack aware routines */
113 void *silc_smalloc(SilcStack stack, SilcUInt32 size)
115 return stack ? silc_stack_malloc(stack, size) : silc_malloc(size);
118 void silc_sfree(SilcStack stack, void *ptr)
123 *(unsigned char *)ptr = 'F';
124 #endif /* SILC_DEBUG */
130 void *silc_scalloc(SilcStack stack, SilcUInt32 items, SilcUInt32 size)
135 return silc_calloc(items, size);
137 addr = silc_stack_malloc(stack, items * size);
138 if (silc_unlikely(!addr))
140 memset(addr, 0, items * size);
144 void *silc_srealloc(SilcStack stack, SilcUInt32 old_size,
145 void *ptr, SilcUInt32 size)
150 return silc_realloc(ptr, size);
152 new_ptr = silc_stack_realloc(stack, old_size, ptr, size);
154 new_ptr = silc_smalloc(stack, size);
157 memcpy(new_ptr, ptr, old_size > size ? size : old_size);
163 void *silc_smemdup(SilcStack stack, const void *ptr, SilcUInt32 size)
168 return silc_memdup(ptr, size);
170 addr = silc_stack_malloc(stack, size + 1);
171 if (silc_unlikely(!addr))
173 memcpy((void *)addr, ptr, size);
178 char *silc_sstrdup(SilcStack stack, const char *str)
180 SilcInt32 size = strlen(str);
184 return silc_memdup(str, size);
186 addr = silc_stack_malloc(stack, size + 1);
187 if (silc_unlikely(!addr))
189 memcpy((void *)addr, str, size);