Tell core that we want to have this packet delivered
[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  * 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.
75  *
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
81  */
82 void *GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename, int linenumber)
83 {
84   void *ret;
85   /* As a security precaution, we generally do not allow very large
86      allocations here */
87   GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
88 #ifdef W32_MEM_LIMIT
89   size += sizeof (size_t);
90   if (mem_used + size > W32_MEM_LIMIT)
91     return NULL;
92 #endif
93   GNUNET_assert_at (size < INT_MAX, filename, linenumber);
94   ret = malloc (size);
95   if (ret == NULL)
96     {
97       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
98       abort ();
99     }
100 #ifdef W32_MEM_LIMIT
101   *((size_t *) ret) = size;
102   ret = &((size_t *) ret)[1];
103   mem_used += size;
104 #endif
105   memcpy (ret, buf, size);
106   return ret;
107 }
108
109
110
111 /**
112  * Wrapper around malloc. Allocates size bytes of memory.
113  * The memory will be zero'ed out.
114  *
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
119  */
120 void *
121 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
122 {
123   void *result;
124
125 #ifdef W32_MEM_LIMIT
126   size += sizeof (size_t);
127   if (mem_used + size > W32_MEM_LIMIT)
128     return NULL;
129 #endif
130
131   GNUNET_assert_at (size < INT_MAX, filename, linenumber);
132   result = malloc (size);
133   if (result == NULL)
134     return NULL;
135   memset (result, 0, size);
136
137 #ifdef W32_MEM_LIMIT
138   *((size_t *) result) = size;
139   result = &((size_t *) result)[1];
140   mem_used += size;
141 #endif
142
143   return result;
144 }
145
146
147 /**
148  * Reallocate memory. Checks the return value, aborts if no more
149  * memory is available.
150  *
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
156  */
157 void *
158 GNUNET_xrealloc_ (void *ptr,
159                   size_t n,
160                   const char *filename, int linenumber)
161 {
162 #ifdef W32_MEM_LIMIT
163   n += sizeof (size_t);
164   ptr = &((size_t *) ptr)[-1];
165   mem_used = mem_used - *((size_t *) ptr) + n;
166 #endif
167   ptr = realloc (ptr, n);
168   if (!ptr)
169     {
170       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "realloc");
171       abort ();
172     }
173 #ifdef W32_MEM_LIMIT
174   ptr = &((size_t *) ptr)[1];
175 #endif
176   return ptr;
177 }
178
179
180 /**
181  * Free memory. Merely a wrapper for the case that we
182  * want to keep track of allocations.
183  *
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
187  */
188 void
189 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
190 {
191   GNUNET_assert_at (ptr != NULL, filename, linenumber);
192 #ifdef W32_MEM_LIMIT
193   ptr = &((size_t *) ptr)[-1];
194   mem_used -= *((size_t *) ptr);
195 #endif
196   free (ptr);
197 }
198
199 /**
200  * Dup a string (same semantics as strdup).
201  *
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)
206  */
207 char *
208 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
209 {
210   char *res;
211
212   GNUNET_assert_at (str != NULL, filename, linenumber);
213   res = GNUNET_xmalloc_ (strlen (str) + 1, filename, linenumber);
214   memcpy (res, str, strlen (str) + 1);
215   return res;
216 }
217
218 /**
219  * Grow an array.  Grows old by (*oldCount-newCount)*elementSize bytes
220  * and sets *oldCount to newCount.
221  *
222  * @param old address of the pointer to the array
223  *        *old may be NULL
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
229  */
230 void
231 GNUNET_xgrow_ (void **old,
232                size_t elementSize,
233                unsigned int *oldCount,
234                unsigned int newCount, const char *filename, int linenumber)
235 {
236   void *tmp;
237   size_t size;
238
239   GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
240   size = newCount * elementSize;
241   if (size == 0)
242     {
243       tmp = NULL;
244     }
245   else
246     {
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));
252     }
253
254   if (*old != NULL)
255     {
256       GNUNET_xfree_ (*old, filename, linenumber);
257     }
258   *old = tmp;
259   *oldCount = newCount;
260 }
261
262
263 /**
264  * Like asprintf, just portable.
265  *
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
270  */
271 int
272 GNUNET_asprintf (char **buf, const char *format, ...)
273 {
274   int ret;
275   va_list args;
276
277   va_start (args, format);
278   ret = VSNPRINTF (NULL, 0, format, args);
279   va_end (args);
280   *buf = GNUNET_malloc (ret + 1);
281   va_start (args, format);
282   ret = VSPRINTF (*buf, format, args);
283   va_end (args);
284   return ret;
285 }
286
287
288 /**
289  * Like snprintf, just aborts if the buffer is of insufficient size.
290  *
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
296  */
297 int
298 GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
299 {
300   int ret;
301   va_list args;
302
303   va_start (args, format);
304   ret = VSNPRINTF (buf, size, format, args);
305   va_end (args);
306   GNUNET_assert (ret <= size);
307   return ret;
308 }
309
310 /* end of common_allocation.c */