5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1999 - 2007 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)
30 if (silc_unlikely(size <= 0 || size >= SILC_MAX_ALLOC)) {
31 SILC_LOG_ERROR(("Invalid memory allocation"));
35 if (silc_unlikely(!addr))
36 SILC_LOG_ERROR(("System out of memory"));
40 void *silc_calloc(size_t items, size_t size)
43 if (silc_unlikely(size * items <= 0 || size * items >= SILC_MAX_ALLOC)) {
44 SILC_LOG_ERROR(("Invalid memory allocation"));
47 addr = calloc(items, size);
48 if (silc_unlikely(!addr))
49 SILC_LOG_ERROR(("System out of memory"));
53 void *silc_realloc(void *ptr, size_t size)
56 if (silc_unlikely(size <= 0 || size >= SILC_MAX_ALLOC)) {
57 SILC_LOG_ERROR(("Invalid memory allocation"));
60 addr = realloc(ptr, size);
61 if (silc_unlikely(!addr))
62 SILC_LOG_ERROR(("System out of memory"));
66 void silc_free(void *ptr)
71 void *silc_memdup(const void *ptr, size_t size)
74 addr = silc_malloc(size + 1);
75 if (silc_unlikely(!addr)) {
76 SILC_LOG_ERROR(("System out of memory"));
79 memcpy((void *)addr, ptr, size);
84 #endif /* !SILC_STACKTRACE */
86 /* SilcStack aware routines */
88 void *silc_smalloc(SilcStack stack, SilcUInt32 size)
90 return stack ? silc_stack_malloc(stack, size) : silc_malloc(size);
93 void silc_sfree(SilcStack stack, void *ptr)
100 void *silc_scalloc(SilcStack stack, SilcUInt32 items, SilcUInt32 size)
105 return silc_calloc(items, size);
107 addr = silc_stack_malloc(stack, items * size);
108 if (silc_unlikely(!addr))
110 memset(addr, 0, items * size);
114 void *silc_srealloc(SilcStack stack, SilcUInt32 old_size,
115 void *ptr, SilcUInt32 size)
117 return stack ? silc_stack_realloc(stack, old_size, ptr, size) :
118 silc_realloc(ptr, size);
121 void *silc_smemdup(SilcStack stack, const void *ptr, SilcUInt32 size)
126 return silc_memdup(ptr, size);
128 addr = silc_stack_malloc(stack, size + 1);
129 if (silc_unlikely(!addr))
131 memcpy((void *)addr, ptr, size);
136 char *silc_sstrdup(SilcStack stack, const char *str)
138 SilcInt32 size = strlen(str);
142 return silc_memdup(str, size);
144 addr = silc_stack_malloc(stack, size + 1);
145 if (silc_unlikely(!addr))
147 memcpy((void *)addr, str, size);