Added SILC Server library.
[silc.git] / lib / silcutil / silcmemory.c
1 /*
2
3   silcmemory.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1999 - 2005 Pekka Riikonen
8
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.
12
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.
17
18 */
19 /* $Id$ */
20
21 #include "silc.h"
22
23 #ifndef SILC_STACKTRACE
24
25 #define SILC_MAX_ALLOC (1024 * 1024L * 1024L)
26
27 void *silc_malloc(size_t size)
28 {
29   void *addr;
30   if (size <= 0 || size >= SILC_MAX_ALLOC) {
31     SILC_LOG_ERROR(("Invalid memory allocation"));
32     return NULL;
33   }
34   addr = malloc(size);
35   if (!addr)
36     SILC_LOG_ERROR(("System out of memory"));
37   return addr;
38 }
39
40 void *silc_calloc(size_t items, size_t size)
41 {
42   void *addr;
43   if (size * items <= 0 || size * items >= SILC_MAX_ALLOC) {
44     SILC_LOG_ERROR(("Invalid memory allocation"));
45     return NULL;
46   }
47   addr = calloc(items, size);
48   if (!addr)
49     SILC_LOG_ERROR(("System out of memory"));
50   return addr;
51 }
52
53 void *silc_realloc(void *ptr, size_t size)
54 {
55   void *addr;
56   if (size <= 0 || size >= SILC_MAX_ALLOC) {
57     SILC_LOG_ERROR(("Invalid memory allocation"));
58     return NULL;
59   }
60   addr = realloc(ptr, size);
61   if (!addr)
62     SILC_LOG_ERROR(("System out of memory"));
63   return addr;
64 }
65
66 void silc_free(void *ptr)
67 {
68   free(ptr);
69 }
70
71 void *silc_memdup(const void *ptr, size_t size)
72 {
73   unsigned char *addr;
74   addr = silc_malloc(size + 1);
75   if (!addr) {
76     SILC_LOG_ERROR(("System out of memory"));
77     return NULL;
78   }
79   memcpy((void *)addr, ptr, size);
80   addr[size] = '\0';
81   return (void *)addr;
82 }
83
84 #endif /* !SILC_STACKTRACE */
85
86 /* SilcStack aware routines */
87
88 void *silc_smalloc(SilcStack stack, SilcUInt32 size)
89 {
90   return stack ? silc_stack_malloc(stack, size, TRUE) : silc_malloc(size);
91 }
92
93 void *silc_smalloc_ua(SilcStack stack, SilcUInt32 size)
94 {
95   return stack ? silc_stack_malloc(stack, size, FALSE) : silc_malloc(size);
96 }
97
98 void *silc_scalloc(SilcStack stack, SilcUInt32 items, SilcUInt32 size)
99 {
100   unsigned char *addr;
101
102   if (!stack)
103     return silc_calloc(items, size);
104
105   addr = silc_stack_malloc(stack, items * size, TRUE);
106   if (!addr)
107     return NULL;
108   memset(addr, 0, items * size);
109   return (void *)addr;
110 }
111
112 void *silc_srealloc(SilcStack stack, SilcUInt32 old_size,
113                     void *ptr, SilcUInt32 size)
114 {
115   return stack ? silc_stack_realloc(stack, old_size, ptr, size, TRUE) :
116     silc_realloc(ptr, size);
117 }
118
119 void *silc_srealloc_ua(SilcStack stack, SilcUInt32 old_size,
120                        void *ptr, SilcUInt32 size)
121 {
122   return stack ? silc_stack_realloc(stack, old_size, ptr, size, FALSE) :
123     silc_realloc(ptr, size);
124 }
125
126 void *silc_smemdup(SilcStack stack, const void *ptr, SilcUInt32 size)
127 {
128   unsigned char *addr;
129
130   if (!stack)
131     return silc_memdup(ptr, size);
132
133   addr = silc_stack_malloc(stack, size + 1, TRUE);
134   if (!addr)
135     return NULL;
136   memcpy((void *)addr, ptr, size);
137   addr[size] = '\0';
138   return (void *)addr;
139 }
140
141 char *silc_sstrdup(SilcStack stack, const char *str)
142 {
143   SilcInt32 size = strlen(str);
144   char *addr;
145
146   if (!stack)
147     return silc_memdup(str, size);
148
149   addr = silc_stack_malloc(stack, size + 1, FALSE);
150   if (!addr)
151     return NULL;
152   memcpy((void *)addr, str, size);
153   addr[size] = '\0';
154   return addr;
155 }