libdecaf
Loading...
Searching...
No Matches
common.h
Go to the documentation of this file.
1
12#ifndef __DECAF_COMMON_H__
13#define __DECAF_COMMON_H__ 1
14
15#include <stdint.h>
16#if defined (__GNUC__) // File only exists for GNU compilers
17#include <sys/types.h>
18#endif
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24/* Goldilocks' build flags default to hidden and stripping executables. */
26#if DOXYGEN || defined(__attribute__)
27#define __attribute__(x)
28#define NOINLINE
29#endif
30
31/* Aliasing MSVC preprocessing to GNU preprocessing */
32#if defined _MSC_VER
33# define __attribute__(x) // Turn off attribute code
34# define __attribute(x)
35# define __restrict__ __restrict // Use MSVC restrict code
36# if defined _DLL
37# define DECAF_API_VIS __declspec(dllexport) // MSVC for visibility
38# else
39# define DECAF_API_VIS __declspec(dllimport)
40# endif
41
42//# define DECAF_NOINLINE __declspec(noinline) // MSVC for noinline
43//# define DECAF_INLINE __forceinline // MSVC for always inline
44//# define DECAF_WARN_UNUSED _Check_return_
45#else // MSVC
46#define DECAF_API_VIS __attribute__((visibility("default")))
47#define DECAF_API_IMPORT
48#endif
49
50// The following are disabled for MSVC
51#define DECAF_NOINLINE __attribute__((noinline))
52#define DECAF_INLINE inline __attribute__((always_inline,unused))
53#define DECAF_WARN_UNUSED __attribute__((warn_unused_result))
54#define DECAF_NONNULL __attribute__((nonnull))
55// Cribbed from libnotmuch
56#if defined (__clang_major__) && __clang_major__ >= 3 \
57 || defined (__GNUC__) && __GNUC__ >= 5 \
58 || defined (__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ >= 5
59#define DECAF_DEPRECATED(msg) __attribute__ ((deprecated(msg)))
60#else
61#define DECAF_DEPRECATED(msg) __attribute__ ((deprecated))
62#endif
65/* Internal word types.
66 *
67 * Somewhat tricky. This could be decided separately per platform. However,
68 * the structs do need to be all the same size and alignment on a given
69 * platform to support dynamic linking, since even if you header was built
70 * with eg arch_neon, you might end up linking a library built with arch_arm32.
71 */
72#ifndef DECAF_WORD_BITS
73 #if (defined(__ILP64__) || defined(__amd64__) || defined(__x86_64__) || (((__UINT_FAST32_MAX__)>>30)>>30))
74 #define DECAF_WORD_BITS 64
75 #else
76 #define DECAF_WORD_BITS 32
77 #endif
78#endif
79
80#if DECAF_WORD_BITS == 64
81typedef uint64_t decaf_word_t;
82typedef int64_t decaf_sword_t;
83typedef uint64_t decaf_bool_t;
84typedef __uint128_t decaf_dword_t;
85typedef __int128_t decaf_dsword_t;
86#elif DECAF_WORD_BITS == 32
87typedef uint32_t decaf_word_t;
88typedef int32_t decaf_sword_t;
89typedef uint32_t decaf_bool_t;
90typedef uint64_t decaf_dword_t;
91typedef int64_t decaf_dsword_t;
92#else
93#error "Only supporting DECAF_WORD_BITS = 32 or 64 for now"
94#endif
95
96/* MSCV compiler doesn't like the trick to have -1 assigned to an unsigned int to
97 * set it to all ones, so do it openly */
98#if DECAF_WORD_BITS == 64
100static const decaf_bool_t DECAF_TRUE = (decaf_bool_t)0xFFFFFFFFFFFFFFFF;
102static const decaf_word_t DECAF_WORD_ALL_SET = (decaf_word_t)0xFFFFFFFFFFFFFFFF;
104static const decaf_word_t DECAF_WORD_ALL_UNSET = (decaf_word_t)0x0;
105#elif DECAF_WORD_BITS == 32
107static const decaf_bool_t DECAF_TRUE = (decaf_bool_t)0xFFFFFFFF;
109static const decaf_word_t DECAF_WORD_ALL_SET = (decaf_word_t)0xFFFFFFFF;
111static const decaf_word_t DECAF_WORD_ALL_UNSET = (decaf_word_t)0x0;
112#else
113#error "Only supporting DECAF_WORD_BITS = 32 or 64 for now"
114#endif
115
117static const decaf_bool_t DECAF_FALSE = 0;
118
120typedef enum {
122 DECAF_FAILURE = 0
124
125
127static DECAF_INLINE decaf_error_t
128decaf_succeed_if(decaf_bool_t x) {
129 return (decaf_error_t)x;
130}
131
133static DECAF_INLINE decaf_bool_t
134decaf_successful(decaf_error_t e) {
135 decaf_word_t succ = DECAF_SUCCESS;
136 decaf_dword_t w = ((decaf_word_t)e) ^ succ;
137 return (w-1)>>DECAF_WORD_BITS;
138}
139
141void DECAF_API_VIS decaf_bzero (
142 void *data,
143 size_t size
144) DECAF_NONNULL;
145
147decaf_bool_t DECAF_API_VIS decaf_memeq (
148 const void *data1,
149 const void *data2,
150 size_t size
151) DECAF_NONNULL DECAF_WARN_UNUSED;
152
153#ifdef __cplusplus
154} /* extern "C" */
155#endif
156
157#endif /* __DECAF_COMMON_H__ */
void DECAF_API_VIS decaf_bzero(void *data, size_t size) DECAF_NONNULL
Overwrite data with zeros.
decaf_bool_t DECAF_API_VIS decaf_memeq(const void *data1, const void *data2, size_t size) DECAF_NONNULL DECAF_WARN_UNUSED
Compare two buffers, returning DECAF_TRUE if they are equal.
#define DECAF_WORD_BITS
The number of bits in a word.
Definition common.h:76
decaf_error_t
Another boolean type used to indicate success or failure.
Definition common.h:120
@ DECAF_FAILURE
The operation failed.
Definition common.h:122
@ DECAF_SUCCESS
The operation succeeded.
Definition common.h:121