2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2003, 2005, 2006 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/common_allocation.c
23 * @brief wrapper around malloc/free
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
31 #if HAVE_MALLOC_MALLOC_H
32 #include <malloc/malloc.h>
35 #define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
37 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", 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 and initialize memory. Checks the return value, aborts if no more
90 * memory is available. Don't use #GNUNET_xmemdup_() directly. Use the
91 * GNUNET_memdup() macro.
93 * @param buf buffer to initialize from (must contain size bytes)
94 * @param size number of bytes to allocate
95 * @param filename where is this call being made (for debugging)
96 * @param linenumber line where this call is being made (for debugging)
97 * @return allocated memory, never NULL
100 GNUNET_xmemdup_ (const void *buf,
102 const char *filename,
107 /* As a security precaution, we generally do not allow very large
108 * allocations here */
109 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
111 size += sizeof (size_t);
112 if (mem_used + size > W32_MEM_LIMIT)
115 GNUNET_assert_at (size < INT_MAX, filename, linenumber);
119 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc");
123 *((size_t *) ret) = size;
124 ret = &((size_t *) ret)[1];
127 memcpy (ret, buf, size);
133 * Wrapper around malloc(). Allocates size bytes of memory.
134 * The memory will be zero'ed out.
136 * @param size the number of bytes to allocate
137 * @param filename where in the code was the call to GNUNET_malloc_unchecked()
138 * @param linenumber where in the code was the call to GNUNET_malloc_unchecked()
139 * @return pointer to size bytes of memory, NULL if we do not have enough memory
142 GNUNET_xmalloc_unchecked_ (size_t size,
143 const char *filename,
149 size += sizeof (size_t);
150 if (mem_used + size > W32_MEM_LIMIT)
154 result = malloc (size);
157 memset (result, 0, size);
160 *((size_t *) result) = size;
161 result = &((size_t *) result)[1];
170 * Reallocate memory. Checks the return value, aborts if no more
171 * memory is available.
173 * @param ptr the pointer to reallocate
174 * @param n how many bytes of memory to allocate
175 * @param filename where in the code was the call to GNUNET_realloc()
176 * @param linenumber where in the code was the call to GNUNET_realloc()
177 * @return pointer to size bytes of memory
180 GNUNET_xrealloc_ (void *ptr,
182 const char *filename,
186 n += sizeof (size_t);
187 ptr = &((size_t *) ptr)[-1];
188 mem_used = mem_used - *((size_t *) ptr) + n;
190 ptr = realloc (ptr, n);
191 if ((NULL == ptr) && (n > 0))
193 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "realloc");
197 ptr = &((size_t *) ptr)[1];
203 # if __BYTE_ORDER == __LITTLE_ENDIAN
204 #define BAADFOOD_STR "\x0D\xF0\xAD\xBA"
206 # if __BYTE_ORDER == __BIG_ENDIAN
207 #define BAADFOOD_STR "\xBA\xAD\xF0\x0D"
211 #define M_SIZE(p) _msize (p)
214 #include <malloc_np.h>
216 #if HAVE_MALLOC_USABLE_SIZE
217 #define M_SIZE(p) malloc_usable_size (p)
218 #elif HAVE_MALLOC_SIZE
219 #define M_SIZE(p) malloc_size (p)
223 * Free memory. Merely a wrapper for the case that we
224 * want to keep track of allocations.
226 * @param ptr the pointer to free
227 * @param filename where in the code was the call to GNUNET_array_grow
228 * @param linenumber where in the code was the call to GNUNET_array_grow
231 GNUNET_xfree_ (void *ptr,
232 const char *filename,
235 GNUNET_assert_at (NULL != ptr,
239 ptr = &((size_t *) ptr)[-1];
240 mem_used -= *((size_t *) ptr);
245 const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
246 uint64_t *base = ptr;
247 size_t s = M_SIZE (ptr);
252 memcpy (&base[s/8], &baadfood, s % 8);
261 * Dup a string (same semantics as strdup).
263 * @param str the string to dup
264 * @param filename where in the code was the call to GNUNET_strdup()
265 * @param linenumber where in the code was the call to GNUNET_strdup()
266 * @return `strdup(@a str)`
269 GNUNET_xstrdup_ (const char *str,
270 const char *filename,
276 GNUNET_assert_at (str != NULL,
279 slen = strlen (str) + 1;
280 res = GNUNET_xmalloc_ (slen,
292 strnlen (const char *s,
297 e = memchr (s, '\0', n);
306 * Dup partially a string (same semantics as strndup).
308 * @param str the string to dup
309 * @param len the length of the string to dup
310 * @param filename where in the code was the call to GNUNET_strndup()
311 * @param linenumber where in the code was the call to GNUNET_strndup()
312 * @return `strndup(@a str,@a len)`
315 GNUNET_xstrndup_ (const char *str,
317 const char *filename,
323 return GNUNET_strdup ("");
324 GNUNET_assert_at (NULL != str,
329 res = GNUNET_xmalloc_ (len + 1,
332 memcpy (res, str, len);
333 /* res[len] = '\0'; 'malloc' zeros out anyway */
339 * Grow an array. Grows old by (*oldCount-newCount)*elementSize bytes
340 * and sets *oldCount to newCount.
342 * @param old address of the pointer to the array
344 * @param elementSize the size of the elements of the array
345 * @param oldCount address of the number of elements in the *old array
346 * @param newCount number of elements in the new array, may be 0
347 * @param filename where in the code was the call to GNUNET_array_grow()
348 * @param linenumber where in the code was the call to GNUNET_array_grow()
351 GNUNET_xgrow_ (void **old,
353 unsigned int *oldCount,
354 unsigned int newCount,
355 const char *filename,
361 GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
362 size = newCount * elementSize;
369 tmp = GNUNET_xmalloc_ (size, filename, linenumber);
370 memset (tmp, 0, size); /* client code should not rely on this, though... */
371 if (*oldCount > newCount)
372 *oldCount = newCount; /* shrink is also allowed! */
373 memcpy (tmp, *old, elementSize * (*oldCount));
378 GNUNET_xfree_ (*old, filename, linenumber);
381 *oldCount = newCount;
386 * Like asprintf(), just portable.
388 * @param buf set to a buffer of sufficient size (allocated, caller must free)
389 * @param format format string (see printf(), fprintf(), etc.)
390 * @param ... data for format string
391 * @return number of bytes in `*@a buf`, excluding 0-termination
394 GNUNET_asprintf (char **buf,
401 va_start (args, format);
402 ret = VSNPRINTF (NULL, 0, format, args);
404 *buf = GNUNET_malloc (ret + 1);
405 va_start (args, format);
406 ret = VSPRINTF (*buf, format, args);
413 * Like snprintf(), just aborts if the buffer is of insufficient size.
415 * @param buf pointer to buffer that is written to
416 * @param size number of bytes in buf
417 * @param format format strings
418 * @param ... data for format string
419 * @return number of bytes written to buf or negative value on error
422 GNUNET_snprintf (char *buf,
424 const char *format, ...)
429 va_start (args, format);
430 ret = VSNPRINTF (buf, size, format, args);
432 GNUNET_assert (ret < size);
438 * Create a copy of the given message.
440 * @param msg message to copy
441 * @return duplicate of the message
443 struct GNUNET_MessageHeader *
444 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg)
446 struct GNUNET_MessageHeader *ret;
449 msize = ntohs (msg->size);
450 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
451 ret = GNUNET_malloc (msize);
452 memcpy (ret, msg, msize);
457 /* end of common_allocation.c */