GNUnet  0.10.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...
 

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 37 of file common_allocation.c.

Referenced by GNUNET_xmalloc_(), GNUNET_xmemdup_(), and GNUNET_xrealloc_().

◆ INT_MAX

#define INT_MAX   0x7FFFFFFF

◆ BAADFOOD_STR [1/2]

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

Definition at line 290 of file common_allocation.c.

◆ BAADFOOD_STR [2/2]

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

Definition at line 290 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 64 of file common_allocation.c.

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

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

67 {
68  void *ret;
69 
70  /* As a security precaution, we generally do not allow very large
71  * allocations using the default 'GNUNET_malloc()' macro */
73  filename,
74  linenumber);
76  filename,
77  linenumber);
78  if (NULL == ret)
79  {
81  "malloc");
82  GNUNET_assert (0);
83  }
84  return ret;
85 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int ret
Final status code.
Definition: gnunet-arm.c:89
static char * filename
#define LOG_STRERROR(kind, syscall)
void * GNUNET_xmalloc_unchecked_(size_t size, const char *filename, int linenumber)
Wrapper around malloc().
static unsigned int size
Size of the "table".
Definition: peer.c:67
#define GNUNET_MAX_MALLOC_CHECKED
Maximum allocation with GNUNET_malloc macro.
#define GNUNET_assert_at(cond, f, l)
Use this for fatal errors that cannot be handled.
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 103 of file common_allocation.c.

References GNUNET_xmalloc_(), and ret.

108 {
109  /* use char pointer internally to avoid void pointer arithmetic warnings */
110  char **ret = GNUNET_xmalloc_ (n * sizeof (void *) + /* 1. dim header */
111  n * m * elementSize, /* element data */
112  filename, linenumber);
113 
114  for (size_t i = 0; i < n; i++)
115  ret[i] = (char *)ret + /* base address */
116  n * sizeof (void *) + /* skip 1. dim header */
117  i * m * elementSize; /* skip to 2. dim row header */
118  return (void **)ret;
119 }
static int ret
Final status code.
Definition: gnunet-arm.c:89
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:99
static char * filename
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 138 of file common_allocation.c.

References GNUNET_xmalloc_(), m, and ret.

140 {
141  /* use char pointer internally to avoid void pointer arithmetic warnings */
142  char ***ret = GNUNET_xmalloc_ (n * sizeof (void **) + /* 1. dim header */
143  n * m * sizeof (void *) + /* 2. dim header */
144  n * m * o * elementSize, /* element data */
145  filename, linenumber);
146 
147  for (size_t i = 0; i < n; i++)
148  {
149  /* need to cast to (char *) temporarily for byte level acuracy */
150  ret[i] = (char **)((char *)ret + /* base address */
151  n * sizeof (void **) + /* skip 1. dim header */
152  i * m * sizeof (void *)); /* skip to 2. dim header */
153  for (size_t j = 0; j < m; j++)
154  ret[i][j] = (char *)ret + /* base address */
155  n * sizeof (void **) + /* skip 1. dim header */
156  n * m * sizeof (void *) + /* skip 2. dim header */
157  i * m * o * elementSize + /* skip to 2. dim part */
158  j * o * elementSize; /* skip to 3. dim row data */
159  }
160  return (void ***)ret;
161 }
static int ret
Final status code.
Definition: gnunet-arm.c:89
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:99
static char * filename
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 176 of file common_allocation.c.

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

180 {
181  void *ret;
182 
183  /* As a security precaution, we generally do not allow very large
184  * allocations here */
186 #ifdef W32_MEM_LIMIT
187  size += sizeof (size_t);
188  if (mem_used + size > W32_MEM_LIMIT)
189  return NULL;
190 #endif
191  GNUNET_assert_at (size < INT_MAX, filename, linenumber);
192  ret = malloc (size);
193  if (ret == NULL)
194  {
196  GNUNET_assert (0);
197  }
198 #ifdef W32_MEM_LIMIT
199  *((size_t *) ret) = size;
200  ret = &((size_t *) ret)[1];
201  mem_used += size;
202 #endif
203  GNUNET_memcpy (ret, buf, size);
204  return ret;
205 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define INT_MAX
#define GNUNET_memcpy(dst, src, n)
static char buf[2048]
static char * filename
#define LOG_STRERROR(kind, syscall)
static unsigned int size
Size of the "table".
Definition: peer.c:67
#define GNUNET_MAX_MALLOC_CHECKED
Maximum allocation with GNUNET_malloc macro.
#define GNUNET_assert_at(cond, f, l)
Use this for fatal errors that cannot be handled.

◆ 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 218 of file common_allocation.c.

References result, and size.

Referenced by GNUNET_xmalloc_().

221 {
222  void *result;
223 
224  (void) filename;
225  (void) linenumber;
226 #ifdef W32_MEM_LIMIT
227  size += sizeof (size_t);
228  if (mem_used + size > W32_MEM_LIMIT)
229  return NULL;
230 #endif
231 
232  result = malloc (size);
233  if (NULL == result)
234  return NULL;
235  memset (result, 0, size);
236 
237 #ifdef W32_MEM_LIMIT
238  *((size_t *) result) = size;
239  result = &((size_t *) result)[1];
240  mem_used += size;
241 #endif
242 
243  return result;
244 }
static char * filename
static int result
Global testing status.
static unsigned int size
Size of the "table".
Definition: peer.c:67
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 259 of file common_allocation.c.

References GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, and LOG_STRERROR.

263 {
264  (void) filename;
265  (void) linenumber;
266 
267 #ifdef W32_MEM_LIMIT
268  n += sizeof (size_t);
269  ptr = &((size_t *) ptr)[-1];
270  mem_used = mem_used - *((size_t *) ptr) + n;
271 #endif
272  ptr = realloc (ptr, n);
273  if ((NULL == ptr) && (n > 0))
274  {
276  "realloc");
277  GNUNET_assert (0);
278  }
279 #ifdef W32_MEM_LIMIT
280  ptr = &((size_t *) ptr)[1];
281 #endif
282  return ptr;
283 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static char * filename
#define LOG_STRERROR(kind, syscall)

◆ 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 314 of file common_allocation.c.

References GNUNET_assert_at, GNUNET_memcpy, and GNUNET_ntohll().

Referenced by GNUNET_xgrow_().

317 {
318  GNUNET_assert_at (NULL != ptr,
319  filename,
320  linenumber);
321 #ifdef W32_MEM_LIMIT
322  ptr = &((size_t *) ptr)[-1];
323  mem_used -= *((size_t *) ptr);
324 #endif
325 #if defined(M_SIZE)
326 #if ENABLE_POISONING
327  {
328  const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
329  uint64_t *base = ptr;
330  size_t s = M_SIZE (ptr);
331  size_t i;
332 
333  for (i=0;i<s/8;i++)
334  base[i] = baadfood;
335  GNUNET_memcpy (&base[s/8], &baadfood, s % 8);
336  }
337 #endif
338 #endif
339  free (ptr);
340 }
#define GNUNET_memcpy(dst, src, n)
static char * filename
#define GNUNET_assert_at(cond, f, l)
Use this for fatal errors that cannot be handled.
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:48
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 352 of file common_allocation.c.

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

355 {
356  char *res;
357  size_t slen;
358 
359  GNUNET_assert_at (str != NULL,
360  filename,
361  linenumber);
362  slen = strlen (str) + 1;
363  res = GNUNET_xmalloc_ (slen,
364  filename,
365  linenumber);
366  GNUNET_memcpy (res,
367  str,
368  slen);
369  return res;
370 }
void * GNUNET_xmalloc_(size_t size, const char *filename, int linenumber)
Allocate memory.
#define GNUNET_memcpy(dst, src, n)
static char * filename
static int res
#define GNUNET_assert_at(cond, f, l)
Use this for fatal errors that cannot be handled.
Here is the call graph for this function:

◆ strnlen()

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

Definition at line 375 of file common_allocation.c.

References e.

Referenced by GNUNET_strlcpy(), and GNUNET_xstrndup_().

377 {
378  const char *e;
379 
380  e = memchr (s, '\0', n);
381  if (NULL == e)
382  return n;
383  return e - s;
384 }
static struct Experiment * e
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 398 of file common_allocation.c.

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

402 {
403  char *res;
404 
405  if (0 == len)
406  return GNUNET_strdup ("");
407  GNUNET_assert_at (NULL != str,
408  filename,
409  linenumber);
410  len = strnlen (str,
411  len);
412  res = GNUNET_xmalloc_ (len + 1,
413  filename,
414  linenumber);
415  GNUNET_memcpy (res, str, len);
416  /* res[len] = '\0'; 'malloc' zeros out anyway */
417  return res;
418 }
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
void * GNUNET_xmalloc_(size_t size, const char *filename, int linenumber)
Allocate memory.
#define GNUNET_memcpy(dst, src, n)
static char * filename
static int res
#define GNUNET_assert_at(cond, f, l)
Use this for fatal errors that cannot be handled.
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...
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 434 of file common_allocation.c.

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

440 {
441  void *tmp;
442  size_t size;
443 
444  GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
445  size = newCount * elementSize;
446  if (0 == size)
447  {
448  tmp = NULL;
449  }
450  else
451  {
452  tmp = GNUNET_xmalloc_ (size, filename, linenumber);
453  if (NULL != *old)
454  {
455  GNUNET_memcpy (tmp, *old, elementSize * GNUNET_MIN(*oldCount, newCount));
456  }
457  }
458 
459  if (NULL != *old)
460  {
461  GNUNET_xfree_ (*old, filename, linenumber);
462  }
463  *old = tmp;
464  *oldCount = newCount;
465 }
void * GNUNET_xmalloc_(size_t size, const char *filename, int linenumber)
Allocate memory.
#define INT_MAX
#define GNUNET_memcpy(dst, src, n)
#define GNUNET_MIN(a, b)
Definition: gnunet_common.h:83
void GNUNET_xfree_(void *ptr, const char *filename, int linenumber)
Free memory.
static char * filename
static unsigned int size
Size of the "table".
Definition: peer.c:67
#define GNUNET_assert_at(cond, f, l)
Use this for fatal errors that cannot be handled.
Here is the call graph for this function: