IsoSpec  2.1.2
platform.h
1 /*
2  * Copyright (C) 2015-2020 Mateusz Łącki and Michał Startek.
3  *
4  * This file is part of IsoSpec.
5  *
6  * IsoSpec is free software: you can redistribute it and/or modify
7  * it under the terms of the Simplified ("2-clause") BSD licence.
8  *
9  * IsoSpec is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * You should have received a copy of the Simplified BSD Licence
14  * along with IsoSpec. If not, see <https://opensource.org/licenses/BSD-2-Clause>.
15  */
16 
17 #pragma once
18 
19 #if !defined(ISOSPEC_BUILDING_R)
20 #define ISOSPEC_BUILDING_R false
21 #endif
22 
23 #if !defined(ISOSPEC_BUILDING_CPP)
24 #define ISOSPEC_BUILDING_CPP true
25 #endif
26 
27 #if !defined(ISOSPEC_BUILDING_PYTHON)
28 #define ISOSPEC_BUILDING_PYTHON false
29 #endif
30 
31 #if !defined(ISOSPEC_BUILDING_OPENMS)
32 #define ISOSPEC_BUILDING_OPENMS false
33 #endif
34 
35 #if defined(__unix__) || defined(__unix) || \
36  (defined(__APPLE__) && defined(__MACH__))
37 #define ISOSPEC_TEST_WE_ARE_ON_UNIX_YAY true
38 #define ISOSPEC_TEST_WE_ARE_ON_WINDOWS false /* CYGWIN doesn't really count as Windows for our purposes, we'll be using UNIX API anyway */
39 #define ISOSPEC_TEST_GOT_SYSTEM_MMAN true
40 #define ISOSPEC_TEST_GOT_MMAN true
41 #elif defined(__MINGW32__) || defined(_WIN32)
42 #define ISOSPEC_TEST_WE_ARE_ON_UNIX_YAY false
43 #define ISOSPEC_TEST_WE_ARE_ON_WINDOWS true
44 #define ISOSPEC_TEST_GOT_SYSTEM_MMAN false
45 #define ISOSPEC_TEST_GOT_MMAN true
46 #else
47 #define ISOSPEC_TEST_WE_ARE_ON_UNIX_YAY false /* Well, probably... */
48 #define ISOSPEC_TEST_WE_ARE_ON_WINDOWS false
49 #define ISOSPEC_TEST_GOT_SYSTEM_MMAN false
50 #define ISOSPEC_TEST_GOT_MMAN false
51 #endif
52 
53 #if !defined(ISOSPEC_USE_PTHREADS)
54 #define ISOSPEC_USE_PTHREADS false // TODO(who knows?): possibly put a macro here to detect whether we
55 #endif // can/should use pthreads - or rip them out altogether.
56  // Investigate whether the performance advantage of pthreads on
57  // some platforms (*cough* CYGWIN *cough*) is still large
58  // enough to justify keeping both implementations around
59 
60 #if !defined(ISOSPEC_WE_ARE_ON_UNIX_YAY)
61 #define ISOSPEC_WE_ARE_ON_UNIX_YAY ISOSPEC_TEST_WE_ARE_ON_UNIX_YAY
62 #endif
63 
64 #if !defined(ISOSPEC_WE_ARE_ON_WINDOWS)
65 #define ISOSPEC_WE_ARE_ON_WINDOWS ISOSPEC_TEST_WE_ARE_ON_WINDOWS
66 #endif
67 
68 #if !defined(ISOSPEC_GOT_SYSTEM_MMAN)
69 #define ISOSPEC_GOT_SYSTEM_MMAN ISOSPEC_TEST_GOT_SYSTEM_MMAN
70 #endif
71 
72 #if !defined(ISOSPEC_GOT_MMAN)
73 #define ISOSPEC_GOT_MMAN ISOSPEC_TEST_GOT_MMAN
74 #endif
75 
76 
77 // Note: __GNUC__ is defined by clang and gcc
78 #ifdef __GNUC__
79 #define ISOSPEC_IMPOSSIBLE(condition) if(condition) __builtin_unreachable();
80 #define ISOSPEC_LIKELY(condition) __builtin_expect(static_cast<bool>(condition), 1)
81 #define ISOSPEC_UNLIKELY(condition) __builtin_expect(static_cast<bool>(condition), 0)
82 // For aggressive inlining
83 #define ISOSPEC_FORCE_INLINE __attribute__ ((always_inline)) inline
84 #elif defined _MSC_VER
85 #define ISOSPEC_IMPOSSIBLE(condition) __assume(!(condition));
86 #define ISOSPEC_LIKELY(condition) condition
87 #define ISOSPEC_UNLIKELY(condition) condition
88 #define ISOSPEC_FORCE_INLINE __forceinline inline
89 #else
90 #define ISOSPEC_IMPOSSIBLE(condition)
91 #define ISOSPEC_LIKELY(condition) condition
92 #define ISOSPEC_UNLIKELY(condition) condition
93 #define ISOSPEC_FORCE_INLINE inline
94 #endif
95 
96 #ifdef ISOSPEC_DEBUG
97 #undef ISOSPEC_IMPOSSIBLE
98 #include <cassert>
99 #define ISOSPEC_IMPOSSIBLE(condition) assert(!(condition));
100 #endif /* ISOSPEC_DEBUG */
101 
102 
103 #if ISOSPEC_GOT_MMAN
104  #if ISOSPEC_GOT_SYSTEM_MMAN
105  #include <sys/mman.h>
106  #else
107  #include "mman.h"
108  #endif
109 #else
110  #include <stdlib.h> /* malloc, free, rand */
111 #endif
112 
113 
114 #if defined(OPENMS_DLLAPI) /* IsoSpec is being built as a part of OpenMS: use their visibility macros */
115 #define ISOSPEC_EXPORT_SYMBOL OPENMS_DLLAPI
116 #else /* it's a can of worms we don't yet want to open ourselves though... */
117 #define ISOSPEC_EXPORT_SYMBOL
118 #endif
119 
120 #if !defined(__cpp_if_constexpr)
121 #define constexpr_if if
122 #define ISOSPEC_MAYBE_UNUSED
123 #else
124 #define constexpr_if if constexpr
125 #define ISOSPEC_MAYBE_UNUSED [[maybe_unused]]
126 #endif