GNUnet  0.11.x
Macros | Functions
common_allocation.c File Reference

wrapper around malloc/free More...

#include "platform.h"
#include "gnunet_crypto_lib.h"
Include dependency graph for common_allocation.c:

Go to the source code of this file.

Macros

#define LOG(kind, ...)    GNUNET_log_from (kind, "util-common-allocation", __VA_ARGS__)
 
#define LOG_STRERROR(kind, syscall)    GNUNET_log_from_strerror (kind, "util-common-allocation", syscall)
 
#define INT_MAX   0x7FFFFFFF
 
#define BAADFOOD_STR   "\x0D\xF0\xAD\xBA"
 
#define BAADFOOD_STR   "\xBA\xAD\xF0\x0D"
 

Functions

void * GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
 Allocate memory. More...
 
void ** GNUNET_xnew_array_2d_ (size_t n, size_t m, size_t elementSize, const char *filename, int linenumber)
 Allocate memory for a two dimensional array in one block and set up pointers. More...
 
void *** GNUNET_xnew_array_3d_ (size_t n, size_t m, size_t o, size_t elementSize, const char *filename, int linenumber)
 Allocate memory for a three dimensional array in one block and set up pointers. More...
 
void * GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename, int linenumber)
 Allocate and initialize memory. More...
 
void * GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
 Wrapper around malloc(). More...
 
void * GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
 Reallocate memory. More...
 
void GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
 Free memory. More...
 
char * GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
 Dup a string (same semantics as strdup). More...
 
static size_t strnlen (const char *s, size_t n)
 
char * GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, int linenumber)
 Dup partially a string (same semantics as strndup). More...
 
void GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount, unsigned int newCount, const char *filename, int linenumber)
 Grow an array. More...
 
int GNUNET_asprintf (char **buf, const char *format,...)
 Like asprintf(), just portable. More...
 
int GNUNET_snprintf (char *buf, size_t size, const char *format,...)
 Like snprintf(), just aborts if the buffer is of insufficient size. More...
 
struct GNUNET_MessageHeaderGNUNET_copy_message (const struct GNUNET_MessageHeader *msg)
 Create a copy of the given message. More...
 
enum GNUNET_GenericReturnValue GNUNET_is_zero_ (const void *a, size_t n)
 Check that memory in a is all zeros. More...
 

Detailed Description

wrapper around malloc/free

Author
Christian Grothoff

Definition in file common_allocation.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)     GNUNET_log_from (kind, "util-common-allocation", __VA_ARGS__)

Definition at line 35 of file common_allocation.c.

◆ LOG_STRERROR

#define LOG_STRERROR (   kind,
  syscall 
)     GNUNET_log_from_strerror (kind, "util-common-allocation", syscall)

Definition at line 38 of file common_allocation.c.

◆ INT_MAX

#define INT_MAX   0x7FFFFFFF

Definition at line 42 of file common_allocation.c.

◆ BAADFOOD_STR [1/2]

#define BAADFOOD_STR   "\x0D\xF0\xAD\xBA"

Definition at line 273 of file common_allocation.c.

◆ BAADFOOD_STR [2/2]

#define BAADFOOD_STR   "\xBA\xAD\xF0\x0D"

Definition at line 273 of file common_allocation.c.

Function Documentation

◆ GNUNET_xmalloc_()

void* GNUNET_xmalloc_ ( size_t  size,
const char *  filename,
int  linenumber 
)

Allocate memory.

Checks the return value, aborts if no more memory is available.

Parameters
sizehow many bytes of memory to allocate, do NOT use this function (or GNUNET_malloc()) to allocate more than several MB of memory, if you are possibly needing a very large chunk use GNUNET_xmalloc_unchecked_() instead.
filenamewhere in the code was the call to GNUNET_malloc()
linenumberwhere in the code was the call to GNUNET_malloc()
Returns
pointer to size bytes of memory

Definition at line 59 of file common_allocation.c.

60 {
61  void *ret;
62 
63  /* As a security precaution, we generally do not allow very large
64  * allocations using the default 'GNUNET_malloc()' macro */
66  ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber);
67  if (NULL == ret)
68  {
70  GNUNET_assert (0);
71  }
72  return ret;
73 }
#define LOG_STRERROR(kind, syscall)
void * GNUNET_xmalloc_unchecked_(size_t size, const char *filename, int linenumber)
Wrapper around malloc().
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static char * filename
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_assert_at(cond, f, l)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_MAX_MALLOC_CHECKED
Maximum allocation with GNUNET_malloc macro.
static unsigned int size
Size of the "table".
Definition: peer.c:67

References filename, GNUNET_assert, GNUNET_assert_at, GNUNET_ERROR_TYPE_ERROR, GNUNET_MAX_MALLOC_CHECKED, GNUNET_xmalloc_unchecked_(), LOG_STRERROR, ret, and size.

Referenced by GNUNET_xgrow_(), GNUNET_xnew_array_2d_(), GNUNET_xnew_array_3d_(), GNUNET_xstrdup_(), and GNUNET_xstrndup_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_xnew_array_2d_()

void** GNUNET_xnew_array_2d_ ( size_t  n,
size_t  m,
size_t  elementSize,
const char *  filename,
int  linenumber 
)

Allocate memory for a two dimensional array in one block and set up pointers.

Aborts if no more memory is available. Don't use GNUNET_xnew_array_2d_ directly. Use the GNUNET_new_array_2d macro. The memory of the elements will be zero'ed out.

Parameters
nsize of the first dimension
msize of the second dimension
elementSizesize of a single element in bytes
filenamewhere is this call being made (for debugging)
linenumberline where this call is being made (for debugging)
Returns
allocated memory, never NULL

Definition at line 91 of file common_allocation.c.

96 {
97  /* use char pointer internally to avoid void pointer arithmetic warnings */
98  char **ret = GNUNET_xmalloc_ (n * sizeof(void *) /* 1. dim header */
99  + n * m * elementSize, /* element data */
100  filename,
101  linenumber);
102 
103  for (size_t i = 0; i < n; i++)
104  ret[i] = (char *) ret /* base address */
105  + n * sizeof(void *) /* skip 1. dim header */
106  + i * m * elementSize; /* skip to 2. dim row header */
107  return (void **) ret;
108 }
void * GNUNET_xmalloc_(size_t size, const char *filename, int linenumber)
Allocate memory.
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition: gnunet-arm.c:104

References filename, GNUNET_xmalloc_(), m, and ret.

Here is the call graph for this function:

◆ GNUNET_xnew_array_3d_()

void*** GNUNET_xnew_array_3d_ ( size_t  n,
size_t  m,
size_t  o,
size_t  elementSize,
const char *  filename,
int  linenumber 
)

Allocate memory for a three dimensional array in one block and set up pointers.

Aborts if no more memory is available. Don't use GNUNET_xnew_array_3d_ directly. Use the GNUNET_new_array_3d macro. The memory of the elements will be zero'ed out.

Parameters
nsize of the first dimension
msize of the second dimension
osize of the third dimension
elementSizesize of a single element in bytes
filenamewhere is this call being made (for debugging)
linenumberline where this call is being made (for debugging)
Returns
allocated memory, never NULL

Definition at line 127 of file common_allocation.c.

133 {
134  /* use char pointer internally to avoid void pointer arithmetic warnings */
135  char ***ret = GNUNET_xmalloc_ (n * sizeof(void **) /* 1. dim header */
136  + n * m * sizeof(void *) /* 2. dim header */
137  + n * m * o * elementSize, /* element data */
138  filename,
139  linenumber);
140 
141  for (size_t i = 0; i < n; i++)
142  {
143  /* need to cast to (char *) temporarily for byte level accuracy */
144  ret[i] = (char **) ((char *) ret /* base address */
145  + n * sizeof(void **) /* skip 1. dim header */
146  + i * m * sizeof(void *)); /* skip to 2. dim header */
147  for (size_t j = 0; j < m; j++)
148  ret[i][j] = (char *) ret /* base address */
149  + n * sizeof(void **) /* skip 1. dim header */
150  + n * m * sizeof(void *) /* skip 2. dim header */
151  + i * m * o * elementSize /* skip to 2. dim part */
152  + j * o * elementSize; /* skip to 3. dim row data */
153  }
154  return (void ***) ret;
155 }

References filename, GNUNET_xmalloc_(), m, and ret.

Here is the call graph for this function:

◆ GNUNET_xmemdup_()

void* GNUNET_xmemdup_ ( const void *  buf,
size_t  size,
const char *  filename,
int  linenumber 
)

Allocate and initialize memory.

Checks the return value, aborts if no more memory is available. Don't use GNUNET_xmemdup_() directly. Use the GNUNET_memdup() macro.

Parameters
bufbuffer to initialize from (must contain size bytes)
sizenumber of bytes to allocate
filenamewhere is this call being made (for debugging)
linenumberline where this call is being made (for debugging)
Returns
allocated memory, never NULL

Definition at line 170 of file common_allocation.c.

174 {
175  void *ret;
176 
177  /* As a security precaution, we generally do not allow very large
178  * allocations here */
180  GNUNET_assert_at (size < INT_MAX, filename, linenumber);
181  ret = malloc (size);
182  if (ret == NULL)
183  {
185  GNUNET_assert (0);
186  }
188  return ret;
189 }
#define INT_MAX
static char buf[2048]
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.

References buf, filename, GNUNET_assert, GNUNET_assert_at, GNUNET_ERROR_TYPE_ERROR, GNUNET_MAX_MALLOC_CHECKED, GNUNET_memcpy, INT_MAX, LOG_STRERROR, ret, and size.

◆ GNUNET_xmalloc_unchecked_()

void* GNUNET_xmalloc_unchecked_ ( size_t  size,
const char *  filename,
int  linenumber 
)

Wrapper around malloc().

Allocate memory.

Allocates size bytes of memory. The memory will be zero'ed out.

Parameters
sizethe number of bytes to allocate
filenamewhere in the code was the call to GNUNET_malloc_unchecked()
linenumberwhere in the code was the call to GNUNET_malloc_unchecked()
Returns
pointer to size bytes of memory, NULL if we do not have enough memory

Definition at line 202 of file common_allocation.c.

203 {
204  void *result;
205 
206  (void) filename;
207  (void) linenumber;
208 
209  result = malloc (size);
210  if (NULL == result)
211  return NULL;
212  memset (result, 0, size);
213 
214  return result;
215 }
static int result
Global testing status.

References filename, result, and size.

Referenced by GNUNET_xmalloc_().

Here is the caller graph for this function:

◆ GNUNET_xrealloc_()

void* GNUNET_xrealloc_ ( void *  ptr,
size_t  n,
const char *  filename,
int  linenumber 
)

Reallocate memory.

Checks the return value, aborts if no more memory is available. The content of the intersection of the new and old size will be unchanged.

Parameters
ptrthe pointer to reallocate
nhow many bytes of memory to allocate
filenamewhere in the code was the call to GNUNET_realloc()
linenumberwhere in the code was the call to GNUNET_realloc()
Returns
pointer to size bytes of memory

Definition at line 230 of file common_allocation.c.

231 {
232  (void) filename;
233  (void) linenumber;
234 
235 #if defined(M_SIZE)
236 #if ENABLE_POISONING
237  {
238  uint64_t *base = ptr;
239  size_t s = M_SIZE (ptr);
240 
241  if (s > n)
242  {
243  const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
244  char *cbase = ptr;
245 
246  GNUNET_memcpy (&cbase[n],
247  &baadfood,
248  GNUNET_MIN (8 - (n % 8),
249  s - n));
250  for (size_t i = 1 + (n + 7) / 8; i < s / 8; i++)
251  base[i] = baadfood;
252  GNUNET_memcpy (&base[s / 8],
253  &baadfood,
254  s % 8);
255  }
256  }
257 #endif
258 #endif
259  ptr = realloc (ptr, n);
260  if ((NULL == ptr) && (n > 0))
261  {
263  GNUNET_assert (0);
264  }
265  return ptr;
266 }
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:53
#define GNUNET_MIN(a, b)

References filename, GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_memcpy, GNUNET_MIN, GNUNET_ntohll(), and LOG_STRERROR.

Here is the call graph for this function:

◆ GNUNET_xfree_()

void GNUNET_xfree_ ( void *  ptr,
const char *  filename,
int  linenumber 
)

Free memory.

Merely a wrapper for the case that we want to keep track of allocations.

Parameters
ptrthe pointer to free
filenamewhere in the code was the call to GNUNET_free()
linenumberwhere in the code was the call to GNUNET_free()

Definition at line 294 of file common_allocation.c.

297 {
298  if (NULL == ptr)
299  return;
300 #if defined(M_SIZE)
301 #if ENABLE_POISONING
302  {
303  const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
304  uint64_t *base = ptr;
305  size_t s = M_SIZE (ptr);
306 
307  for (size_t i = 0; i < s / 8; i++)
308  base[i] = baadfood;
309  GNUNET_memcpy (&base[s / 8], &baadfood, s % 8);
310  }
311 #endif
312 #endif
313  free (ptr);
314 }

References GNUNET_memcpy, and GNUNET_ntohll().

Referenced by GNUNET_xgrow_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_xstrdup_()

char* GNUNET_xstrdup_ ( const char *  str,
const char *  filename,
int  linenumber 
)

Dup a string (same semantics as strdup).

Dup a string.

Parameters
strthe string to dup
filenamewhere in the code was the call to GNUNET_strdup()
linenumberwhere in the code was the call to GNUNET_strdup()
Returns
strdup(@a str)

Definition at line 326 of file common_allocation.c.

327 {
328  char *res;
329  size_t slen;
330 
331  GNUNET_assert_at (str != NULL, filename, linenumber);
332  slen = strlen (str) + 1;
333  res = GNUNET_xmalloc_ (slen, filename, linenumber);
334  GNUNET_memcpy (res, str, slen);
335  return res;
336 }
static int res

References filename, GNUNET_assert_at, GNUNET_memcpy, GNUNET_xmalloc_(), and res.

Here is the call graph for this function:

◆ strnlen()

static size_t strnlen ( const char *  s,
size_t  n 
)
static

Definition at line 341 of file common_allocation.c.

342 {
343  const char *e;
344 
345  e = memchr (s, '\0', n);
346  if (NULL == e)
347  return n;
348  return e - s;
349 }
static struct Experiment * e

References e.

Referenced by GNUNET_strlcpy(), and GNUNET_xstrndup_().

Here is the caller graph for this function:

◆ GNUNET_xstrndup_()

char* GNUNET_xstrndup_ ( const char *  str,
size_t  len,
const char *  filename,
int  linenumber 
)

Dup partially a string (same semantics as strndup).

Dup partially a string.

Parameters
strthe string to dup
lenthe length of the string to dup
filenamewhere in the code was the call to GNUNET_strndup()
linenumberwhere in the code was the call to GNUNET_strndup()
Returns
strndup(@a str,@a len)

Definition at line 365 of file common_allocation.c.

369 {
370  char *res;
371 
372  if (0 == len)
373  return GNUNET_strdup ("");
374  GNUNET_assert_at (NULL != str, filename, linenumber);
375  len = strnlen (str, len);
376  res = GNUNET_xmalloc_ (len + 1, filename, linenumber);
377  GNUNET_memcpy (res, str, len);
378  /* res[len] = '\0'; 'malloc' zeros out anyway */
379  return res;
380 }
static size_t strnlen(const char *s, size_t n)
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.

References filename, GNUNET_assert_at, GNUNET_memcpy, GNUNET_strdup, GNUNET_xmalloc_(), len, res, and strnlen().

Here is the call graph for this function:

◆ GNUNET_xgrow_()

void GNUNET_xgrow_ ( void **  old,
size_t  elementSize,
unsigned int *  oldCount,
unsigned int  newCount,
const char *  filename,
int  linenumber 
)

Grow an array.

Grow an array, the new elements are zeroed out.

Grows old by (*oldCount-newCount)*elementSize bytes and sets *oldCount to newCount.

Parameters
oldaddress of the pointer to the array *old may be NULL
elementSizethe size of the elements of the array
oldCountaddress of the number of elements in the *old array
newCountnumber of elements in the new array, may be 0
filenamewhere in the code was the call to GNUNET_array_grow()
linenumberwhere in the code was the call to GNUNET_array_grow()

Definition at line 396 of file common_allocation.c.

402 {
403  void *tmp;
404  size_t size;
405 
406  GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
407  size = newCount * elementSize;
408  if (0 == size)
409  {
410  tmp = NULL;
411  }
412  else
413  {
414  tmp = GNUNET_xmalloc_ (size, filename, linenumber);
415  if (NULL != *old)
416  {
417  GNUNET_memcpy (tmp, *old, elementSize * GNUNET_MIN (*oldCount, newCount));
418  }
419  }
420 
421  if (NULL != *old)
422  {
423  GNUNET_xfree_ (*old, filename, linenumber);
424  }
425  *old = tmp;
426  *oldCount = newCount;
427 }
void GNUNET_xfree_(void *ptr, const char *filename, int linenumber)
Free memory.

References filename, GNUNET_assert_at, GNUNET_memcpy, GNUNET_MIN, GNUNET_xfree_(), GNUNET_xmalloc_(), INT_MAX, and size.

Here is the call graph for this function:

◆ GNUNET_asprintf()

int GNUNET_asprintf ( char **  buf,
const char *  format,
  ... 
)

Like asprintf(), just portable.

Parameters
bufset to a buffer of sufficient size (allocated, caller must free)
formatformat string (see printf(), fprintf(), etc.)
...data for format string
Returns
number of bytes in *@a buf, excluding 0-termination

Definition at line 439 of file common_allocation.c.

440 {
441  int ret;
442  va_list args;
443 
444  va_start (args, format);
445  ret = vsnprintf (NULL, 0, format, args);
446  va_end (args);
447  GNUNET_assert (ret >= 0);
448  *buf = GNUNET_malloc (ret + 1);
449  va_start (args, format);
450  ret = vsprintf (*buf, format, args);
451  va_end (args);
452  return ret;
453 }
#define GNUNET_malloc(size)
Wrapper around malloc.

References consensus-simulation::args, buf, GNUNET_assert, GNUNET_malloc, and ret.

◆ GNUNET_snprintf()

int GNUNET_snprintf ( char *  buf,
size_t  size,
const char *  format,
  ... 
)

Like snprintf(), just aborts if the buffer is of insufficient size.

Parameters
bufpointer to buffer that is written to
sizenumber of bytes in buf
formatformat strings
...data for format string
Returns
number of bytes written to buf or negative value on error

Definition at line 466 of file common_allocation.c.

467 {
468  int ret;
469  va_list args;
470 
471  va_start (args, format);
472  ret = vsnprintf (buf, size, format, args);
473  va_end (args);
474  GNUNET_assert ((ret >= 0) && (((size_t) ret) < size));
475  return ret;
476 }

References consensus-simulation::args, buf, GNUNET_assert, ret, and size.

◆ GNUNET_is_zero_()

enum GNUNET_GenericReturnValue GNUNET_is_zero_ ( const void *  a,
size_t  n 
)

Check that memory in a is all zeros.

a must be a pointer.

Parameters
apointer to n bytes which should be tested for the entire memory being zero'ed out.
nnumber of bytes in to be tested
Returns
GNUNET_YES if a is zero, GNUNET_NO otherwise

Definition at line 486 of file common_allocation.c.

510 {
511  const char *b = a;
512 
513  for (size_t i = 0; i < n; i++)
514  if (b[i])
515  return GNUNET_NO;
516  return GNUNET_YES;
517 }
@ GNUNET_YES
Definition: gnunet_common.h:97
@ GNUNET_NO
Definition: gnunet_common.h:94