Returns now GNUNET_SYSERR
[oweals/gnunet.git] / src / util / common_allocation.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2005, 2006 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file util/common_allocation.c
23  * @brief wrapper around malloc/free
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29
30 #ifndef INT_MAX
31 #define INT_MAX 0x7FFFFFFF
32 #endif
33
34 #if 0
35 #define W32_MEM_LIMIT 200000000
36 #endif
37
38 #ifdef W32_MEM_LIMIT
39 static LONG mem_used = 0;
40 #endif
41
42 /**
43  * Allocate memory. Checks the return value, aborts if no more
44  * memory is available.
45  *
46  * @param size how many bytes of memory to allocate, do NOT use
47  *  this function (or GNUNET_malloc) to allocate more than several MB
48  *  of memory, if you are possibly needing a very large chunk use
49  *  GNUNET_xmalloc_unchecked_ instead.
50  * @param filename where in the code was the call to GNUNET_malloc
51  * @param linenumber where in the code was the call to GNUNET_malloc
52  * @return pointer to size bytes of memory
53  */
54 void *
55 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
56 {
57   void *ret;
58   /* As a security precaution, we generally do not allow very large
59      allocations using the default 'GNUNET_malloc' macro */
60   GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
61   ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber);
62   if (ret == NULL)
63     {
64       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
65       abort ();
66     }
67   return ret;
68 }
69
70
71 /**
72  * Wrapper around malloc. Allocates size bytes of memory.
73  * The memory will be zero'ed out.
74  *
75  * @param size the number of bytes to allocate
76  * @param filename where in the code was the call to GNUNET_malloc_large
77  * @param linenumber where in the code was the call to GNUNET_malloc_large
78  * @return pointer to size bytes of memory, NULL if we do not have enough memory
79  */
80 void *
81 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
82 {
83   void *result;
84
85 #ifdef W32_MEM_LIMIT
86   size += sizeof (size_t);
87   if (mem_used + size > W32_MEM_LIMIT)
88     return NULL;
89 #endif
90
91   GNUNET_assert_at (size < INT_MAX, filename, linenumber);
92   result = malloc (size);
93   if (result == NULL)
94     return NULL;
95   memset (result, 0, size);
96
97 #ifdef W32_MEM_LIMIT
98   *((size_t *) result) = size;
99   result = &((size_t *) result)[1];
100   mem_used += size;
101 #endif
102
103   return result;
104 }
105
106
107 /**
108  * Reallocate memory. Checks the return value, aborts if no more
109  * memory is available.
110  *
111  * @param ptr the pointer to reallocate
112  * @param n how many bytes of memory to allocate
113  * @param filename where in the code was the call to GNUNET_realloc
114  * @param linenumber where in the code was the call to GNUNET_realloc
115  * @return pointer to size bytes of memory
116  */
117 void *
118 GNUNET_xrealloc_ (void *ptr,
119                   size_t n,
120                   const char *filename, int linenumber)
121 {
122 #ifdef W32_MEM_LIMIT
123   n += sizeof (size_t);
124   ptr = &((size_t *) ptr)[-1];
125   mem_used = mem_used - *((size_t *) ptr) + n;
126 #endif
127   ptr = realloc (ptr, n);
128   if (!ptr)
129     {
130       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "realloc");
131       abort ();
132     }
133 #ifdef W32_MEM_LIMIT
134   ptr = &((size_t *) ptr)[1];
135 #endif
136   return ptr;
137 }
138
139
140 /**
141  * Free memory. Merely a wrapper for the case that we
142  * want to keep track of allocations.
143  *
144  * @param ptr the pointer to free
145  * @param filename where in the code was the call to GNUNET_array_grow
146  * @param linenumber where in the code was the call to GNUNET_array_grow
147  */
148 void
149 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
150 {
151   GNUNET_assert_at (ptr != NULL, filename, linenumber);
152 #ifdef W32_MEM_LIMIT
153   ptr = &((size_t *) ptr)[-1];
154   mem_used -= *((size_t *) ptr);
155 #endif
156   free (ptr);
157 }
158
159 /**
160  * Dup a string (same semantics as strdup).
161  *
162  * @param str the string to dup
163  * @param filename where in the code was the call to GNUNET_strdup
164  * @param linenumber where in the code was the call to GNUNET_strdup
165  * @return strdup(str)
166  */
167 char *
168 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
169 {
170   char *res;
171
172   GNUNET_assert_at (str != NULL, filename, linenumber);
173   res = GNUNET_xmalloc_ (strlen (str) + 1, filename, linenumber);
174   memcpy (res, str, strlen (str) + 1);
175   return res;
176 }
177
178 /**
179  * Grow an array.  Grows old by (*oldCount-newCount)*elementSize bytes
180  * and sets *oldCount to newCount.
181  *
182  * @param old address of the pointer to the array
183  *        *old may be NULL
184  * @param elementSize the size of the elements of the array
185  * @param oldCount address of the number of elements in the *old array
186  * @param newCount number of elements in the new array, may be 0
187  * @param filename where in the code was the call to GNUNET_array_grow
188  * @param linenumber where in the code was the call to GNUNET_array_grow
189  */
190 void
191 GNUNET_xgrow_ (void **old,
192                size_t elementSize,
193                unsigned int *oldCount,
194                unsigned int newCount, const char *filename, int linenumber)
195 {
196   void *tmp;
197   size_t size;
198
199   GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
200   size = newCount * elementSize;
201   if (size == 0)
202     {
203       tmp = NULL;
204     }
205   else
206     {
207       tmp = GNUNET_xmalloc_ (size, filename, linenumber);
208       memset (tmp, 0, size);    /* client code should not rely on this, though... */
209       if (*oldCount > newCount)
210         *oldCount = newCount;   /* shrink is also allowed! */
211       memcpy (tmp, *old, elementSize * (*oldCount));
212     }
213
214   if (*old != NULL)
215     {
216       GNUNET_xfree_ (*old, filename, linenumber);
217     }
218   *old = tmp;
219   *oldCount = newCount;
220 }
221
222
223 /**
224  * Like asprintf, just portable.
225  *
226  * @param buf set to a buffer of sufficient size (allocated, caller must free)
227  * @param format format string (see printf, fprintf, etc.)
228  * @param ... data for format string
229  * @return number of bytes in "*buf" excluding 0-termination
230  */
231 int
232 GNUNET_asprintf (char **buf, const char *format, ...)
233 {
234   int ret;
235   va_list args;
236
237   va_start (args, format);
238   ret = VSNPRINTF (NULL, 0, format, args);
239   va_end (args);
240   *buf = GNUNET_malloc (ret + 1);
241   va_start (args, format);
242   ret = VSPRINTF (*buf, format, args);
243   va_end (args);
244   return ret;
245 }
246
247
248 /**
249  * Like snprintf, just aborts if the buffer is of insufficient size.
250  *
251  * @param buf pointer to buffer that is written to
252  * @param size number of bytes in buf
253  * @param format format strings
254  * @param ... data for format string
255  * @return number of bytes written to buf or negative value on error
256  */
257 int
258 GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
259 {
260   int ret;
261   va_list args;
262
263   va_start (args, format);
264   ret = VSNPRINTF (buf, size, format, args);
265   va_end (args);
266   GNUNET_assert (ret <= size);
267   return ret;
268 }
269
270 /* end of common_allocation.c */