doc
c_macro.h
Go to the documentation of this file.
1/*
2 * cynapses libc functions
3 *
4 * Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22 * @file c_macro.h
23 *
24 * @brief cynapses libc macro definitions
25 *
26 * @defgroup cynMacroInternals cynapses libc macro definitions
27 * @ingroup cynLibraryAPI
28 *
29 * @{
30 */
31#ifndef _C_MACRO_H
32#define _C_MACRO_H
33
34#include <stdint.h>
35#include <string.h>
36
37#define INT_TO_POINTER(i) (void *) i
38#define POINTER_TO_INT(p) *((int *) (p))
39
40/** Zero a structure */
41#define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
42
43/** Zero a structure given a pointer to the structure */
44#define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
45
46/** Free memory and zero the pointer */
47#define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
48
49/** Get the smaller value */
50#define MIN(a,b) ((a) < (b) ? (a) : (b))
51
52/** Get the bigger value */
53#define MAX(a,b) ((a) < (b) ? (b) : (a))
54
55/** Get the size of an array */
56#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
57
58/**
59 * This is a hack to fix warnings. The idea is to use this everywhere that we
60 * get the "discarding const" warning by the compiler. That doesn't actually
61 * fix the real issue, but marks the place and you can search the code for
62 * discard_const.
63 *
64 * Please use this macro only when there is no other way to fix the warning.
65 * We should use this function in only in a very few places.
66 *
67 * Also, please call this via the discard_const_p() macro interface, as that
68 * makes the return type safe.
69 */
70#define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
71
72/**
73 * Type-safe version of discard_const
74 */
75#define discard_const_p(type, ptr) ((type *)discard_const(ptr))
76
77#if (__GNUC__ >= 3)
78# ifndef likely
79# define likely(x) __builtin_expect(!!(x), 1)
80# endif
81# ifndef unlikely
82# define unlikely(x) __builtin_expect(!!(x), 0)
83# endif
84#else /* __GNUC__ */
85# ifndef likely
86# define likely(x) (x)
87# endif
88# ifndef unlikely
89# define unlikely(x) (x)
90# endif
91#endif /* __GNUC__ */
92
93/**
94 * }@
95 */
96#endif /* _C_MACRO_H */
97