-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform_defines.h
More file actions
176 lines (153 loc) · 4.17 KB
/
platform_defines.h
File metadata and controls
176 lines (153 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**************************************************************************/
/* platform_defines.h */
/**************************************************************************/
/* Independent Memory Management Module */
/* Platform-specific type definitions and macros */
/**************************************************************************/
#pragma once
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <utility>
#include <type_traits>
// Ensure C++14 or later for basic functionality
// Note: Some features may require C++17, consider upgrading your project settings
static_assert(__cplusplus >= 201402L, "Minimum of C++14 required. Consider upgrading to C++17 for full feature support.");
// Platform-specific inline macros
#ifndef MEMORY_ALWAYS_INLINE
#if defined(__GNUC__)
#define MEMORY_ALWAYS_INLINE __attribute__((always_inline)) inline
#elif defined(_MSC_VER)
#define MEMORY_ALWAYS_INLINE __forceinline
#else
#define MEMORY_ALWAYS_INLINE inline
#endif
#endif
#ifndef MEMORY_FORCE_INLINE
#if defined(DEBUG) || defined(_DEBUG)
#define MEMORY_FORCE_INLINE inline
#else
#define MEMORY_FORCE_INLINE MEMORY_ALWAYS_INLINE
#endif
#endif
#ifndef MEMORY_NO_INLINE
#if defined(__GNUC__)
#define MEMORY_NO_INLINE __attribute__((noinline))
#elif defined(_MSC_VER)
#define MEMORY_NO_INLINE __declspec(noinline)
#else
#define MEMORY_NO_INLINE
#endif
#endif
// Type aliases for consistency
using memory_size_t = std::size_t;
using memory_uint64_t = std::uint64_t;
using memory_uint32_t = std::uint32_t;
using memory_uint8_t = std::uint8_t;
using memory_uintptr_t = std::uintptr_t;
// Utility functions
template <typename T>
MEMORY_ALWAYS_INLINE bool is_power_of_2(const T x) {
return x && ((x & (x - 1)) == 0);
}
// Function to find the next power of 2
constexpr memory_uint64_t next_power_of_2(memory_uint64_t p_number) {
if (p_number == 0) {
return 0;
}
--p_number;
p_number |= p_number >> 1;
p_number |= p_number >> 2;
p_number |= p_number >> 4;
p_number |= p_number >> 8;
p_number |= p_number >> 16;
p_number |= p_number >> 32;
return ++p_number;
}
// Platform-specific alignment
#ifndef MEMORY_ALIGN
#if defined(__GNUC__) || defined(__clang__)
#define MEMORY_ALIGN(x) __attribute__((aligned(x)))
#elif defined(_MSC_VER)
#define MEMORY_ALIGN(x) __declspec(align(x))
#else
#define MEMORY_ALIGN(x)
#endif
#endif
// String manipulation macros
#ifndef MEMORY_STR
#define MEMORY_STR(m_x) #m_x
#define MEMORY_MKSTR(m_x) MEMORY_STR(m_x)
#endif
// Function name macro
#ifdef __GNUC__
#define MEMORY_FUNCTION_STR __FUNCTION__
#else
#define MEMORY_FUNCTION_STR __FUNCTION__
#endif
// Swap macro
#ifndef MEMORY_SWAP
#define MEMORY_SWAP(m_x, m_y) std::swap((m_x), (m_y))
#endif
// Likely/unlikely hints
#ifndef MEMORY_LIKELY
#if defined(__GNUC__) || defined(__clang__)
#define MEMORY_LIKELY(x) __builtin_expect(!!(x), 1)
#define MEMORY_UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
#define MEMORY_LIKELY(x) (x)
#define MEMORY_UNLIKELY(x) (x)
#endif
#endif
// Debug/Release detection
#ifndef MEMORY_DEBUG_ENABLED
#if defined(DEBUG) || defined(_DEBUG) || !defined(NDEBUG)
#define MEMORY_DEBUG_ENABLED 1
#else
#define MEMORY_DEBUG_ENABLED 0
#endif
#endif
// Platform detection
#ifndef MEMORY_PLATFORM_WINDOWS
#if defined(_WIN32) || defined(_WIN64)
#define MEMORY_PLATFORM_WINDOWS 1
#else
#define MEMORY_PLATFORM_WINDOWS 0
#endif
#endif
#ifndef MEMORY_PLATFORM_LINUX
#if defined(__linux__)
#define MEMORY_PLATFORM_LINUX 1
#else
#define MEMORY_PLATFORM_LINUX 0
#endif
#endif
#ifndef MEMORY_PLATFORM_MACOS
#if defined(__APPLE__) && defined(__MACH__)
#define MEMORY_PLATFORM_MACOS 1
#else
#define MEMORY_PLATFORM_MACOS 0
#endif
#endif
// Compiler detection
#ifndef MEMORY_COMPILER_MSVC
#if defined(_MSC_VER)
#define MEMORY_COMPILER_MSVC 1
#else
#define MEMORY_COMPILER_MSVC 0
#endif
#endif
#ifndef MEMORY_COMPILER_GCC
#if defined(__GNUC__) && !defined(__clang__)
#define MEMORY_COMPILER_GCC 1
#else
#define MEMORY_COMPILER_GCC 0
#endif
#endif
#ifndef MEMORY_COMPILER_CLANG
#if defined(__clang__)
#define MEMORY_COMPILER_CLANG 1
#else
#define MEMORY_COMPILER_CLANG 0
#endif
#endif