2 This file is part of GNUnet.
3 (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 2, 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_common.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, const char *filename, int linenumber)
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, filename, linenumber);
71 ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber);
74 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc");
82 * Allocate and initialize memory. Checks the return value, aborts if no more
83 * memory is available. Don't use GNUNET_xmemdup_ directly. Use the
84 * GNUNET_memdup macro.
86 * @param buf buffer to initialize from (must contain size bytes)
87 * @param size number of bytes to allocate
88 * @param filename where is this call being made (for debugging)
89 * @param linenumber line where this call is being made (for debugging)
90 * @return allocated memory, never NULL
93 GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
98 /* As a security precaution, we generally do not allow very large
100 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
102 size += sizeof (size_t);
103 if (mem_used + size > W32_MEM_LIMIT)
106 GNUNET_assert_at (size < INT_MAX, filename, linenumber);
110 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc");
114 *((size_t *) ret) = size;
115 ret = &((size_t *) ret)[1];
118 memcpy (ret, buf, size);
125 * Wrapper around malloc. Allocates size bytes of memory.
126 * The memory will be zero'ed out.
128 * @param size the number of bytes to allocate
129 * @param filename where in the code was the call to GNUNET_malloc_large
130 * @param linenumber where in the code was the call to GNUNET_malloc_large
131 * @return pointer to size bytes of memory, NULL if we do not have enough memory
134 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
139 size += sizeof (size_t);
140 if (mem_used + size > W32_MEM_LIMIT)
144 result = malloc (size);
147 memset (result, 0, size);
150 *((size_t *) result) = size;
151 result = &((size_t *) result)[1];
160 * Reallocate memory. Checks the return value, aborts if no more
161 * memory is available.
163 * @param ptr the pointer to reallocate
164 * @param n how many bytes of memory to allocate
165 * @param filename where in the code was the call to GNUNET_realloc
166 * @param linenumber where in the code was the call to GNUNET_realloc
167 * @return pointer to size bytes of memory
170 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
173 n += sizeof (size_t);
174 ptr = &((size_t *) ptr)[-1];
175 mem_used = mem_used - *((size_t *) ptr) + n;
177 ptr = realloc (ptr, n);
178 if ((NULL == ptr) && (n > 0))
180 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "realloc");
184 ptr = &((size_t *) ptr)[1];
190 # if __BYTE_ORDER == __LITTLE_ENDIAN
191 #define BAADFOOD_STR "\x0D\xF0\xAD\xBA"
193 # if __BYTE_ORDER == __BIG_ENDIAN
194 #define BAADFOOD_STR "\xBA\xAD\xF0\x0D"
198 #define M_SIZE(p) _msize (p)
200 #if HAVE_MALLOC_USABLE_SIZE
201 #define M_SIZE(p) malloc_usable_size (p)
202 #elif HAVE_MALLOC_SIZE
203 #define M_SIZE(p) malloc_size (p)
207 * Free memory. Merely a wrapper for the case that we
208 * want to keep track of allocations.
210 * @param ptr the pointer to free
211 * @param filename where in the code was the call to GNUNET_array_grow
212 * @param linenumber where in the code was the call to GNUNET_array_grow
215 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
217 GNUNET_assert_at (ptr != NULL, filename, linenumber);
219 ptr = &((size_t *) ptr)[-1];
220 mem_used -= *((size_t *) ptr);
226 char baadfood[5] = BAADFOOD_STR;
227 size_t s = M_SIZE (ptr);
228 for (i = 0; i < s; i++)
229 ((char *) ptr)[i] = baadfood[i % 4];
237 * Dup a string (same semantics as strdup).
239 * @param str the string to dup
240 * @param filename where in the code was the call to GNUNET_strdup
241 * @param linenumber where in the code was the call to GNUNET_strdup
242 * @return strdup(str)
245 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
249 GNUNET_assert_at (str != NULL, filename, linenumber);
250 res = GNUNET_xmalloc_ (strlen (str) + 1, filename, linenumber);
251 memcpy (res, str, strlen (str) + 1);
257 * Dup partially a string (same semantics as strndup).
259 * @param str the string to dup
260 * @param len the length of the string to dup
261 * @param filename where in the code was the call to GNUNET_strndup
262 * @param linenumber where in the code was the call to GNUNET_strndup
263 * @return strndup(str,len)
266 GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
271 GNUNET_assert_at (str != NULL, filename, linenumber);
272 len = GNUNET_MIN (len, strlen (str));
273 res = GNUNET_xmalloc_ (len + 1, filename, linenumber);
274 memcpy (res, str, len);
275 /* res[len] = '\0'; 'malloc' zeros out anyway */
281 * Grow an array. Grows old by (*oldCount-newCount)*elementSize bytes
282 * and sets *oldCount to newCount.
284 * @param old address of the pointer to the array
286 * @param elementSize the size of the elements of the array
287 * @param oldCount address of the number of elements in the *old array
288 * @param newCount number of elements in the new array, may be 0
289 * @param filename where in the code was the call to GNUNET_array_grow
290 * @param linenumber where in the code was the call to GNUNET_array_grow
293 GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
294 unsigned int newCount, const char *filename, int linenumber)
299 GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
300 size = newCount * elementSize;
307 tmp = GNUNET_xmalloc_ (size, filename, linenumber);
308 memset (tmp, 0, size); /* client code should not rely on this, though... */
309 if (*oldCount > newCount)
310 *oldCount = newCount; /* shrink is also allowed! */
311 memcpy (tmp, *old, elementSize * (*oldCount));
316 GNUNET_xfree_ (*old, filename, linenumber);
319 *oldCount = newCount;
324 * Like asprintf, just portable.
326 * @param buf set to a buffer of sufficient size (allocated, caller must free)
327 * @param format format string (see printf, fprintf, etc.)
328 * @param ... data for format string
329 * @return number of bytes in "*buf" excluding 0-termination
332 GNUNET_asprintf (char **buf, const char *format, ...)
337 va_start (args, format);
338 ret = VSNPRINTF (NULL, 0, format, args);
340 *buf = GNUNET_malloc (ret + 1);
341 va_start (args, format);
342 ret = VSPRINTF (*buf, format, args);
349 * Like snprintf, just aborts if the buffer is of insufficient size.
351 * @param buf pointer to buffer that is written to
352 * @param size number of bytes in buf
353 * @param format format strings
354 * @param ... data for format string
355 * @return number of bytes written to buf or negative value on error
358 GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
363 va_start (args, format);
364 ret = VSNPRINTF (buf, size, format, args);
366 GNUNET_assert (ret <= size);
372 * Create a copy of the given message.
374 * @param msg message to copy
375 * @return duplicate of the message
377 struct GNUNET_MessageHeader *
378 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg)
380 struct GNUNET_MessageHeader *ret;
383 msize = ntohs (msg->size);
384 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
385 ret = GNUNET_malloc (msize);
386 memcpy (ret, msg, msize);
391 /* end of common_allocation.c */