Added silc_sfree. Removed unaligned memory allocation routines.
[crypto.git] / lib / silcutil / silcmemory.c
1 /*
2
3   silcmemory.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1999 - 2007 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 (silc_unlikely(size <= 0 || size >= SILC_MAX_ALLOC)) {
31     SILC_LOG_ERROR(("Invalid memory allocation"));
32     return NULL;
33   }
34   addr = malloc(size);
35   if (silc_unlikely(!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 (silc_unlikely(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 (silc_unlikely(!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 (silc_unlikely(size <= 0 || size >= SILC_MAX_ALLOC)) {
57     SILC_LOG_ERROR(("Invalid memory allocation"));
58     return NULL;
59   }
60   addr = realloc(ptr, size);
61   if (silc_unlikely(!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 (silc_unlikely(!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) : silc_malloc(size);
91 }
92
93 void silc_sfree(SilcStack stack, void *ptr)
94 {
95   if (stack)
96     return;
97   silc_free(ptr);
98 }
99
100 void *silc_scalloc(SilcStack stack, SilcUInt32 items, SilcUInt32 size)
101 {
102   unsigned char *addr;
103
104   if (!stack)
105     return silc_calloc(items, size);
106
107   addr = silc_stack_malloc(stack, items * size);
108   if (silc_unlikely(!addr))
109     return NULL;
110   memset(addr, 0, items * size);
111   return (void *)addr;
112 }
113
114 void *silc_srealloc(SilcStack stack, SilcUInt32 old_size,
115                     void *ptr, SilcUInt32 size)
116 {
117   return stack ? silc_stack_realloc(stack, old_size, ptr, size) :
118     silc_realloc(ptr, size);
119 }
120
121 void *silc_smemdup(SilcStack stack, const void *ptr, SilcUInt32 size)
122 {
123   unsigned char *addr;
124
125   if (!stack)
126     return silc_memdup(ptr, size);
127
128   addr = silc_stack_malloc(stack, size + 1);
129   if (silc_unlikely(!addr))
130     return NULL;
131   memcpy((void *)addr, ptr, size);
132   addr[size] = '\0';
133   return (void *)addr;
134 }
135
136 char *silc_sstrdup(SilcStack stack, const char *str)
137 {
138   SilcInt32 size = strlen(str);
139   char *addr;
140
141   if (!stack)
142     return silc_memdup(str, size);
143
144   addr = silc_stack_malloc(stack, size + 1);
145   if (silc_unlikely(!addr))
146     return NULL;
147   memcpy((void *)addr, str, size);
148   addr[size] = '\0';
149   return addr;
150 }