fix leak, improve API
[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_array_grow
51  * @param linenumber where in the code was the call to GNUNET_array_grow
52  * @return pointer to size bytes of memory
53  */
54 void *
55 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
56 {
57   /* As a security precaution, we generally do not allow very large
58      allocations using the default 'GNUNET_malloc' macro */
59   GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
60   return GNUNET_xmalloc_unchecked_ (size, filename, linenumber);
61 }
62
63 void *
64 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
65 {
66   void *result;
67
68 #ifdef W32_MEM_LIMIT
69   size += sizeof(size_t);
70   if (mem_used + size > W32_MEM_LIMIT)
71     return NULL;
72 #endif
73
74   GNUNET_assert_at (size < INT_MAX, filename, linenumber);
75   result = malloc (size);
76   if (result == NULL)
77     {
78       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
79       abort ();
80     }
81   memset (result, 0, size);
82
83 #ifdef W32_MEM_LIMIT
84   *((size_t *) result) = size;
85   result = &((size_t *) result)[1];
86   mem_used += size;
87 #endif
88
89   return result;
90 }
91
92 /**
93  * Reallocate memory. Checks the return value, aborts if no more
94  * memory is available.
95  *
96  * @param ptr the pointer to reallocate
97  * @param n how many bytes of memory to allocate
98  * @param filename where in the code was the call to GNUNET_realloc
99  * @param linenumber where in the code was the call to GNUNET_realloc
100  * @return pointer to size bytes of memory
101  */
102 void *
103 GNUNET_xrealloc_ (void *ptr,
104 #ifndef W32_MEM_LIMIT
105                   const size_t n,
106 #else
107                   size_t n,
108 #endif
109                   const char *filename, int linenumber)
110 {
111 #ifdef W32_MEM_LIMIT
112   n += sizeof(size_t);
113   ptr = &((size_t *) ptr)[-1];
114   mem_used = mem_used - *((size_t *) ptr) + n;
115 #endif
116   ptr = realloc (ptr, n);
117   if (!ptr)
118     {
119       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "realloc");
120       abort ();
121     }
122 #ifdef W32_MEM_LIMIT
123   ptr = &((size_t *) ptr)[1];
124 #endif
125   return ptr;
126 }
127
128 /**
129  * Free memory. Merely a wrapper for the case that we
130  * want to keep track of allocations.
131  *
132  * @param ptr the pointer to free
133  * @param filename where in the code was the call to GNUNET_array_grow
134  * @param linenumber where in the code was the call to GNUNET_array_grow
135  */
136 void
137 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
138 {
139   GNUNET_assert_at (ptr != NULL, filename, linenumber);
140 #ifdef W32_MEM_LIMIT
141   ptr = &((size_t *) ptr)[-1];
142   mem_used -= *((size_t *) ptr);
143 #endif
144   free (ptr);
145 }
146
147 /**
148  * Dup a string (same semantics as strdup).
149  *
150  * @param str the string to dup
151  * @param filename where in the code was the call to GNUNET_array_grow
152  * @param linenumber where in the code was the call to GNUNET_array_grow
153  * @return strdup(str)
154  */
155 char *
156 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
157 {
158   char *res;
159
160   GNUNET_assert_at (str != NULL, filename, linenumber);
161   res = GNUNET_xmalloc_ (strlen (str) + 1, filename, linenumber);
162   memcpy (res, str, strlen (str) + 1);
163   return res;
164 }
165
166 /**
167  * Grow an array.  Grows old by (*oldCount-newCount)*elementSize bytes
168  * and sets *oldCount to newCount.
169  *
170  * @param old address of the pointer to the array
171  *        *old may be NULL
172  * @param elementSize the size of the elements of the array
173  * @param oldCount address of the number of elements in the *old array
174  * @param newCount number of elements in the new array, may be 0
175  * @param filename where in the code was the call to GNUNET_array_grow
176  * @param linenumber where in the code was the call to GNUNET_array_grow
177  */
178 void
179 GNUNET_xgrow_ (void **old,
180                size_t elementSize,
181                unsigned int *oldCount,
182                unsigned int newCount, const char *filename, int linenumber)
183 {
184   void *tmp;
185   size_t size;
186
187   GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
188   size = newCount * elementSize;
189   if (size == 0)
190     {
191       tmp = NULL;
192     }
193   else
194     {
195       tmp = GNUNET_xmalloc_ (size, filename, linenumber);
196       memset (tmp, 0, size);    /* client code should not rely on this, though... */
197       if (*oldCount > newCount)
198         *oldCount = newCount;   /* shrink is also allowed! */
199       memcpy (tmp, *old, elementSize * (*oldCount));
200     }
201
202   if (*old != NULL)
203     {
204       GNUNET_xfree_ (*old, filename, linenumber);
205     }
206   *old = tmp;
207   *oldCount = newCount;
208 }
209
210
211 /**
212  * Like asprintf, just portable.
213  *
214  * @param buf set to a buffer of sufficient size (allocated, caller must free)
215  * @param format format string (see printf, fprintf, etc.)
216  * @param ... data for format string
217  * @return number of bytes in "*buf" excluding 0-termination
218  */
219 int
220 GNUNET_asprintf (char **buf, const char *format, ...)
221 {
222   int ret;
223   va_list args;
224
225   va_start (args, format);
226   ret = VSNPRINTF (NULL, 0, format, args);
227   va_end (args);
228   *buf = GNUNET_malloc (ret + 1);
229   va_start (args, format);
230   ret = VSPRINTF (*buf, format, args);
231   va_end (args);
232   return ret;
233 }
234
235
236 /**
237  * Like snprintf, just aborts if the buffer is of insufficient size.
238  *
239  * @param buf pointer to buffer that is written to
240  * @param size number of bytes in buf
241  * @param format format strings
242  * @param ... data for format string
243  * @return number of bytes written to buf or negative value on error
244  */
245 int
246 GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
247 {
248   int ret;
249   va_list args;
250
251   va_start (args, format);
252   ret = VSNPRINTF (buf, size, format, args);
253   va_end (args);
254   GNUNET_assert (ret <= size);
255   return ret;
256 }
257
258 /* end of common_allocation.c */