2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2003, 2005, 2006 GNUnet e.V.
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 3, or (at your
8 option) any later version.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file util/common_allocation.c
23 * @brief wrapper around malloc/free
24 * @author Christian Grothoff
27 #include "gnunet_crypto_lib.h"
31 #if HAVE_MALLOC_MALLOC_H
32 #include <malloc/malloc.h>
35 #define LOG(kind,...) GNUNET_log_from (kind, "util-common-allocation",__VA_ARGS__)
37 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-common-allocation", syscall)
40 #define INT_MAX 0x7FFFFFFF
44 #define W32_MEM_LIMIT 200000000
48 static LONG mem_used = 0;
52 * Allocate memory. Checks the return value, aborts if no more
53 * memory is available.
55 * @param size how many bytes of memory to allocate, do NOT use
56 * this function (or GNUNET_malloc()) to allocate more than several MB
57 * of memory, if you are possibly needing a very large chunk use
58 * #GNUNET_xmalloc_unchecked_() instead.
59 * @param filename where in the code was the call to GNUNET_malloc()
60 * @param linenumber where in the code was the call to GNUNET_malloc()
61 * @return pointer to size bytes of memory
64 GNUNET_xmalloc_ (size_t size,
70 /* As a security precaution, we generally do not allow very large
71 * allocations using the default 'GNUNET_malloc()' macro */
72 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED,
75 ret = GNUNET_xmalloc_unchecked_ (size,
80 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
89 * Allocate memory for a two dimensional array in one block
90 * and set up pointers. Aborts if no more memory is available.
91 * Don't use GNUNET_xnew_array_2d_ directly. Use the
92 * #GNUNET_new_array_2d macro.
93 * The memory of the elements will be zero'ed out.
95 * @param n size of the first dimension
96 * @param m size of the second dimension
97 * @param elementSize size of a single element in bytes
98 * @param filename where is this call being made (for debugging)
99 * @param linenumber line where this call is being made (for debugging)
100 * @return allocated memory, never NULL
103 GNUNET_xnew_array_2d_ (size_t n, size_t m, size_t elementSize,
104 const char *filename, int linenumber)
106 /* use char pointer internally to avoid void pointer arithmetic warnings */
107 char **ret = GNUNET_xmalloc_ (n * sizeof (void *) + /* 1. dim header */
108 n * m * elementSize, /* element data */
109 filename, linenumber);
111 for (size_t i = 0; i < n; i++)
112 ret[i] = (char *)ret + /* base address */
113 n * sizeof (void *) + /* skip 1. dim header */
114 i * m * elementSize; /* skip to 2. dim row header */
120 * Allocate memory for a three dimensional array in one block
121 * and set up pointers. Aborts if no more memory is available.
122 * Don't use GNUNET_xnew_array_3d_ directly. Use the
123 * #GNUNET_new_array_3d macro.
124 * The memory of the elements will be zero'ed out.
126 * @param n size of the first dimension
127 * @param m size of the second dimension
128 * @param o size of the third dimension
129 * @param elementSize size of a single element in bytes
130 * @param filename where is this call being made (for debugging)
131 * @param linenumber line where this call is being made (for debugging)
132 * @return allocated memory, never NULL
135 GNUNET_xnew_array_3d_ (size_t n, size_t m, size_t o, size_t elementSize,
136 const char *filename, int linenumber)
138 /* use char pointer internally to avoid void pointer arithmetic warnings */
139 char ***ret = GNUNET_xmalloc_ (n * sizeof (void **) + /* 1. dim header */
140 n * m * sizeof (void *) + /* 2. dim header */
141 n * m * o * elementSize, /* element data */
142 filename, linenumber);
144 for (size_t i = 0; i < n; i++)
146 /* need to cast to (char *) temporarily for byte level acuracy */
147 ret[i] = (char **)((char *)ret + /* base address */
148 n * sizeof (void **) + /* skip 1. dim header */
149 i * m * sizeof (void *)); /* skip to 2. dim header */
150 for (size_t j = 0; j < m; j++)
151 ret[i][j] = (char *)ret + /* base address */
152 n * sizeof (void **) + /* skip 1. dim header */
153 n * m * sizeof (void *) + /* skip 2. dim header */
154 i * m * o * elementSize + /* skip to 2. dim part */
155 j * o * elementSize; /* skip to 3. dim row data */
157 return (void ***)ret;
162 * Allocate and initialize memory. Checks the return value, aborts if no more
163 * memory is available. Don't use #GNUNET_xmemdup_() directly. Use the
164 * GNUNET_memdup() macro.
166 * @param buf buffer to initialize from (must contain size bytes)
167 * @param size number of bytes to allocate
168 * @param filename where is this call being made (for debugging)
169 * @param linenumber line where this call is being made (for debugging)
170 * @return allocated memory, never NULL
173 GNUNET_xmemdup_ (const void *buf,
175 const char *filename,
180 /* As a security precaution, we generally do not allow very large
181 * allocations here */
182 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
184 size += sizeof (size_t);
185 if (mem_used + size > W32_MEM_LIMIT)
188 GNUNET_assert_at (size < INT_MAX, filename, linenumber);
192 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc");
196 *((size_t *) ret) = size;
197 ret = &((size_t *) ret)[1];
200 GNUNET_memcpy (ret, buf, size);
206 * Wrapper around malloc(). Allocates size bytes of memory.
207 * The memory will be zero'ed out.
209 * @param size the number of bytes to allocate
210 * @param filename where in the code was the call to GNUNET_malloc_unchecked()
211 * @param linenumber where in the code was the call to GNUNET_malloc_unchecked()
212 * @return pointer to size bytes of memory, NULL if we do not have enough memory
215 GNUNET_xmalloc_unchecked_ (size_t size,
216 const char *filename,
222 size += sizeof (size_t);
223 if (mem_used + size > W32_MEM_LIMIT)
227 result = malloc (size);
230 memset (result, 0, size);
233 *((size_t *) result) = size;
234 result = &((size_t *) result)[1];
243 * Reallocate memory. Checks the return value, aborts if no more
244 * memory is available.
245 * The content of the intersection of the new and old size will be unchanged.
247 * @param ptr the pointer to reallocate
248 * @param n how many bytes of memory to allocate
249 * @param filename where in the code was the call to GNUNET_realloc()
250 * @param linenumber where in the code was the call to GNUNET_realloc()
251 * @return pointer to size bytes of memory
254 GNUNET_xrealloc_ (void *ptr,
256 const char *filename,
260 n += sizeof (size_t);
261 ptr = &((size_t *) ptr)[-1];
262 mem_used = mem_used - *((size_t *) ptr) + n;
264 ptr = realloc (ptr, n);
265 if ((NULL == ptr) && (n > 0))
267 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "realloc");
271 ptr = &((size_t *) ptr)[1];
277 # if __BYTE_ORDER == __LITTLE_ENDIAN
278 #define BAADFOOD_STR "\x0D\xF0\xAD\xBA"
280 # if __BYTE_ORDER == __BIG_ENDIAN
281 #define BAADFOOD_STR "\xBA\xAD\xF0\x0D"
285 #define M_SIZE(p) _msize (p)
288 #include <malloc_np.h>
290 #if HAVE_MALLOC_USABLE_SIZE
291 #define M_SIZE(p) malloc_usable_size (p)
292 #elif HAVE_MALLOC_SIZE
293 #define M_SIZE(p) malloc_size (p)
297 * Free memory. Merely a wrapper for the case that we
298 * want to keep track of allocations.
300 * @param ptr the pointer to free
301 * @param filename where in the code was the call to GNUNET_free
302 * @param linenumber where in the code was the call to GNUNET_free
305 GNUNET_xfree_ (void *ptr,
306 const char *filename,
309 GNUNET_assert_at (NULL != ptr,
313 ptr = &((size_t *) ptr)[-1];
314 mem_used -= *((size_t *) ptr);
319 const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
320 uint64_t *base = ptr;
321 size_t s = M_SIZE (ptr);
326 GNUNET_memcpy (&base[s/8], &baadfood, s % 8);
335 * Dup a string (same semantics as strdup).
337 * @param str the string to dup
338 * @param filename where in the code was the call to GNUNET_strdup()
339 * @param linenumber where in the code was the call to GNUNET_strdup()
340 * @return `strdup(@a str)`
343 GNUNET_xstrdup_ (const char *str,
344 const char *filename,
350 GNUNET_assert_at (str != NULL,
353 slen = strlen (str) + 1;
354 res = GNUNET_xmalloc_ (slen,
366 strnlen (const char *s,
371 e = memchr (s, '\0', n);
380 * Dup partially a string (same semantics as strndup).
382 * @param str the string to dup
383 * @param len the length of the string to dup
384 * @param filename where in the code was the call to GNUNET_strndup()
385 * @param linenumber where in the code was the call to GNUNET_strndup()
386 * @return `strndup(@a str,@a len)`
389 GNUNET_xstrndup_ (const char *str,
391 const char *filename,
397 return GNUNET_strdup ("");
398 GNUNET_assert_at (NULL != str,
403 res = GNUNET_xmalloc_ (len + 1,
406 GNUNET_memcpy (res, str, len);
407 /* res[len] = '\0'; 'malloc' zeros out anyway */
413 * Grow an array. Grows old by (*oldCount-newCount)*elementSize bytes
414 * and sets *oldCount to newCount.
416 * @param old address of the pointer to the array
418 * @param elementSize the size of the elements of the array
419 * @param oldCount address of the number of elements in the *old array
420 * @param newCount number of elements in the new array, may be 0
421 * @param filename where in the code was the call to GNUNET_array_grow()
422 * @param linenumber where in the code was the call to GNUNET_array_grow()
425 GNUNET_xgrow_ (void **old,
427 unsigned int *oldCount,
428 unsigned int newCount,
429 const char *filename,
435 GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
436 size = newCount * elementSize;
443 tmp = GNUNET_xmalloc_ (size, filename, linenumber);
446 GNUNET_memcpy (tmp, *old, elementSize * GNUNET_MIN(*oldCount, newCount));
452 GNUNET_xfree_ (*old, filename, linenumber);
455 *oldCount = newCount;
460 * Like asprintf(), just portable.
462 * @param buf set to a buffer of sufficient size (allocated, caller must free)
463 * @param format format string (see printf(), fprintf(), etc.)
464 * @param ... data for format string
465 * @return number of bytes in `*@a buf`, excluding 0-termination
468 GNUNET_asprintf (char **buf,
475 va_start (args, format);
476 ret = VSNPRINTF (NULL, 0, format, args);
478 *buf = GNUNET_malloc (ret + 1);
479 va_start (args, format);
480 ret = VSPRINTF (*buf, format, args);
487 * Like snprintf(), just aborts if the buffer is of insufficient size.
489 * @param buf pointer to buffer that is written to
490 * @param size number of bytes in buf
491 * @param format format strings
492 * @param ... data for format string
493 * @return number of bytes written to buf or negative value on error
496 GNUNET_snprintf (char *buf,
498 const char *format, ...)
503 va_start (args, format);
504 ret = VSNPRINTF (buf, size, format, args);
506 GNUNET_assert (ret < size);
512 * Create a copy of the given message.
514 * @param msg message to copy
515 * @return duplicate of the message
517 struct GNUNET_MessageHeader *
518 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg)
520 struct GNUNET_MessageHeader *ret;
523 msize = ntohs (msg->size);
524 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
525 ret = GNUNET_malloc (msize);
526 GNUNET_memcpy (ret, msg, msize);
531 /* end of common_allocation.c */