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, allocation by %x", size));
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, allocation by %x", size));
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, allocation by %x", size));
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)
98 *(unsigned char *)ptr = 'F';
99 #endif /* SILC_DEBUG */
105 void *silc_scalloc(SilcStack stack, SilcUInt32 items, SilcUInt32 size)
110 return silc_calloc(items, size);
112 addr = silc_stack_malloc(stack, items * size);
113 if (silc_unlikely(!addr))
115 memset(addr, 0, items * size);
119 void *silc_srealloc(SilcStack stack, SilcUInt32 old_size,
120 void *ptr, SilcUInt32 size)
122 return stack ? silc_stack_realloc(stack, old_size, ptr, size) :
123 silc_realloc(ptr, size);
126 void *silc_smemdup(SilcStack stack, const void *ptr, SilcUInt32 size)
131 return silc_memdup(ptr, size);
133 addr = silc_stack_malloc(stack, size + 1);
134 if (silc_unlikely(!addr))
136 memcpy((void *)addr, ptr, size);
141 char *silc_sstrdup(SilcStack stack, const char *str)
143 SilcInt32 size = strlen(str);
147 return silc_memdup(str, size);
149 addr = silc_stack_malloc(stack, size + 1);
150 if (silc_unlikely(!addr))
152 memcpy((void *)addr, str, size);