1ae01ca49f43b4a5885ddab3ceabcef1fd2abbfa
[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 /**
220  * Dup partially a string (same semantics as strndup).
221  *
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)
227  */
228 char *
229 GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, int linenumber)
230 {
231   char *res;
232
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);
237   res[len] = '\0';
238   return res;
239 }
240
241
242 /**
243  * Grow an array.  Grows old by (*oldCount-newCount)*elementSize bytes
244  * and sets *oldCount to newCount.
245  *
246  * @param old address of the pointer to the array
247  *        *old may be NULL
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
253  */
254 void
255 GNUNET_xgrow_ (void **old,
256                size_t elementSize,
257                unsigned int *oldCount,
258                unsigned int newCount, const char *filename, int linenumber)
259 {
260   void *tmp;
261   size_t size;
262
263   GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
264   size = newCount * elementSize;
265   if (size == 0)
266     {
267       tmp = NULL;
268     }
269   else
270     {
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));
276     }
277
278   if (*old != NULL)
279     {
280       GNUNET_xfree_ (*old, filename, linenumber);
281     }
282   *old = tmp;
283   *oldCount = newCount;
284 }
285
286
287 /**
288  * Like asprintf, just portable.
289  *
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
294  */
295 int
296 GNUNET_asprintf (char **buf, const char *format, ...)
297 {
298   int ret;
299   va_list args;
300
301   va_start (args, format);
302   ret = VSNPRINTF (NULL, 0, format, args);
303   va_end (args);
304   *buf = GNUNET_malloc (ret + 1);
305   va_start (args, format);
306   ret = VSPRINTF (*buf, format, args);
307   va_end (args);
308   return ret;
309 }
310
311
312 /**
313  * Like snprintf, just aborts if the buffer is of insufficient size.
314  *
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
320  */
321 int
322 GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
323 {
324   int ret;
325   va_list args;
326
327   va_start (args, format);
328   ret = VSNPRINTF (buf, size, format, args);
329   va_end (args);
330   GNUNET_assert (ret <= size);
331   return ret;
332 }
333
334 /* end of common_allocation.c */