aboutsummaryrefslogtreecommitdiff
path: root/src/util/common_allocation.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2015-04-17 22:57:40 +0000
committerChristian Grothoff <christian@grothoff.org>2015-04-17 22:57:40 +0000
commit0eec6dcddb60bd17c5227d8da94cb06bf8b363f3 (patch)
tree2c7fbc82496c5026069aeb06fb8d3e2a2963532f /src/util/common_allocation.c
parentdc08cf45466193ee67fbbe97cddee413f0273cff (diff)
downloadgnunet-0eec6dcddb60bd17c5227d8da94cb06bf8b363f3.tar.gz
gnunet-0eec6dcddb60bd17c5227d8da94cb06bf8b363f3.zip
-doxygen, indentation
Diffstat (limited to 'src/util/common_allocation.c')
-rw-r--r--src/util/common_allocation.c141
1 files changed, 92 insertions, 49 deletions
diff --git a/src/util/common_allocation.c b/src/util/common_allocation.c
index c31ad6253..5280d73b2 100644
--- a/src/util/common_allocation.c
+++ b/src/util/common_allocation.c
@@ -53,25 +53,32 @@ static LONG mem_used = 0;
53 * memory is available. 53 * memory is available.
54 * 54 *
55 * @param size how many bytes of memory to allocate, do NOT use 55 * @param size how many bytes of memory to allocate, do NOT use
56 * this function (or GNUNET_malloc) to allocate more than several MB 56 * this function (or GNUNET_malloc()) to allocate more than several MB
57 * of memory, if you are possibly needing a very large chunk use 57 * of memory, if you are possibly needing a very large chunk use
58 * GNUNET_xmalloc_unchecked_ instead. 58 * #GNUNET_xmalloc_unchecked_() instead.
59 * @param filename where in the code was the call to GNUNET_malloc 59 * @param filename where in the code was the call to GNUNET_malloc()
60 * @param linenumber where in the code was the call to GNUNET_malloc 60 * @param linenumber where in the code was the call to GNUNET_malloc()
61 * @return pointer to size bytes of memory 61 * @return pointer to size bytes of memory
62 */ 62 */
63void * 63void *
64GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber) 64GNUNET_xmalloc_ (size_t size,
65 const char *filename,
66 int linenumber)
65{ 67{
66 void *ret; 68 void *ret;
67 69
68 /* As a security precaution, we generally do not allow very large 70 /* As a security precaution, we generally do not allow very large
69 * allocations using the default 'GNUNET_malloc' macro */ 71 * allocations using the default 'GNUNET_malloc()' macro */
70 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber); 72 GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED,
71 ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber); 73 filename,
72 if (ret == NULL) 74 linenumber);
75 ret = GNUNET_xmalloc_unchecked_ (size,
76 filename,
77 linenumber);
78 if (NULL == ret)
73 { 79 {
74 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "malloc"); 80 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
81 "malloc");
75 GNUNET_assert (0); 82 GNUNET_assert (0);
76 } 83 }
77 return ret; 84 return ret;
@@ -80,8 +87,8 @@ GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
80 87
81/** 88/**
82 * Allocate and initialize memory. Checks the return value, aborts if no more 89 * Allocate and initialize memory. Checks the return value, aborts if no more
83 * memory is available. Don't use GNUNET_xmemdup_ directly. Use the 90 * memory is available. Don't use #GNUNET_xmemdup_() directly. Use the
84 * GNUNET_memdup macro. 91 * GNUNET_memdup() macro.
85 * 92 *
86 * @param buf buffer to initialize from (must contain size bytes) 93 * @param buf buffer to initialize from (must contain size bytes)
87 * @param size number of bytes to allocate 94 * @param size number of bytes to allocate
@@ -90,7 +97,9 @@ GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
90 * @return allocated memory, never NULL 97 * @return allocated memory, never NULL
91 */ 98 */
92void * 99void *
93GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename, 100GNUNET_xmemdup_ (const void *buf,
101 size_t size,
102 const char *filename,
94 int linenumber) 103 int linenumber)
95{ 104{
96 void *ret; 105 void *ret;
@@ -120,18 +129,19 @@ GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
120} 129}
121 130
122 131
123
124/** 132/**
125 * Wrapper around malloc. Allocates size bytes of memory. 133 * Wrapper around malloc(). Allocates size bytes of memory.
126 * The memory will be zero'ed out. 134 * The memory will be zero'ed out.
127 * 135 *
128 * @param size the number of bytes to allocate 136 * @param size the number of bytes to allocate
129 * @param filename where in the code was the call to GNUNET_malloc_large 137 * @param filename where in the code was the call to GNUNET_malloc_unchecked()
130 * @param linenumber where in the code was the call to GNUNET_malloc_large 138 * @param linenumber where in the code was the call to GNUNET_malloc_unchecked()
131 * @return pointer to size bytes of memory, NULL if we do not have enough memory 139 * @return pointer to size bytes of memory, NULL if we do not have enough memory
132 */ 140 */
133void * 141void *
134GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber) 142GNUNET_xmalloc_unchecked_ (size_t size,
143 const char *filename,
144 int linenumber)
135{ 145{
136 void *result; 146 void *result;
137 147
@@ -142,7 +152,7 @@ GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
142#endif 152#endif
143 153
144 result = malloc (size); 154 result = malloc (size);
145 if (result == NULL) 155 if (NULL == result)
146 return NULL; 156 return NULL;
147 memset (result, 0, size); 157 memset (result, 0, size);
148 158
@@ -162,12 +172,15 @@ GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
162 * 172 *
163 * @param ptr the pointer to reallocate 173 * @param ptr the pointer to reallocate
164 * @param n how many bytes of memory to allocate 174 * @param n how many bytes of memory to allocate
165 * @param filename where in the code was the call to GNUNET_realloc 175 * @param filename where in the code was the call to GNUNET_realloc()
166 * @param linenumber where in the code was the call to GNUNET_realloc 176 * @param linenumber where in the code was the call to GNUNET_realloc()
167 * @return pointer to size bytes of memory 177 * @return pointer to size bytes of memory
168 */ 178 */
169void * 179void *
170GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber) 180GNUNET_xrealloc_ (void *ptr,
181 size_t n,
182 const char *filename,
183 int linenumber)
171{ 184{
172#ifdef W32_MEM_LIMIT 185#ifdef W32_MEM_LIMIT
173 n += sizeof (size_t); 186 n += sizeof (size_t);
@@ -215,9 +228,13 @@ GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
215 * @param linenumber 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
216 */ 229 */
217void 230void
218GNUNET_xfree_ (void *ptr, const char *filename, int linenumber) 231GNUNET_xfree_ (void *ptr,
232 const char *filename,
233 int linenumber)
219{ 234{
220 GNUNET_assert_at (ptr != NULL, filename, linenumber); 235 GNUNET_assert_at (NULL != ptr,
236 filename,
237 linenumber);
221#ifdef W32_MEM_LIMIT 238#ifdef W32_MEM_LIMIT
222 ptr = &((size_t *) ptr)[-1]; 239 ptr = &((size_t *) ptr)[-1];
223 mem_used -= *((size_t *) ptr); 240 mem_used -= *((size_t *) ptr);
@@ -239,22 +256,33 @@ GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
239 free (ptr); 256 free (ptr);
240} 257}
241 258
259
242/** 260/**
243 * Dup a string (same semantics as strdup). 261 * Dup a string (same semantics as strdup).
244 * 262 *
245 * @param str the string to dup 263 * @param str the string to dup
246 * @param filename where in the code was the call to GNUNET_strdup 264 * @param filename where in the code was the call to GNUNET_strdup()
247 * @param linenumber where in the code was the call to GNUNET_strdup 265 * @param linenumber where in the code was the call to GNUNET_strdup()
248 * @return strdup(str) 266 * @return `strdup(@a str)`
249 */ 267 */
250char * 268char *
251GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber) 269GNUNET_xstrdup_ (const char *str,
270 const char *filename,
271 int linenumber)
252{ 272{
253 char *res; 273 char *res;
254 274 size_t slen;
255 GNUNET_assert_at (str != NULL, filename, linenumber); 275
256 res = GNUNET_xmalloc_ (strlen (str) + 1, filename, linenumber); 276 GNUNET_assert_at (str != NULL,
257 memcpy (res, str, strlen (str) + 1); 277 filename,
278 linenumber);
279 slen = strlen (str) + 1;
280 res = GNUNET_xmalloc_ (slen,
281 filename,
282 linenumber);
283 memcpy (res,
284 str,
285 slen);
258 return res; 286 return res;
259} 287}
260 288
@@ -279,21 +307,28 @@ strnlen (const char *s,
279 * 307 *
280 * @param str the string to dup 308 * @param str the string to dup
281 * @param len the length of the string to dup 309 * @param len the length of the string to dup
282 * @param filename where in the code was the call to GNUNET_strndup 310 * @param filename where in the code was the call to GNUNET_strndup()
283 * @param linenumber where in the code was the call to GNUNET_strndup 311 * @param linenumber where in the code was the call to GNUNET_strndup()
284 * @return strndup(str,len) 312 * @return `strndup(@a str,@a len)`
285 */ 313 */
286char * 314char *
287GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, 315GNUNET_xstrndup_ (const char *str,
316 size_t len,
317 const char *filename,
288 int linenumber) 318 int linenumber)
289{ 319{
290 char *res; 320 char *res;
291 321
292 if (0 == len) 322 if (0 == len)
293 return GNUNET_strdup (""); 323 return GNUNET_strdup ("");
294 GNUNET_assert_at (str != NULL, filename, linenumber); 324 GNUNET_assert_at (NULL != str,
295 len = strnlen (str, len); 325 filename,
296 res = GNUNET_xmalloc_ (len + 1, filename, linenumber); 326 linenumber);
327 len = strnlen (str,
328 len);
329 res = GNUNET_xmalloc_ (len + 1,
330 filename,
331 linenumber);
297 memcpy (res, str, len); 332 memcpy (res, str, len);
298 /* res[len] = '\0'; 'malloc' zeros out anyway */ 333 /* res[len] = '\0'; 'malloc' zeros out anyway */
299 return res; 334 return res;
@@ -309,12 +344,16 @@ GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
309 * @param elementSize the size of the elements of the array 344 * @param elementSize the size of the elements of the array
310 * @param oldCount address of the number of elements in the *old array 345 * @param oldCount address of the number of elements in the *old array
311 * @param newCount number of elements in the new array, may be 0 346 * @param newCount number of elements in the new array, may be 0
312 * @param filename where in the code was the call to GNUNET_array_grow 347 * @param filename where in the code was the call to GNUNET_array_grow()
313 * @param linenumber where in the code was the call to GNUNET_array_grow 348 * @param linenumber where in the code was the call to GNUNET_array_grow()
314 */ 349 */
315void 350void
316GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount, 351GNUNET_xgrow_ (void **old,
317 unsigned int newCount, const char *filename, int linenumber) 352 size_t elementSize,
353 unsigned int *oldCount,
354 unsigned int newCount,
355 const char *filename,
356 int linenumber)
318{ 357{
319 void *tmp; 358 void *tmp;
320 size_t size; 359 size_t size;
@@ -344,15 +383,17 @@ GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
344 383
345 384
346/** 385/**
347 * Like asprintf, just portable. 386 * Like asprintf(), just portable.
348 * 387 *
349 * @param buf set to a buffer of sufficient size (allocated, caller must free) 388 * @param buf set to a buffer of sufficient size (allocated, caller must free)
350 * @param format format string (see printf, fprintf, etc.) 389 * @param format format string (see printf(), fprintf(), etc.)
351 * @param ... data for format string 390 * @param ... data for format string
352 * @return number of bytes in "*buf" excluding 0-termination 391 * @return number of bytes in `*@a buf`, excluding 0-termination
353 */ 392 */
354int 393int
355GNUNET_asprintf (char **buf, const char *format, ...) 394GNUNET_asprintf (char **buf,
395 const char *format,
396 ...)
356{ 397{
357 int ret; 398 int ret;
358 va_list args; 399 va_list args;
@@ -369,7 +410,7 @@ GNUNET_asprintf (char **buf, const char *format, ...)
369 410
370 411
371/** 412/**
372 * Like snprintf, just aborts if the buffer is of insufficient size. 413 * Like snprintf(), just aborts if the buffer is of insufficient size.
373 * 414 *
374 * @param buf pointer to buffer that is written to 415 * @param buf pointer to buffer that is written to
375 * @param size number of bytes in buf 416 * @param size number of bytes in buf
@@ -378,7 +419,9 @@ GNUNET_asprintf (char **buf, const char *format, ...)
378 * @return number of bytes written to buf or negative value on error 419 * @return number of bytes written to buf or negative value on error
379 */ 420 */
380int 421int
381GNUNET_snprintf (char *buf, size_t size, const char *format, ...) 422GNUNET_snprintf (char *buf,
423 size_t size,
424 const char *format, ...)
382{ 425{
383 int ret; 426 int ret;
384 va_list args; 427 va_list args;