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_malloc
51 * @param linenumber where in the code was the call to GNUNET_malloc
52 * @return pointer to size bytes of memory
55 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
58 /* As a security precaution, we generally do not allow very large
59 allocations using the default 'GNUNET_malloc' macro */
60 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
61 ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber);
64 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
72 * Allocate and initialize memory. Checks the return value, aborts if no more
73 * memory is available. Don't use GNUNET_xmemdup_ directly. Use the
74 * GNUNET_memdup macro.
76 * @param buf buffer to initialize from (must contain size bytes)
77 * @param size number of bytes to allocate
78 * @param filename where is this call being made (for debugging)
79 * @param linenumber line where this call is being made (for debugging)
80 * @return allocated memory, never NULL
82 void *GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename, int linenumber)
85 /* As a security precaution, we generally do not allow very large
87 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
89 size += sizeof (size_t);
90 if (mem_used + size > W32_MEM_LIMIT)
93 GNUNET_assert_at (size < INT_MAX, filename, linenumber);
97 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
101 *((size_t *) ret) = size;
102 ret = &((size_t *) ret)[1];
105 memcpy (ret, buf, size);
112 * Wrapper around malloc. Allocates size bytes of memory.
113 * The memory will be zero'ed out.
115 * @param size the number of bytes to allocate
116 * @param filename where in the code was the call to GNUNET_malloc_large
117 * @param linenumber where in the code was the call to GNUNET_malloc_large
118 * @return pointer to size bytes of memory, NULL if we do not have enough memory
121 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
126 size += sizeof (size_t);
127 if (mem_used + size > W32_MEM_LIMIT)
131 GNUNET_assert_at (size < INT_MAX, filename, linenumber);
132 result = malloc (size);
135 memset (result, 0, size);
138 *((size_t *) result) = size;
139 result = &((size_t *) result)[1];
148 * Reallocate memory. Checks the return value, aborts if no more
149 * memory is available.
151 * @param ptr the pointer to reallocate
152 * @param n how many bytes of memory to allocate
153 * @param filename where in the code was the call to GNUNET_realloc
154 * @param linenumber where in the code was the call to GNUNET_realloc
155 * @return pointer to size bytes of memory
158 GNUNET_xrealloc_ (void *ptr,
160 const char *filename, int linenumber)
163 n += sizeof (size_t);
164 ptr = &((size_t *) ptr)[-1];
165 mem_used = mem_used - *((size_t *) ptr) + n;
167 ptr = realloc (ptr, n);
170 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "realloc");
174 ptr = &((size_t *) ptr)[1];
181 * Free memory. Merely a wrapper for the case that we
182 * want to keep track of allocations.
184 * @param ptr the pointer to free
185 * @param filename where in the code was the call to GNUNET_array_grow
186 * @param linenumber where in the code was the call to GNUNET_array_grow
189 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
191 GNUNET_assert_at (ptr != NULL, filename, linenumber);
193 ptr = &((size_t *) ptr)[-1];
194 mem_used -= *((size_t *) ptr);
200 * Dup a string (same semantics as strdup).
202 * @param str the string to dup
203 * @param filename where in the code was the call to GNUNET_strdup
204 * @param linenumber where in the code was the call to GNUNET_strdup
205 * @return strdup(str)
208 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
212 GNUNET_assert_at (str != NULL, filename, linenumber);
213 res = GNUNET_xmalloc_ (strlen (str) + 1, filename, linenumber);
214 memcpy (res, str, strlen (str) + 1);
219 * Grow an array. Grows old by (*oldCount-newCount)*elementSize bytes
220 * and sets *oldCount to newCount.
222 * @param old address of the pointer to the array
224 * @param elementSize the size of the elements of the array
225 * @param oldCount address of the number of elements in the *old array
226 * @param newCount number of elements in the new array, may be 0
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_xgrow_ (void **old,
233 unsigned int *oldCount,
234 unsigned int newCount, const char *filename, int linenumber)
239 GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
240 size = newCount * elementSize;
247 tmp = GNUNET_xmalloc_ (size, filename, linenumber);
248 memset (tmp, 0, size); /* client code should not rely on this, though... */
249 if (*oldCount > newCount)
250 *oldCount = newCount; /* shrink is also allowed! */
251 memcpy (tmp, *old, elementSize * (*oldCount));
256 GNUNET_xfree_ (*old, filename, linenumber);
259 *oldCount = newCount;
264 * Like asprintf, just portable.
266 * @param buf set to a buffer of sufficient size (allocated, caller must free)
267 * @param format format string (see printf, fprintf, etc.)
268 * @param ... data for format string
269 * @return number of bytes in "*buf" excluding 0-termination
272 GNUNET_asprintf (char **buf, const char *format, ...)
277 va_start (args, format);
278 ret = VSNPRINTF (NULL, 0, format, args);
280 *buf = GNUNET_malloc (ret + 1);
281 va_start (args, format);
282 ret = VSPRINTF (*buf, format, args);
289 * Like snprintf, just aborts if the buffer is of insufficient size.
291 * @param buf pointer to buffer that is written to
292 * @param size number of bytes in buf
293 * @param format format strings
294 * @param ... data for format string
295 * @return number of bytes written to buf or negative value on error
298 GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
303 va_start (args, format);
304 ret = VSNPRINTF (buf, size, format, args);
306 GNUNET_assert (ret <= size);
310 /* end of common_allocation.c */