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);
168 if ( (NULL == ptr) && (n > 0) )
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);
220 * Dup partially a string (same semantics as strndup).
222 * @param str the string to dup
223 * @param len the lenght of the string to dup
224 * @param filename where in the code was the call to GNUNET_strndup
225 * @param linenumber where in the code was the call to GNUNET_strndup
226 * @return strndup(str,len)
229 GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, int linenumber)
233 GNUNET_assert_at (str != NULL, filename, linenumber);
234 len = GNUNET_MIN(len,strlen(str));
235 res = GNUNET_xmalloc_ (len + 1, filename, linenumber);
236 memcpy (res, str, len);
243 * Grow an array. Grows old by (*oldCount-newCount)*elementSize bytes
244 * and sets *oldCount to newCount.
246 * @param old address of the pointer to the array
248 * @param elementSize the size of the elements of the array
249 * @param oldCount address of the number of elements in the *old array
250 * @param newCount number of elements in the new array, may be 0
251 * @param filename where in the code was the call to GNUNET_array_grow
252 * @param linenumber where in the code was the call to GNUNET_array_grow
255 GNUNET_xgrow_ (void **old,
257 unsigned int *oldCount,
258 unsigned int newCount, const char *filename, int linenumber)
263 GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
264 size = newCount * elementSize;
271 tmp = GNUNET_xmalloc_ (size, filename, linenumber);
272 memset (tmp, 0, size); /* client code should not rely on this, though... */
273 if (*oldCount > newCount)
274 *oldCount = newCount; /* shrink is also allowed! */
275 memcpy (tmp, *old, elementSize * (*oldCount));
280 GNUNET_xfree_ (*old, filename, linenumber);
283 *oldCount = newCount;
288 * Like asprintf, just portable.
290 * @param buf set to a buffer of sufficient size (allocated, caller must free)
291 * @param format format string (see printf, fprintf, etc.)
292 * @param ... data for format string
293 * @return number of bytes in "*buf" excluding 0-termination
296 GNUNET_asprintf (char **buf, const char *format, ...)
301 va_start (args, format);
302 ret = VSNPRINTF (NULL, 0, format, args);
304 *buf = GNUNET_malloc (ret + 1);
305 va_start (args, format);
306 ret = VSPRINTF (*buf, format, args);
313 * Like snprintf, just aborts if the buffer is of insufficient size.
315 * @param buf pointer to buffer that is written to
316 * @param size number of bytes in buf
317 * @param format format strings
318 * @param ... data for format string
319 * @return number of bytes written to buf or negative value on error
322 GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
327 va_start (args, format);
328 ret = VSNPRINTF (buf, size, format, args);
330 GNUNET_assert (ret <= size);
334 /* end of common_allocation.c */