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.
23 #ifndef SILC_STACKTRACE
25 #define SILC_MAX_ALLOC (1024 * 1024L * 1024L)
27 void *silc_malloc(size_t size)
31 if (silc_unlikely(size <= 0 || size >= SILC_MAX_ALLOC)) {
33 silc_set_errno_nofail(SILC_ERR_ZERO_ALLOCATION);
35 silc_set_errno_reason_nofail(SILC_ERR_TOO_LARGE_ALLOCATION,
36 "Allocation by %d", size);
41 if (silc_unlikely(!addr))
42 silc_set_errno_nofail(SILC_ERR_OUT_OF_MEMORY);
47 void *silc_calloc(size_t items, size_t size)
51 if (silc_unlikely(size * items <= 0 || size * items >= SILC_MAX_ALLOC)) {
53 silc_set_errno_nofail(SILC_ERR_ZERO_ALLOCATION);
55 silc_set_errno_reason_nofail(SILC_ERR_TOO_LARGE_ALLOCATION,
56 "Allocation by %d", size);
60 addr = calloc(items, size);
61 if (silc_unlikely(!addr))
62 silc_set_errno_nofail(SILC_ERR_OUT_OF_MEMORY);
67 void *silc_realloc(void *ptr, size_t size)
70 if (silc_unlikely(size <= 0 || size >= SILC_MAX_ALLOC)) {
72 silc_set_errno_nofail(SILC_ERR_ZERO_ALLOCATION);
74 silc_set_errno_reason_nofail(SILC_ERR_TOO_LARGE_ALLOCATION,
75 "Allocation by %d", size);
79 addr = realloc(ptr, size);
80 if (silc_unlikely(!addr))
81 silc_set_errno_nofail(SILC_ERR_OUT_OF_MEMORY);
86 void silc_free(void *ptr)
91 void *silc_memdup(const void *ptr, size_t size)
95 addr = silc_malloc(size + 1);
96 if (silc_unlikely(!addr)) {
97 silc_set_errno_nofail(SILC_ERR_OUT_OF_MEMORY);
100 memcpy((void *)addr, ptr, size);
105 char *silc_strdup(const char *str)
107 return silc_memdup(str, strlen(str));
110 #endif /* !SILC_STACKTRACE */
112 /* SilcStack aware routines */
114 void *silc_smalloc(SilcStack stack, SilcUInt32 size)
116 return stack ? silc_stack_malloc(stack, size) : silc_malloc(size);
119 void silc_sfree(SilcStack stack, void *ptr)
124 *(unsigned char *)ptr = 'F';
125 #endif /* SILC_DEBUG */
131 void *silc_scalloc(SilcStack stack, SilcUInt32 items, SilcUInt32 size)
136 return silc_calloc(items, size);
138 addr = silc_stack_malloc(stack, items * size);
139 if (silc_unlikely(!addr))
141 memset(addr, 0, items * size);
145 void *silc_srealloc(SilcStack stack, SilcUInt32 old_size,
146 void *ptr, SilcUInt32 size)
151 return silc_realloc(ptr, size);
153 new_ptr = silc_stack_realloc(stack, old_size, ptr, size);
155 new_ptr = silc_smalloc(stack, size);
158 memcpy(new_ptr, ptr, old_size);
164 void *silc_smemdup(SilcStack stack, const void *ptr, SilcUInt32 size)
169 return silc_memdup(ptr, size);
171 addr = silc_stack_malloc(stack, size + 1);
172 if (silc_unlikely(!addr))
174 memcpy((void *)addr, ptr, size);
179 char *silc_sstrdup(SilcStack stack, const char *str)
181 SilcInt32 size = strlen(str);
185 return silc_memdup(str, size);
187 addr = silc_stack_malloc(stack, size + 1);
188 if (silc_unlikely(!addr))
190 memcpy((void *)addr, str, size);