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

◆ BAADFOOD_STR [2/2]

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

Definition at line 285 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 66 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  ret = GNUNET_xmalloc_unchecked_(size, filename, linenumber);
74  if (NULL == ret)
75  {
77  GNUNET_assert(0);
78  }
79  return ret;
80 }
#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:66
#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 98 of file common_allocation.c.

References GNUNET_xmalloc_(), and ret.

103 {
104  /* use char pointer internally to avoid void pointer arithmetic warnings */
105  char **ret = GNUNET_xmalloc_(n * sizeof(void *) + /* 1. dim header */
106  n * m * elementSize, /* element data */
107  filename,
108  linenumber);
109 
110  for (size_t i = 0; i < n; i++)
111  ret[i] = (char *)ret + /* base address */
112  n * sizeof(void *) + /* skip 1. dim header */
113  i * m * elementSize; /* skip to 2. dim row header */
114  return (void **)ret;
115 }
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 134 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,
146  linenumber);
147 
148  for (size_t i = 0; i < n; i++)
149  {
150  /* need to cast to (char *) temporarily for byte level acuracy */
151  ret[i] = (char **)((char *)ret + /* base address */
152  n * sizeof(void **) + /* skip 1. dim header */
153  i * m * sizeof(void *)); /* skip to 2. dim header */
154  for (size_t j = 0; j < m; j++)
155  ret[i][j] = (char *)ret + /* base address */
156  n * sizeof(void **) + /* skip 1. dim header */
157  n * m * sizeof(void *) + /* skip 2. dim header */
158  i * m * o * elementSize + /* skip to 2. dim part */
159  j * o * elementSize; /* skip to 3. dim row data */
160  }
161  return (void ***)ret;
162 }
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 177 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.

181 {
182  void *ret;
183 
184  /* As a security precaution, we generally do not allow very large
185  * allocations here */
187 #ifdef W32_MEM_LIMIT
188  size += sizeof(size_t);
189  if (mem_used + size > W32_MEM_LIMIT)
190  return NULL;
191 #endif
192  GNUNET_assert_at(size < INT_MAX, filename, linenumber);
193  ret = malloc(size);
194  if (ret == NULL)
195  {
197  GNUNET_assert(0);
198  }
199 #ifdef W32_MEM_LIMIT
200  *((size_t *)ret) = size;
201  ret = &((size_t *)ret)[1];
202  mem_used += size;
203 #endif
204  GNUNET_memcpy(ret, buf, size);
205  return ret;
206 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define INT_MAX
static char buf[2048]
static char * filename
#define LOG_STRERROR(kind, syscall)
static unsigned int size
Size of the "table".
Definition: peer.c:66
#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 219 of file common_allocation.c.

References result, and size.

Referenced by GNUNET_xmalloc_().

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

References GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, and LOG_STRERROR.

259 {
260  (void)filename;
261  (void)linenumber;
262 
263 #ifdef W32_MEM_LIMIT
264  n += sizeof(size_t);
265  ptr = &((size_t *)ptr)[-1];
266  mem_used = mem_used - *((size_t *)ptr) + n;
267 #endif
268  ptr = realloc(ptr, n);
269  if ((NULL == ptr) && (n > 0))
270  {
272  GNUNET_assert(0);
273  }
274 #ifdef W32_MEM_LIMIT
275  ptr = &((size_t *)ptr)[1];
276 #endif
277  return ptr;
278 }
#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 306 of file common_allocation.c.

References GNUNET_assert_at, GNUNET_memcpy, and GNUNET_ntohll().

Referenced by GNUNET_xgrow_().

307 {
308  GNUNET_assert_at(NULL != ptr, filename, linenumber);
309 #ifdef W32_MEM_LIMIT
310  ptr = &((size_t *)ptr)[-1];
311  mem_used -= *((size_t *)ptr);
312 #endif
313 #if defined(M_SIZE)
314 #if ENABLE_POISONING
315  {
316  const uint64_t baadfood = GNUNET_ntohll(0xBAADF00DBAADF00DLL);
317  uint64_t *base = ptr;
318  size_t s = M_SIZE(ptr);
319  size_t i;
320 
321  for (i = 0; i < s / 8; i++)
322  base[i] = baadfood;
323  GNUNET_memcpy(&base[s / 8], &baadfood, s % 8);
324  }
325 #endif
326 #endif
327  free(ptr);
328 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
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 340 of file common_allocation.c.

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

341 {
342  char *res;
343  size_t slen;
344 
345  GNUNET_assert_at(str != NULL, filename, linenumber);
346  slen = strlen(str) + 1;
347  res = GNUNET_xmalloc_(slen, filename, linenumber);
348  GNUNET_memcpy(res, str, slen);
349  return res;
350 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
void * GNUNET_xmalloc_(size_t size, const char *filename, int linenumber)
Allocate memory.
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 355 of file common_allocation.c.

References e.

Referenced by GNUNET_strlcpy(), and GNUNET_xstrndup_().

356 {
357  const char *e;
358 
359  e = memchr(s, '\0', n);
360  if (NULL == e)
361  return n;
362  return e - s;
363 }
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 377 of file common_allocation.c.

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

381 {
382  char *res;
383 
384  if (0 == len)
385  return GNUNET_strdup("");
386  GNUNET_assert_at(NULL != str, filename, linenumber);
387  len = strnlen(str, len);
388  res = GNUNET_xmalloc_(len + 1, filename, linenumber);
389  GNUNET_memcpy(res, str, len);
390  /* res[len] = '\0'; 'malloc' zeros out anyway */
391  return res;
392 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
void * GNUNET_xmalloc_(size_t size, const char *filename, int linenumber)
Allocate memory.
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 408 of file common_allocation.c.

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

414 {
415  void *tmp;
416  size_t size;
417 
418  GNUNET_assert_at(INT_MAX / elementSize > newCount, filename, linenumber);
419  size = newCount * elementSize;
420  if (0 == size)
421  {
422  tmp = NULL;
423  }
424  else
425  {
426  tmp = GNUNET_xmalloc_(size, filename, linenumber);
427  if (NULL != *old)
428  {
429  GNUNET_memcpy(tmp, *old, elementSize * GNUNET_MIN(*oldCount, newCount));
430  }
431  }
432 
433  if (NULL != *old)
434  {
435  GNUNET_xfree_(*old, filename, linenumber);
436  }
437  *old = tmp;
438  *oldCount = newCount;
439 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
void * GNUNET_xmalloc_(size_t size, const char *filename, int linenumber)
Allocate memory.
#define INT_MAX
#define GNUNET_MIN(a, b)
Definition: gnunet_common.h:80
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:66
#define GNUNET_assert_at(cond, f, l)
Use this for fatal errors that cannot be handled.
Here is the call graph for this function: