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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file util/common_allocation.c
21 * @brief wrapper around malloc/free
22 * @author Christian Grothoff
25 #include "gnunet_crypto_lib.h"
29 #if HAVE_MALLOC_MALLOC_H
30 #include <malloc/malloc.h>
33 #define LOG(kind,...) GNUNET_log_from (kind, "util-common-allocation",__VA_ARGS__)
35 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-common-allocation", syscall)
38 #define INT_MAX 0x7FFFFFFF
42 #define W32_MEM_LIMIT 200000000
46 static LONG mem_used = 0;
50 * Allocate memory. Checks the return value, aborts if no more
51 * memory is available.
53 * @param size how many bytes of memory to allocate, do NOT use
54 * this function (or GNUNET_malloc()) to allocate more than several MB
55 * of memory, if you are possibly needing a very large chunk use
56 * #GNUNET_xmalloc_unchecked_() instead.
57 * @param filename where in the code was the call to GNUNET_malloc()
58 * @param linenumber where in the code was the call to GNUNET_malloc()
59 * @return pointer to size bytes of memory
62 GNUNET_xmalloc_ (size_t size,
68 /* As a security precaution, we generally do not allow very large
69 * allocations using the default 'GNUNET_malloc()' macro */
70 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED,
73 ret = GNUNET_xmalloc_unchecked_ (size,
78 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
87 * Allocate memory for a two dimensional array in one block
88 * and set up pointers. Aborts if no more memory is available.
89 * Don't use GNUNET_xnew_array_2d_ directly. Use the
90 * #GNUNET_new_array_2d macro.
91 * The memory of the elements will be zero'ed out.
93 * @param n size of the first dimension
94 * @param m size of the second dimension
95 * @param elementSize size of a single element in bytes
96 * @param filename where is this call being made (for debugging)
97 * @param linenumber line where this call is being made (for debugging)
98 * @return allocated memory, never NULL
101 GNUNET_xnew_array_2d_ (size_t n,
104 const char *filename,
107 /* use char pointer internally to avoid void pointer arithmetic warnings */
108 char **ret = GNUNET_xmalloc_ (n * sizeof (void *) + /* 1. dim header */
109 n * m * elementSize, /* element data */
110 filename, linenumber);
112 for (size_t i = 0; i < n; i++)
113 ret[i] = (char *)ret + /* base address */
114 n * sizeof (void *) + /* skip 1. dim header */
115 i * m * elementSize; /* skip to 2. dim row header */
121 * Allocate memory for a three dimensional array in one block
122 * and set up pointers. Aborts if no more memory is available.
123 * Don't use GNUNET_xnew_array_3d_ directly. Use the
124 * #GNUNET_new_array_3d macro.
125 * The memory of the elements will be zero'ed out.
127 * @param n size of the first dimension
128 * @param m size of the second dimension
129 * @param o size of the third dimension
130 * @param elementSize size of a single element in bytes
131 * @param filename where is this call being made (for debugging)
132 * @param linenumber line where this call is being made (for debugging)
133 * @return allocated memory, never NULL
136 GNUNET_xnew_array_3d_ (size_t n, size_t m, size_t o, size_t elementSize,
137 const char *filename, int linenumber)
139 /* use char pointer internally to avoid void pointer arithmetic warnings */
140 char ***ret = GNUNET_xmalloc_ (n * sizeof (void **) + /* 1. dim header */
141 n * m * sizeof (void *) + /* 2. dim header */
142 n * m * o * elementSize, /* element data */
143 filename, linenumber);
145 for (size_t i = 0; i < n; i++)
147 /* need to cast to (char *) temporarily for byte level acuracy */
148 ret[i] = (char **)((char *)ret + /* base address */
149 n * sizeof (void **) + /* skip 1. dim header */
150 i * m * sizeof (void *)); /* skip to 2. dim header */
151 for (size_t j = 0; j < m; j++)
152 ret[i][j] = (char *)ret + /* base address */
153 n * sizeof (void **) + /* skip 1. dim header */
154 n * m * sizeof (void *) + /* skip 2. dim header */
155 i * m * o * elementSize + /* skip to 2. dim part */
156 j * o * elementSize; /* skip to 3. dim row data */
158 return (void ***)ret;
163 * Allocate and initialize memory. Checks the return value, aborts if no more
164 * memory is available. Don't use #GNUNET_xmemdup_() directly. Use the
165 * GNUNET_memdup() macro.
167 * @param buf buffer to initialize from (must contain size bytes)
168 * @param size number of bytes to allocate
169 * @param filename where is this call being made (for debugging)
170 * @param linenumber line where this call is being made (for debugging)
171 * @return allocated memory, never NULL
174 GNUNET_xmemdup_ (const void *buf,
176 const char *filename,
181 /* As a security precaution, we generally do not allow very large
182 * allocations here */
183 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
185 size += sizeof (size_t);
186 if (mem_used + size > W32_MEM_LIMIT)
189 GNUNET_assert_at (size < INT_MAX, filename, linenumber);
193 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc");
197 *((size_t *) ret) = size;
198 ret = &((size_t *) ret)[1];
201 GNUNET_memcpy (ret, buf, size);
207 * Wrapper around malloc(). Allocates size bytes of memory.
208 * The memory will be zero'ed out.
210 * @param size the number of bytes to allocate
211 * @param filename where in the code was the call to GNUNET_malloc_unchecked()
212 * @param linenumber where in the code was the call to GNUNET_malloc_unchecked()
213 * @return pointer to size bytes of memory, NULL if we do not have enough memory
216 GNUNET_xmalloc_unchecked_ (size_t size,
217 const char *filename,
225 size += sizeof (size_t);
226 if (mem_used + size > W32_MEM_LIMIT)
230 result = malloc (size);
233 memset (result, 0, size);
236 *((size_t *) result) = size;
237 result = &((size_t *) result)[1];
246 * Reallocate memory. Checks the return value, aborts if no more
247 * memory is available.
248 * The content of the intersection of the new and old size will be unchanged.
250 * @param ptr the pointer to reallocate
251 * @param n how many bytes of memory to allocate
252 * @param filename where in the code was the call to GNUNET_realloc()
253 * @param linenumber where in the code was the call to GNUNET_realloc()
254 * @return pointer to size bytes of memory
257 GNUNET_xrealloc_ (void *ptr,
259 const char *filename,
266 n += sizeof (size_t);
267 ptr = &((size_t *) ptr)[-1];
268 mem_used = mem_used - *((size_t *) ptr) + n;
270 ptr = realloc (ptr, n);
271 if ((NULL == ptr) && (n > 0))
273 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
278 ptr = &((size_t *) ptr)[1];
284 # if __BYTE_ORDER == __LITTLE_ENDIAN
285 #define BAADFOOD_STR "\x0D\xF0\xAD\xBA"
287 # if __BYTE_ORDER == __BIG_ENDIAN
288 #define BAADFOOD_STR "\xBA\xAD\xF0\x0D"
292 #define M_SIZE(p) _msize (p)
295 #include <malloc_np.h>
297 #if HAVE_MALLOC_USABLE_SIZE
298 #define M_SIZE(p) malloc_usable_size (p)
299 #elif HAVE_MALLOC_SIZE
300 #define M_SIZE(p) malloc_size (p)
304 * Free memory. Merely a wrapper for the case that we
305 * want to keep track of allocations.
307 * @param ptr the pointer to free
308 * @param filename where in the code was the call to GNUNET_free
309 * @param linenumber where in the code was the call to GNUNET_free
312 GNUNET_xfree_ (void *ptr,
313 const char *filename,
316 GNUNET_assert_at (NULL != ptr,
320 ptr = &((size_t *) ptr)[-1];
321 mem_used -= *((size_t *) ptr);
326 const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
327 uint64_t *base = ptr;
328 size_t s = M_SIZE (ptr);
333 GNUNET_memcpy (&base[s/8], &baadfood, s % 8);
342 * Dup a string (same semantics as strdup).
344 * @param str the string to dup
345 * @param filename where in the code was the call to GNUNET_strdup()
346 * @param linenumber where in the code was the call to GNUNET_strdup()
347 * @return `strdup(@a str)`
350 GNUNET_xstrdup_ (const char *str,
351 const char *filename,
357 GNUNET_assert_at (str != NULL,
360 slen = strlen (str) + 1;
361 res = GNUNET_xmalloc_ (slen,
373 strnlen (const char *s,
378 e = memchr (s, '\0', n);
387 * Dup partially a string (same semantics as strndup).
389 * @param str the string to dup
390 * @param len the length of the string to dup
391 * @param filename where in the code was the call to GNUNET_strndup()
392 * @param linenumber where in the code was the call to GNUNET_strndup()
393 * @return `strndup(@a str,@a len)`
396 GNUNET_xstrndup_ (const char *str,
398 const char *filename,
404 return GNUNET_strdup ("");
405 GNUNET_assert_at (NULL != str,
410 res = GNUNET_xmalloc_ (len + 1,
413 GNUNET_memcpy (res, str, len);
414 /* res[len] = '\0'; 'malloc' zeros out anyway */
420 * Grow an array. Grows old by (*oldCount-newCount)*elementSize bytes
421 * and sets *oldCount to newCount.
423 * @param old address of the pointer to the array
425 * @param elementSize the size of the elements of the array
426 * @param oldCount address of the number of elements in the *old array
427 * @param newCount number of elements in the new array, may be 0
428 * @param filename where in the code was the call to GNUNET_array_grow()
429 * @param linenumber where in the code was the call to GNUNET_array_grow()
432 GNUNET_xgrow_ (void **old,
434 unsigned int *oldCount,
435 unsigned int newCount,
436 const char *filename,
442 GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
443 size = newCount * elementSize;
450 tmp = GNUNET_xmalloc_ (size, filename, linenumber);
453 GNUNET_memcpy (tmp, *old, elementSize * GNUNET_MIN(*oldCount, newCount));
459 GNUNET_xfree_ (*old, filename, linenumber);
462 *oldCount = newCount;
467 * Like asprintf(), just portable.
469 * @param buf set to a buffer of sufficient size (allocated, caller must free)
470 * @param format format string (see printf(), fprintf(), etc.)
471 * @param ... data for format string
472 * @return number of bytes in `*@a buf`, excluding 0-termination
475 GNUNET_asprintf (char **buf,
482 va_start (args, format);
483 ret = VSNPRINTF (NULL, 0, format, args);
485 GNUNET_assert (ret >= 0);
486 *buf = GNUNET_malloc (ret + 1);
487 va_start (args, format);
488 ret = VSPRINTF (*buf, format, args);
495 * Like snprintf(), just aborts if the buffer is of insufficient size.
497 * @param buf pointer to buffer that is written to
498 * @param size number of bytes in buf
499 * @param format format strings
500 * @param ... data for format string
501 * @return number of bytes written to buf or negative value on error
504 GNUNET_snprintf (char *buf,
506 const char *format, ...)
511 va_start (args, format);
512 ret = VSNPRINTF (buf,
517 GNUNET_assert ( (ret >= 0) &&
518 (((size_t) ret) < size) );
524 * Create a copy of the given message.
526 * @param msg message to copy
527 * @return duplicate of the message
529 struct GNUNET_MessageHeader *
530 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg)
532 struct GNUNET_MessageHeader *ret;
535 msize = ntohs (msg->size);
536 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
537 ret = GNUNET_malloc (msize);
545 /* end of common_allocation.c */