DebugServer2
Loading...
Searching...
No Matches
Base.h
1//
2// Copyright (c) 2014-present, Facebook, Inc.
3// All rights reserved.
4//
5// This source code is licensed under the University of Illinois/NCSA Open
6// Source License found in the LICENSE file in the root directory of this
7// source tree. An additional grant of patent rights can be found in the
8// PATENTS file in the same directory.
9//
10
11#pragma once
12
13#if defined(_WIN32)
14// clang-format off
15#include <winsock2.h>
16#include <windef.h>
17// clang-format on
18#if !defined(__MINGW32__)
19typedef SSIZE_T ssize_t;
20#endif
21#else
22#include <cstdint>
23#include <cstdlib>
24#endif
25#include <iostream>
26#include <type_traits>
27#include <memory>
28#include <utility>
29
30#if defined(__clang__)
31#define COMPILER_CLANG
32#elif defined(__GNUC__)
33#define COMPILER_GCC
34#elif defined(_MSC_VER)
35#define COMPILER_MSVC
36#else
37#error "Compiler not supported."
38#endif
39
40#if defined(__linux__)
41#define OS_LINUX
42#elif defined(_WIN32)
43#define OS_WIN32
44#elif defined(__FreeBSD__)
45#define OS_FREEBSD
46#elif defined(__APPLE__)
47#define OS_DARWIN
48#else
49#error "Target not supported."
50#endif
51
52#if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_DARWIN)
53#define OS_POSIX
54#endif
55
56#if defined(OS_LINUX)
57#if defined(__TIZEN__)
58#define PLATFORM_TIZEN
59#elif defined(__ANDROID__)
60#define PLATFORM_ANDROID
61#endif
62#elif defined(OS_WIN32)
63#if defined(__MINGW32__)
64#define PLATFORM_MINGW
65#endif
66#endif
67
68#if defined(__arm__) || defined(_M_ARM)
69#define ARCH_ARM
70#define BITSIZE_32
71#elif defined(__aarch64__) || defined(_M_ARM64)
72#define ARCH_ARM64
73#define BITSIZE_64
74#elif defined(__i386__) || defined(_M_IX86)
75#define ARCH_X86
76#define BITSIZE_32
77#elif defined(__x86_64__) || defined(_M_AMD64)
78#define ARCH_X86_64
79#define BITSIZE_64
80#elif defined(__riscv)
81#define ARCH_RISCV
82#if __riscv_xlen == 32
83#define BITSIZE_32
84#elif __riscv_xlen == 64
85#define BITSIZE_64
86#elif __riscv_xlen == 128
87#define BITSIZE_128
88#endif
89#else
90#error "Architecture not supported."
91#endif
92
93#if defined(COMPILER_MSVC)
94#define ENDIAN_LITTLE
95#else
96#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
97#define ENDIAN_LITTLE
98#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
99#define ENDIAN_BIG
100#elif __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__
101#define ENDIAN_MIDDLE
102#else
103#error "Unknown endianness."
104#endif
105#endif
106
107// We use this weird array_sizeof implementation to get the number of elements
108// in an array in two cases we care about:
109// 1) simple static arrays (e.g.: int foo[10]);
110// 2) more complex structures that have a static array member but use an
111// overload of operator[] to access data, and apply some transforms to the
112// index passed. This happens in CPUState functions.
113//
114// array_sizeof is enabled only if the argument is not a pointer (to avoid the
115// classic sizeof(pointer) bug), and if the argument is a POD type (to avoid
116// users doing array_sizeof(my_std_vector) and such. The idea is that if a type
117// overloads operator[] and is a POD, the underlying storage is probably in the
118// structure, and not held as a reference or a pointer.
119template <typename T>
120typename std::enable_if<
121 !std::is_pointer_v<T> && std::is_standard_layout_v<T>,
122 size_t>::type static inline constexpr array_sizeof(T const &array) {
123 return sizeof(array) / (reinterpret_cast<uintptr_t>(&array[1]) -
124 reinterpret_cast<uintptr_t>(&array[0]));
125}
126
127namespace ds2 {
128// Wrapper to allow classes with protected constructors to call
129// `make_protected_unique` to construct a `unique_ptr` cleanly (which calls
130// `make_unique` internally). Without this, classes with protected constructors
131// cannot be `make_unique`'d.
132template <typename T> struct make_unique_enabler {
133 struct make_unique_enabler_helper : public T {
134 template <typename... Args>
135 make_unique_enabler_helper(Args... args) : T(std::forward<Args>(args)...) {}
136 };
137
138 template <typename... Args>
139 static std::unique_ptr<T> make_protected_unique(Args... args) {
140 return std::make_unique<make_unique_enabler_helper>(
141 std::forward<Args>(args)...);
142 }
143};
144} // namespace ds2
Definition Base.h:132