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
28 #include "gnunet_common.h"
31 #define INT_MAX 0x7FFFFFFF
35 #define W32_MEM_LIMIT 200000000
39 static LONG mem_used = 0;
43 * Allocate memory. Checks the return value, aborts if no more
44 * memory is available.
46 * @param size how many bytes of memory to allocate, do NOT use
47 * this function (or GNUNET_malloc) to allocate more than several MB
48 * of memory, if you are possibly needing a very large chunk use
49 * GNUNET_xmalloc_unchecked_ instead.
50 * @param filename where in the code was the call to GNUNET_array_grow
51 * @param linenumber where in the code was the call to GNUNET_array_grow
52 * @return pointer to size bytes of memory
55 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
57 /* As a security precaution, we generally do not allow very large
58 allocations using the default 'GNUNET_malloc' macro */
59 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
60 return GNUNET_xmalloc_unchecked_ (size, filename, linenumber);
64 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
69 size += sizeof(size_t);
70 if (mem_used + size > W32_MEM_LIMIT)
74 GNUNET_assert_at (size < INT_MAX, filename, linenumber);
75 result = malloc (size);
78 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
81 memset (result, 0, size);
84 *((size_t *) result) = size;
85 result = &((size_t *) result)[1];
93 * Reallocate memory. Checks the return value, aborts if no more
94 * memory is available.
96 * @param ptr the pointer to reallocate
97 * @param n how many bytes of memory to allocate
98 * @param filename where in the code was the call to GNUNET_realloc
99 * @param linenumber where in the code was the call to GNUNET_realloc
100 * @return pointer to size bytes of memory
103 GNUNET_xrealloc_ (void *ptr,
104 #ifndef W32_MEM_LIMIT
109 const char *filename, int linenumber)
113 ptr = &((size_t *) ptr)[-1];
114 mem_used = mem_used - *((size_t *) ptr) + n;
116 ptr = realloc (ptr, n);
119 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "realloc");
123 ptr = &((size_t *) ptr)[1];
129 * Free memory. Merely a wrapper for the case that we
130 * want to keep track of allocations.
132 * @param ptr the pointer to free
133 * @param filename where in the code was the call to GNUNET_array_grow
134 * @param linenumber where in the code was the call to GNUNET_array_grow
137 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
139 GNUNET_assert_at (ptr != NULL, filename, linenumber);
141 ptr = &((size_t *) ptr)[-1];
142 mem_used -= *((size_t *) ptr);
148 * Dup a string (same semantics as strdup).
150 * @param str the string to dup
151 * @param filename where in the code was the call to GNUNET_array_grow
152 * @param linenumber where in the code was the call to GNUNET_array_grow
153 * @return strdup(str)
156 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
160 GNUNET_assert_at (str != NULL, filename, linenumber);
161 res = GNUNET_xmalloc_ (strlen (str) + 1, filename, linenumber);
162 memcpy (res, str, strlen (str) + 1);
167 * Grow an array. Grows old by (*oldCount-newCount)*elementSize bytes
168 * and sets *oldCount to newCount.
170 * @param old address of the pointer to the array
172 * @param elementSize the size of the elements of the array
173 * @param oldCount address of the number of elements in the *old array
174 * @param newCount number of elements in the new array, may be 0
175 * @param filename where in the code was the call to GNUNET_array_grow
176 * @param linenumber where in the code was the call to GNUNET_array_grow
179 GNUNET_xgrow_ (void **old,
181 unsigned int *oldCount,
182 unsigned int newCount, const char *filename, int linenumber)
187 GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
188 size = newCount * elementSize;
195 tmp = GNUNET_xmalloc_ (size, filename, linenumber);
196 memset (tmp, 0, size); /* client code should not rely on this, though... */
197 if (*oldCount > newCount)
198 *oldCount = newCount; /* shrink is also allowed! */
199 memcpy (tmp, *old, elementSize * (*oldCount));
204 GNUNET_xfree_ (*old, filename, linenumber);
207 *oldCount = newCount;
212 * Like asprintf, just portable.
214 * @param buf set to a buffer of sufficient size (allocated, caller must free)
215 * @param format format string (see printf, fprintf, etc.)
216 * @param ... data for format string
217 * @return number of bytes in "*buf" excluding 0-termination
220 GNUNET_asprintf (char **buf, const char *format, ...)
225 va_start (args, format);
226 ret = VSNPRINTF (NULL, 0, format, args);
228 *buf = GNUNET_malloc (ret + 1);
229 va_start (args, format);
230 ret = VSPRINTF (*buf, format, args);
237 * Like snprintf, just aborts if the buffer is of insufficient size.
239 * @param buf pointer to buffer that is written to
240 * @param size number of bytes in buf
241 * @param format format strings
242 * @param ... data for format string
243 * @return number of bytes written to buf or negative value on error
246 GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
251 va_start (args, format);
252 ret = VSNPRINTF (buf, size, format, args);
254 GNUNET_assert (ret <= size);
258 /* end of common_allocation.c */