doc
Macros
cynapses libc macro definitions

Macros

#define ARRAY_SIZE(a)   (sizeof(a)/sizeof(a[0]))
 
#define discard_const(ptr)   ((void *)((uintptr_t)(ptr)))
 
#define discard_const_p(type, ptr)   ((type *)discard_const(ptr))
 
#define INT_TO_POINTER(i)   (void *) i
 
#define likely(x)   (x)
 
#define MAX(a, b)   ((a) < (b) ? (b) : (a))
 
#define MIN(a, b)   ((a) < (b) ? (a) : (b))
 
#define POINTER_TO_INT(p)   *((int *) (p))
 
#define SAFE_FREE(x)   do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
 
#define unlikely(x)   (x)
 
#define ZERO_STRUCT(x)   memset((char *)&(x), 0, sizeof(x))
 
#define ZERO_STRUCTP(x)   do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
 

Detailed Description

Macro Definition Documentation

◆ ARRAY_SIZE

#define ARRAY_SIZE (   a)    (sizeof(a)/sizeof(a[0]))

Get the size of an array.

Definition at line 56 of file c_macro.h.

◆ discard_const

#define discard_const (   ptr)    ((void *)((uintptr_t)(ptr)))

This is a hack to fix warnings.

The idea is to use this everywhere that we get the "discarding const" warning by the compiler. That doesn't actually fix the real issue, but marks the place and you can search the code for discard_const.

Please use this macro only when there is no other way to fix the warning. We should use this function in only in a very few places.

Also, please call this via the discard_const_p() macro interface, as that makes the return type safe.

Definition at line 70 of file c_macro.h.

◆ discard_const_p

#define discard_const_p (   type,
  ptr 
)    ((type *)discard_const(ptr))

Type-safe version of discard_const.

Definition at line 75 of file c_macro.h.

◆ INT_TO_POINTER

#define INT_TO_POINTER (   i)    (void *) i

Definition at line 37 of file c_macro.h.

◆ likely

#define likely (   x)    (x)

Definition at line 86 of file c_macro.h.

◆ MAX

#define MAX (   a,
 
)    ((a) < (b) ? (b) : (a))

Get the bigger value.

Definition at line 53 of file c_macro.h.

◆ MIN

#define MIN (   a,
 
)    ((a) < (b) ? (a) : (b))

Get the smaller value.

Definition at line 50 of file c_macro.h.

◆ POINTER_TO_INT

#define POINTER_TO_INT (   p)    *((int *) (p))

Definition at line 38 of file c_macro.h.

◆ SAFE_FREE

#define SAFE_FREE (   x)    do { if ((x) != NULL) {free(x); x=NULL;} } while(0)

Free memory and zero the pointer.

Definition at line 47 of file c_macro.h.

◆ unlikely

#define unlikely (   x)    (x)

Definition at line 89 of file c_macro.h.

◆ ZERO_STRUCT

#define ZERO_STRUCT (   x)    memset((char *)&(x), 0, sizeof(x))

Zero a structure.

Definition at line 41 of file c_macro.h.

◆ ZERO_STRUCTP

#define ZERO_STRUCTP (   x)    do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)

Zero a structure given a pointer to the structure.

Definition at line 44 of file c_macro.h.