e62c12d086254b014391076ddf4800ab141148d1
[oweals/gnunet.git] / src / util / common_allocation.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2005, 2006 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file util/common_allocation.c
23  * @brief wrapper around malloc/free
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29
30 #ifndef INT_MAX
31 #define INT_MAX 0x7FFFFFFF
32 #endif
33
34 #if 0
35 #define W32_MEM_LIMIT 200000000
36 #endif
37
38 #ifdef W32_MEM_LIMIT
39 static LONG mem_used = 0;
40 #endif
41
42 /**
43  * Allocate memory. Checks the return value, aborts if no more
44  * memory is available.
45  *
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
53  */
54 void *
55 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
56 {
57   void *ret;
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);
62   if (ret == NULL)
63     {
64       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
65       abort ();
66     }
67   return ret;
68 }
69
70
71 /**
72  * Wrapper around malloc. Allocates size bytes of memory.
73  * The memory will be zero'ed out.
74  *
75  * @param size the number of bytes to allocate
76  * @param filename where in the code was the call to GNUNET_malloc_large
77  * @param linenumber where in the code was the call to GNUNET_malloc_large
78  * @return pointer to size bytes of memory, NULL if we do not have enough memory
79  */
80 void *
81 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
82 {
83   void *result;
84
85 #ifdef W32_MEM_LIMIT
86   size += sizeof (size_t);
87   if (mem_used + size > W32_MEM_LIMIT)
88     return NULL;
89 #endif
90
91   GNUNET_assert_at (size < INT_MAX, filename, linenumber);
92   result = malloc (size);
93   if (result == NULL)
94     return NULL;
95   memset (result, 0, size);
96
97 #ifdef W32_MEM_LIMIT
98   *((size_t *) result) = size;
99   result = &((size_t *) result)[1];
100   mem_used += size;
101 #endif
102
103   return result;
104 }
105
106 /**
107  * Reallocate memory. Checks the return value, aborts if no more
108  * memory is available.
109  *
110  * @param ptr the pointer to reallocate
111  * @param n how many bytes of memory to allocate
112  * @param filename where in the code was the call to GNUNET_realloc
113  * @param linenumber where in the code was the call to GNUNET_realloc
114  * @return pointer to size bytes of memory
115  */
116 void *
117 GNUNET_xrealloc_ (void *ptr,
118 #ifndef W32_MEM_LIMIT
119                   const size_t n,
120 #else
121                   size_t n,
122 #endif
123                   const char *filename, int linenumber)
124 {
125 #ifdef W32_MEM_LIMIT
126   n += sizeof (size_t);
127   ptr = &((size_t *) ptr)[-1];
128   mem_used = mem_used - *((size_t *) ptr) + n;
129 #endif
130   ptr = realloc (ptr, n);
131   if (!ptr)
132     {
133       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "realloc");
134       abort ();
135     }
136 #ifdef W32_MEM_LIMIT
137   ptr = &((size_t *) ptr)[1];
138 #endif
139   return ptr;
140 }
141
142 /**
143  * Free memory. Merely a wrapper for the case that we
144  * want to keep track of allocations.
145  *
146  * @param ptr the pointer to free
147  * @param filename where in the code was the call to GNUNET_array_grow
148  * @param linenumber where in the code was the call to GNUNET_array_grow
149  */
150 void
151 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
152 {
153   GNUNET_assert_at (ptr != NULL, filename, linenumber);
154 #ifdef W32_MEM_LIMIT
155   ptr = &((size_t *) ptr)[-1];
156   mem_used -= *((size_t *) ptr);
157 #endif
158   free (ptr);
159 }
160
161 /**
162  * Dup a string (same semantics as strdup).
163  *
164  * @param str the string to dup
165  * @param filename where in the code was the call to GNUNET_strdup
166  * @param linenumber where in the code was the call to GNUNET_strdup
167  * @return strdup(str)
168  */
169 char *
170 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
171 {
172   char *res;
173
174   GNUNET_assert_at (str != NULL, filename, linenumber);
175   res = GNUNET_xmalloc_ (strlen (str) + 1, filename, linenumber);
176   memcpy (res, str, strlen (str) + 1);
177   return res;
178 }
179
180 /**
181  * Grow an array.  Grows old by (*oldCount-newCount)*elementSize bytes
182  * and sets *oldCount to newCount.
183  *
184  * @param old address of the pointer to the array
185  *        *old may be NULL
186  * @param elementSize the size of the elements of the array
187  * @param oldCount address of the number of elements in the *old array
188  * @param newCount number of elements in the new array, may be 0
189  * @param filename where in the code was the call to GNUNET_array_grow
190  * @param linenumber where in the code was the call to GNUNET_array_grow
191  */
192 void
193 GNUNET_xgrow_ (void **old,
194                size_t elementSize,
195                unsigned int *oldCount,
196                unsigned int newCount, const char *filename, int linenumber)
197 {
198   void *tmp;
199   size_t size;
200
201   GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
202   size = newCount * elementSize;
203   if (size == 0)
204     {
205       tmp = NULL;
206     }
207   else
208     {
209       tmp = GNUNET_xmalloc_ (size, filename, linenumber);
210       memset (tmp, 0, size);    /* client code should not rely on this, though... */
211       if (*oldCount > newCount)
212         *oldCount = newCount;   /* shrink is also allowed! */
213       memcpy (tmp, *old, elementSize * (*oldCount));
214     }
215
216   if (*old != NULL)
217     {
218       GNUNET_xfree_ (*old, filename, linenumber);
219     }
220   *old = tmp;
221   *oldCount = newCount;
222 }
223
224
225 /**
226  * Like asprintf, just portable.
227  *
228  * @param buf set to a buffer of sufficient size (allocated, caller must free)
229  * @param format format string (see printf, fprintf, etc.)
230  * @param ... data for format string
231  * @return number of bytes in "*buf" excluding 0-termination
232  */
233 int
234 GNUNET_asprintf (char **buf, const char *format, ...)
235 {
236   int ret;
237   va_list args;
238
239   va_start (args, format);
240   ret = VSNPRINTF (NULL, 0, format, args);
241   va_end (args);
242   *buf = GNUNET_malloc (ret + 1);
243   va_start (args, format);
244   ret = VSPRINTF (*buf, format, args);
245   va_end (args);
246   return ret;
247 }
248
249
250 /**
251  * Like snprintf, just aborts if the buffer is of insufficient size.
252  *
253  * @param buf pointer to buffer that is written to
254  * @param size number of bytes in buf
255  * @param format format strings
256  * @param ... data for format string
257  * @return number of bytes written to buf or negative value on error
258  */
259 int
260 GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
261 {
262   int ret;
263   va_list args;
264
265   va_start (args, format);
266   ret = VSNPRINTF (buf, size, format, args);
267   va_end (args);
268   GNUNET_assert (ret <= size);
269   return ret;
270 }
271
272 /* end of common_allocation.c */