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