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"
30 #define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
32 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
35 #define INT_MAX 0x7FFFFFFF
39 #define W32_MEM_LIMIT 200000000
43 static LONG mem_used = 0;
47 * Allocate memory. Checks the return value, aborts if no more
48 * memory is available.
50 * @param size how many bytes of memory to allocate, do NOT use
51 * this function (or GNUNET_malloc) to allocate more than several MB
52 * of memory, if you are possibly needing a very large chunk use
53 * GNUNET_xmalloc_unchecked_ instead.
54 * @param filename where in the code was the call to GNUNET_malloc
55 * @param linenumber where in the code was the call to GNUNET_malloc
56 * @return pointer to size bytes of memory
59 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
63 /* As a security precaution, we generally do not allow very large
64 * allocations using the default 'GNUNET_malloc' macro */
65 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
66 ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber);
69 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc");
77 * Allocate and initialize memory. Checks the return value, aborts if no more
78 * memory is available. Don't use GNUNET_xmemdup_ directly. Use the
79 * GNUNET_memdup macro.
81 * @param buf buffer to initialize from (must contain size bytes)
82 * @param size number of bytes to allocate
83 * @param filename where is this call being made (for debugging)
84 * @param linenumber line where this call is being made (for debugging)
85 * @return allocated memory, never NULL
88 GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
93 /* As a security precaution, we generally do not allow very large
95 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
97 size += sizeof (size_t);
98 if (mem_used + size > W32_MEM_LIMIT)
101 GNUNET_assert_at (size < INT_MAX, filename, linenumber);
105 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc");
109 *((size_t *) ret) = size;
110 ret = &((size_t *) ret)[1];
113 memcpy (ret, buf, size);
120 * Wrapper around malloc. Allocates size bytes of memory.
121 * The memory will be zero'ed out.
123 * @param size the number of bytes to allocate
124 * @param filename where in the code was the call to GNUNET_malloc_large
125 * @param linenumber where in the code was the call to GNUNET_malloc_large
126 * @return pointer to size bytes of memory, NULL if we do not have enough memory
129 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
134 size += sizeof (size_t);
135 if (mem_used + size > W32_MEM_LIMIT)
139 GNUNET_assert_at (size < INT_MAX, filename, linenumber);
140 result = malloc (size);
143 memset (result, 0, size);
146 *((size_t *) result) = size;
147 result = &((size_t *) result)[1];
156 * Reallocate memory. Checks the return value, aborts if no more
157 * memory is available.
159 * @param ptr the pointer to reallocate
160 * @param n how many bytes of memory to allocate
161 * @param filename where in the code was the call to GNUNET_realloc
162 * @param linenumber where in the code was the call to GNUNET_realloc
163 * @return pointer to size bytes of memory
166 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
169 n += sizeof (size_t);
170 ptr = &((size_t *) ptr)[-1];
171 mem_used = mem_used - *((size_t *) ptr) + n;
173 ptr = realloc (ptr, n);
174 if ((NULL == ptr) && (n > 0))
176 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "realloc");
180 ptr = &((size_t *) ptr)[1];
187 * Free memory. Merely a wrapper for the case that we
188 * want to keep track of allocations.
190 * @param ptr the pointer to free
191 * @param filename where in the code was the call to GNUNET_array_grow
192 * @param linenumber where in the code was the call to GNUNET_array_grow
195 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
197 GNUNET_assert_at (ptr != NULL, filename, linenumber);
199 ptr = &((size_t *) ptr)[-1];
200 mem_used -= *((size_t *) ptr);
206 * Dup a string (same semantics as strdup).
208 * @param str the string to dup
209 * @param filename where in the code was the call to GNUNET_strdup
210 * @param linenumber where in the code was the call to GNUNET_strdup
211 * @return strdup(str)
214 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
218 GNUNET_assert_at (str != NULL, filename, linenumber);
219 res = GNUNET_xmalloc_ (strlen (str) + 1, filename, linenumber);
220 memcpy (res, str, strlen (str) + 1);
226 * Dup partially a string (same semantics as strndup).
228 * @param str the string to dup
229 * @param len the length of the string to dup
230 * @param filename where in the code was the call to GNUNET_strndup
231 * @param linenumber where in the code was the call to GNUNET_strndup
232 * @return strndup(str,len)
235 GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
240 GNUNET_assert_at (str != NULL, filename, linenumber);
241 len = GNUNET_MIN (len, strlen (str));
242 res = GNUNET_xmalloc_ (len + 1, filename, linenumber);
243 memcpy (res, str, len);
250 * Grow an array. Grows old by (*oldCount-newCount)*elementSize bytes
251 * and sets *oldCount to newCount.
253 * @param old address of the pointer to the array
255 * @param elementSize the size of the elements of the array
256 * @param oldCount address of the number of elements in the *old array
257 * @param newCount number of elements in the new array, may be 0
258 * @param filename where in the code was the call to GNUNET_array_grow
259 * @param linenumber where in the code was the call to GNUNET_array_grow
262 GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
263 unsigned int newCount, const char *filename, int linenumber)
268 GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
269 size = newCount * elementSize;
276 tmp = GNUNET_xmalloc_ (size, filename, linenumber);
277 memset (tmp, 0, size); /* client code should not rely on this, though... */
278 if (*oldCount > newCount)
279 *oldCount = newCount; /* shrink is also allowed! */
280 memcpy (tmp, *old, elementSize * (*oldCount));
285 GNUNET_xfree_ (*old, filename, linenumber);
288 *oldCount = newCount;
293 * Like asprintf, just portable.
295 * @param buf set to a buffer of sufficient size (allocated, caller must free)
296 * @param format format string (see printf, fprintf, etc.)
297 * @param ... data for format string
298 * @return number of bytes in "*buf" excluding 0-termination
301 GNUNET_asprintf (char **buf, const char *format, ...)
306 va_start (args, format);
307 ret = VSNPRINTF (NULL, 0, format, args);
309 *buf = GNUNET_malloc (ret + 1);
310 va_start (args, format);
311 ret = VSPRINTF (*buf, format, args);
318 * Like snprintf, just aborts if the buffer is of insufficient size.
320 * @param buf pointer to buffer that is written to
321 * @param size number of bytes in buf
322 * @param format format strings
323 * @param ... data for format string
324 * @return number of bytes written to buf or negative value on error
327 GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
332 va_start (args, format);
333 ret = VSNPRINTF (buf, size, format, args);
335 GNUNET_assert (ret <= size);
341 * Create a copy of the given message.
343 * @param msg message to copy
344 * @return duplicate of the message
346 struct GNUNET_MessageHeader *
347 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg)
349 struct GNUNET_MessageHeader *ret;
352 msize = ntohs (msg->size);
353 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
354 ret = GNUNET_malloc (msize);
355 memcpy (ret, msg, msize);
360 /* end of common_allocation.c */