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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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, ...) \
36 GNUNET_log_from (kind, "util-common-allocation", __VA_ARGS__)
38 #define LOG_STRERROR(kind, syscall) \
39 GNUNET_log_from_strerror (kind, "util-common-allocation", syscall)
42 #define INT_MAX 0x7FFFFFFF
46 #define W32_MEM_LIMIT 200000000
50 static LONG mem_used = 0;
54 * Allocate memory. Checks the return value, aborts if no more
55 * memory is available.
57 * @param size how many bytes of memory to allocate, do NOT use
58 * this function (or GNUNET_malloc()) to allocate more than several MB
59 * of memory, if you are possibly needing a very large chunk use
60 * #GNUNET_xmalloc_unchecked_() instead.
61 * @param filename where in the code was the call to GNUNET_malloc()
62 * @param linenumber where in the code was the call to GNUNET_malloc()
63 * @return pointer to size bytes of memory
66 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
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, filename, linenumber);
73 ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber);
76 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc");
84 * Allocate memory for a two dimensional array in one block
85 * and set up pointers. Aborts if no more memory is available.
86 * Don't use GNUNET_xnew_array_2d_ directly. Use the
87 * #GNUNET_new_array_2d macro.
88 * The memory of the elements will be zero'ed out.
90 * @param n size of the first dimension
91 * @param m size of the second dimension
92 * @param elementSize size of a single element in bytes
93 * @param filename where is this call being made (for debugging)
94 * @param linenumber line where this call is being made (for debugging)
95 * @return allocated memory, never NULL
98 GNUNET_xnew_array_2d_ (size_t n,
101 const char *filename,
104 /* use char pointer internally to avoid void pointer arithmetic warnings */
105 char **ret = GNUNET_xmalloc_ (n * sizeof(void *) /* 1. dim header */
106 + n * m * elementSize, /* element data */
110 for (size_t i = 0; i < n; i++)
111 ret[i] = (char *) ret /* base address */
112 + n * sizeof(void *) /* skip 1. dim header */
113 + i * m * elementSize; /* skip to 2. dim row header */
114 return (void **) ret;
119 * Allocate memory for a three dimensional array in one block
120 * and set up pointers. Aborts if no more memory is available.
121 * Don't use GNUNET_xnew_array_3d_ directly. Use the
122 * #GNUNET_new_array_3d macro.
123 * The memory of the elements will be zero'ed out.
125 * @param n size of the first dimension
126 * @param m size of the second dimension
127 * @param o size of the third dimension
128 * @param elementSize size of a single element in bytes
129 * @param filename where is this call being made (for debugging)
130 * @param linenumber line where this call is being made (for debugging)
131 * @return allocated memory, never NULL
134 GNUNET_xnew_array_3d_ (size_t n,
138 const char *filename,
141 /* use char pointer internally to avoid void pointer arithmetic warnings */
142 char ***ret = GNUNET_xmalloc_ (n * sizeof(void **) /* 1. dim header */
143 + n * m * sizeof(void *) /* 2. dim header */
144 + n * m * o * elementSize, /* element data */
148 for (size_t i = 0; i < n; i++)
150 /* need to cast to (char *) temporarily for byte level acuracy */
151 ret[i] = (char **) ((char *) ret /* base address */
152 + n * sizeof(void **) /* skip 1. dim header */
153 + i * m * sizeof(void *)); /* skip to 2. dim header */
154 for (size_t j = 0; j < m; j++)
155 ret[i][j] = (char *) ret /* base address */
156 + n * sizeof(void **) /* skip 1. dim header */
157 + n * m * sizeof(void *) /* skip 2. dim header */
158 + i * m * o * elementSize /* skip to 2. dim part */
159 + j * o * elementSize; /* skip to 3. dim row data */
161 return (void ***) ret;
166 * Allocate and initialize memory. Checks the return value, aborts if no more
167 * memory is available. Don't use #GNUNET_xmemdup_() directly. Use the
168 * GNUNET_memdup() macro.
170 * @param buf buffer to initialize from (must contain size bytes)
171 * @param size number of bytes to allocate
172 * @param filename where is this call being made (for debugging)
173 * @param linenumber line where this call is being made (for debugging)
174 * @return allocated memory, never NULL
177 GNUNET_xmemdup_ (const void *buf,
179 const char *filename,
184 /* As a security precaution, we generally do not allow very large
185 * allocations here */
186 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
188 size += sizeof(size_t);
189 if (mem_used + size > W32_MEM_LIMIT)
192 GNUNET_assert_at (size < INT_MAX, filename, linenumber);
196 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc");
200 *((size_t *) ret) = size;
201 ret = &((size_t *) ret)[1];
204 GNUNET_memcpy (ret, buf, size);
210 * Wrapper around malloc(). Allocates size bytes of memory.
211 * The memory will be zero'ed out.
213 * @param size the number of bytes to allocate
214 * @param filename where in the code was the call to GNUNET_malloc_unchecked()
215 * @param linenumber where in the code was the call to GNUNET_malloc_unchecked()
216 * @return pointer to size bytes of memory, NULL if we do not have enough memory
219 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
226 size += sizeof(size_t);
227 if (mem_used + size > W32_MEM_LIMIT)
231 result = malloc (size);
234 memset (result, 0, size);
237 *((size_t *) result) = size;
238 result = &((size_t *) result)[1];
247 * Reallocate memory. Checks the return value, aborts if no more
248 * memory is available.
249 * The content of the intersection of the new and old size will be unchanged.
251 * @param ptr the pointer to reallocate
252 * @param n how many bytes of memory to allocate
253 * @param filename where in the code was the call to GNUNET_realloc()
254 * @param linenumber where in the code was the call to GNUNET_realloc()
255 * @return pointer to size bytes of memory
258 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
265 ptr = &((size_t *) ptr)[-1];
266 mem_used = mem_used - *((size_t *) ptr) + n;
268 ptr = realloc (ptr, n);
269 if ((NULL == ptr) && (n > 0))
271 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "realloc");
275 ptr = &((size_t *) ptr)[1];
281 #if __BYTE_ORDER == __LITTLE_ENDIAN
282 #define BAADFOOD_STR "\x0D\xF0\xAD\xBA"
284 #if __BYTE_ORDER == __BIG_ENDIAN
285 #define BAADFOOD_STR "\xBA\xAD\xF0\x0D"
289 #include <malloc_np.h>
291 #if HAVE_MALLOC_USABLE_SIZE
292 #define M_SIZE(p) malloc_usable_size (p)
293 #elif HAVE_MALLOC_SIZE
294 #define M_SIZE(p) malloc_size (p)
298 * Free memory. Merely a wrapper for the case that we
299 * want to keep track of allocations.
301 * @param ptr the pointer to free
302 * @param filename where in the code was the call to GNUNET_free
303 * @param linenumber where in the code was the call to GNUNET_free
306 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
308 GNUNET_assert_at (NULL != ptr, filename, linenumber);
310 ptr = &((size_t *) ptr)[-1];
311 mem_used -= *((size_t *) ptr);
316 const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
317 uint64_t *base = ptr;
318 size_t s = M_SIZE (ptr);
321 for (i = 0; i < s / 8; i++)
323 GNUNET_memcpy (&base[s / 8], &baadfood, s % 8);
332 * Dup a string (same semantics as strdup).
334 * @param str the string to dup
335 * @param filename where in the code was the call to GNUNET_strdup()
336 * @param linenumber where in the code was the call to GNUNET_strdup()
337 * @return `strdup(@a str)`
340 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
345 GNUNET_assert_at (str != NULL, filename, linenumber);
346 slen = strlen (str) + 1;
347 res = GNUNET_xmalloc_ (slen, filename, linenumber);
348 GNUNET_memcpy (res, str, slen);
355 strnlen (const char *s, size_t n)
359 e = memchr (s, '\0', n);
368 * Dup partially a string (same semantics as strndup).
370 * @param str the string to dup
371 * @param len the length of the string to dup
372 * @param filename where in the code was the call to GNUNET_strndup()
373 * @param linenumber where in the code was the call to GNUNET_strndup()
374 * @return `strndup(@a str,@a len)`
377 GNUNET_xstrndup_ (const char *str,
379 const char *filename,
385 return GNUNET_strdup ("");
386 GNUNET_assert_at (NULL != str, filename, linenumber);
387 len = strnlen (str, len);
388 res = GNUNET_xmalloc_ (len + 1, filename, linenumber);
389 GNUNET_memcpy (res, str, len);
390 /* res[len] = '\0'; 'malloc' zeros out anyway */
396 * Grow an array. Grows old by (*oldCount-newCount)*elementSize bytes
397 * and sets *oldCount to newCount.
399 * @param old address of the pointer to the array
401 * @param elementSize the size of the elements of the array
402 * @param oldCount address of the number of elements in the *old array
403 * @param newCount number of elements in the new array, may be 0
404 * @param filename where in the code was the call to GNUNET_array_grow()
405 * @param linenumber where in the code was the call to GNUNET_array_grow()
408 GNUNET_xgrow_ (void **old,
410 unsigned int *oldCount,
411 unsigned int newCount,
412 const char *filename,
418 GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
419 size = newCount * elementSize;
426 tmp = GNUNET_xmalloc_ (size, filename, linenumber);
429 GNUNET_memcpy (tmp, *old, elementSize * GNUNET_MIN (*oldCount, newCount));
435 GNUNET_xfree_ (*old, filename, linenumber);
438 *oldCount = newCount;
443 * Like asprintf(), just portable.
445 * @param buf set to a buffer of sufficient size (allocated, caller must free)
446 * @param format format string (see printf(), fprintf(), etc.)
447 * @param ... data for format string
448 * @return number of bytes in `*@a buf`, excluding 0-termination
451 GNUNET_asprintf (char **buf, const char *format, ...)
456 va_start (args, format);
457 ret = vsnprintf (NULL, 0, format, args);
459 GNUNET_assert (ret >= 0);
460 *buf = GNUNET_malloc (ret + 1);
461 va_start (args, format);
462 ret = vsprintf (*buf, format, args);
469 * Like snprintf(), just aborts if the buffer is of insufficient size.
471 * @param buf pointer to buffer that is written to
472 * @param size number of bytes in buf
473 * @param format format strings
474 * @param ... data for format string
475 * @return number of bytes written to buf or negative value on error
478 GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
483 va_start (args, format);
484 ret = vsnprintf (buf, size, format, args);
486 GNUNET_assert ((ret >= 0) && (((size_t) ret) < size));
492 * Create a copy of the given message.
494 * @param msg message to copy
495 * @return duplicate of the message
497 struct GNUNET_MessageHeader *
498 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg)
500 struct GNUNET_MessageHeader *ret;
503 msize = ntohs (msg->size);
504 GNUNET_assert (msize >= sizeof(struct GNUNET_MessageHeader));
505 ret = GNUNET_malloc (msize);
506 GNUNET_memcpy (ret, msg, msize);
511 /* end of common_allocation.c */