98c89d86a6b52ddf265694f12a27e1864ca43cbb
[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
59   /* As a security precaution, we generally do not allow very large
60    * allocations using the default 'GNUNET_malloc' macro */
61   GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
62   ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber);
63   if (ret == NULL)
64   {
65     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
66     abort ();
67   }
68   return ret;
69 }
70
71
72 /**
73  * Allocate and initialize memory. Checks the return value, aborts if no more
74  * memory is available.  Don't use GNUNET_xmemdup_ directly. Use the
75  * GNUNET_memdup macro.
76  *
77  * @param buf buffer to initialize from (must contain size bytes)
78  * @param size number of bytes to allocate
79  * @param filename where is this call being made (for debugging)
80  * @param linenumber line where this call is being made (for debugging)
81  * @return allocated memory, never NULL
82  */
83 void *
84 GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
85                  int linenumber)
86 {
87   void *ret;
88
89   /* As a security precaution, we generally do not allow very large
90    * allocations here */
91   GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
92 #ifdef W32_MEM_LIMIT
93   size += sizeof (size_t);
94   if (mem_used + size > W32_MEM_LIMIT)
95     return NULL;
96 #endif
97   GNUNET_assert_at (size < INT_MAX, filename, linenumber);
98   ret = malloc (size);
99   if (ret == NULL)
100   {
101     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
102     abort ();
103   }
104 #ifdef W32_MEM_LIMIT
105   *((size_t *) ret) = size;
106   ret = &((size_t *) ret)[1];
107   mem_used += size;
108 #endif
109   memcpy (ret, buf, size);
110   return ret;
111 }
112
113
114
115 /**
116  * Wrapper around malloc. Allocates size bytes of memory.
117  * The memory will be zero'ed out.
118  *
119  * @param size the number of bytes to allocate
120  * @param filename where in the code was the call to GNUNET_malloc_large
121  * @param linenumber where in the code was the call to GNUNET_malloc_large
122  * @return pointer to size bytes of memory, NULL if we do not have enough memory
123  */
124 void *
125 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
126 {
127   void *result;
128
129 #ifdef W32_MEM_LIMIT
130   size += sizeof (size_t);
131   if (mem_used + size > W32_MEM_LIMIT)
132     return NULL;
133 #endif
134
135   GNUNET_assert_at (size < INT_MAX, filename, linenumber);
136   result = malloc (size);
137   if (result == NULL)
138     return NULL;
139   memset (result, 0, size);
140
141 #ifdef W32_MEM_LIMIT
142   *((size_t *) result) = size;
143   result = &((size_t *) result)[1];
144   mem_used += size;
145 #endif
146
147   return result;
148 }
149
150
151 /**
152  * Reallocate memory. Checks the return value, aborts if no more
153  * memory is available.
154  *
155  * @param ptr the pointer to reallocate
156  * @param n how many bytes of memory to allocate
157  * @param filename where in the code was the call to GNUNET_realloc
158  * @param linenumber where in the code was the call to GNUNET_realloc
159  * @return pointer to size bytes of memory
160  */
161 void *
162 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
163 {
164 #ifdef W32_MEM_LIMIT
165   n += sizeof (size_t);
166   ptr = &((size_t *) ptr)[-1];
167   mem_used = mem_used - *((size_t *) ptr) + n;
168 #endif
169   ptr = realloc (ptr, n);
170   if ((NULL == ptr) && (n > 0))
171   {
172     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "realloc");
173     abort ();
174   }
175 #ifdef W32_MEM_LIMIT
176   ptr = &((size_t *) ptr)[1];
177 #endif
178   return ptr;
179 }
180
181
182 /**
183  * Free memory. Merely a wrapper for the case that we
184  * want to keep track of allocations.
185  *
186  * @param ptr the pointer to free
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_xfree_ (void *ptr, const char *filename, int linenumber)
192 {
193   GNUNET_assert_at (ptr != NULL, filename, linenumber);
194 #ifdef W32_MEM_LIMIT
195   ptr = &((size_t *) ptr)[-1];
196   mem_used -= *((size_t *) ptr);
197 #endif
198   free (ptr);
199 }
200
201 /**
202  * Dup a string (same semantics as strdup).
203  *
204  * @param str the string to dup
205  * @param filename where in the code was the call to GNUNET_strdup
206  * @param linenumber where in the code was the call to GNUNET_strdup
207  * @return strdup(str)
208  */
209 char *
210 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
211 {
212   char *res;
213
214   GNUNET_assert_at (str != NULL, filename, linenumber);
215   res = GNUNET_xmalloc_ (strlen (str) + 1, filename, linenumber);
216   memcpy (res, str, strlen (str) + 1);
217   return res;
218 }
219
220
221 /**
222  * Dup partially a string (same semantics as strndup).
223  *
224  * @param str the string to dup
225  * @param len the length of the string to dup
226  * @param filename where in the code was the call to GNUNET_strndup
227  * @param linenumber where in the code was the call to GNUNET_strndup
228  * @return strndup(str,len)
229  */
230 char *
231 GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
232                   int linenumber)
233 {
234   char *res;
235
236   GNUNET_assert_at (str != NULL, filename, linenumber);
237   len = GNUNET_MIN (len, strlen (str));
238   res = GNUNET_xmalloc_ (len + 1, filename, linenumber);
239   memcpy (res, str, len);
240   res[len] = '\0';
241   return res;
242 }
243
244
245 /**
246  * Grow an array.  Grows old by (*oldCount-newCount)*elementSize bytes
247  * and sets *oldCount to newCount.
248  *
249  * @param old address of the pointer to the array
250  *        *old may be NULL
251  * @param elementSize the size of the elements of the array
252  * @param oldCount address of the number of elements in the *old array
253  * @param newCount number of elements in the new array, may be 0
254  * @param filename where in the code was the call to GNUNET_array_grow
255  * @param linenumber where in the code was the call to GNUNET_array_grow
256  */
257 void
258 GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
259                unsigned int newCount, const char *filename, int linenumber)
260 {
261   void *tmp;
262   size_t size;
263
264   GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
265   size = newCount * elementSize;
266   if (size == 0)
267   {
268     tmp = NULL;
269   }
270   else
271   {
272     tmp = GNUNET_xmalloc_ (size, filename, linenumber);
273     memset (tmp, 0, size);      /* client code should not rely on this, though... */
274     if (*oldCount > newCount)
275       *oldCount = newCount;     /* shrink is also allowed! */
276     memcpy (tmp, *old, elementSize * (*oldCount));
277   }
278
279   if (*old != NULL)
280   {
281     GNUNET_xfree_ (*old, filename, linenumber);
282   }
283   *old = tmp;
284   *oldCount = newCount;
285 }
286
287
288 /**
289  * Like asprintf, just portable.
290  *
291  * @param buf set to a buffer of sufficient size (allocated, caller must free)
292  * @param format format string (see printf, fprintf, etc.)
293  * @param ... data for format string
294  * @return number of bytes in "*buf" excluding 0-termination
295  */
296 int
297 GNUNET_asprintf (char **buf, const char *format, ...)
298 {
299   int ret;
300   va_list args;
301
302   va_start (args, format);
303   ret = VSNPRINTF (NULL, 0, format, args);
304   va_end (args);
305   *buf = GNUNET_malloc (ret + 1);
306   va_start (args, format);
307   ret = VSPRINTF (*buf, format, args);
308   va_end (args);
309   return ret;
310 }
311
312
313 /**
314  * Like snprintf, just aborts if the buffer is of insufficient size.
315  *
316  * @param buf pointer to buffer that is written to
317  * @param size number of bytes in buf
318  * @param format format strings
319  * @param ... data for format string
320  * @return number of bytes written to buf or negative value on error
321  */
322 int
323 GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
324 {
325   int ret;
326   va_list args;
327
328   va_start (args, format);
329   ret = VSNPRINTF (buf, size, format, args);
330   va_end (args);
331   GNUNET_assert (ret <= size);
332   return ret;
333 }
334
335
336 /**
337  * Create a copy of the given message.
338  *
339  * @param msg message to copy
340  * @return duplicate of the message
341  */
342 struct GNUNET_MessageHeader *
343 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg)
344 {
345   struct GNUNET_MessageHeader *ret;
346   uint16_t msize;
347
348   msize = ntohs (msg->size);
349   GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
350   ret = GNUNET_malloc (msize);
351   memcpy (ret, msg, msize);
352   return ret;
353 }
354
355
356 /* end of common_allocation.c */