| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_BASE_CHECK_PLATFORM_HPP |
| 4 |
#define MPT_BASE_CHECK_PLATFORM_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
#include "mpt/base/pointer.hpp" |
| 11 |
|
| 12 |
#include <limits> |
| 13 |
|
| 14 |
#include <cassert> |
| 15 |
#include <cstddef> |
| 16 |
#include <cstdint> |
| 17 |
|
| 18 |
#if MPT_LIBC_DJGPP |
| 19 |
#include <crt0.h> |
| 20 |
#endif // MPT_LIBC_DJGPP |
| 21 |
|
| 22 |
|
| 23 |
namespace mpt { |
| 24 |
inline namespace MPT_INLINE_NS { |
| 25 |
|
| 26 |
|
| 27 |
static_assert(sizeof(std::uintptr_t) == sizeof(void *)); |
| 28 |
static_assert(std::numeric_limits<unsigned char>::digits == 8); |
| 29 |
|
| 30 |
static_assert(sizeof(char) == 1); |
| 31 |
|
| 32 |
static_assert(sizeof(std::byte) == 1); |
| 33 |
static_assert(alignof(std::byte) == 1); |
| 34 |
|
| 35 |
static_assert(mpt::arch_bits == static_cast<int>(mpt::pointer_size) * 8); |
| 36 |
|
| 37 |
|
| 38 |
#if MPT_LIBC_DJGPP |
| 39 |
|
| 40 |
namespace platform { |
| 41 |
|
| 42 |
namespace detail { |
| 43 |
|
| 44 |
struct libc_checker { |
| 45 |
private: |
| 46 |
bool m_libc_check_failure = false; |
| 47 |
bool m_libc_has_implicit_code_locking = false; |
| 48 |
public: |
| 49 |
[[nodiscard]] inline libc_checker() { |
| 50 |
assert(((_crt0_startup_flags & _CRT0_FLAG_NONMOVE_SBRK) == _CRT0_FLAG_NONMOVE_SBRK) && ((_crt0_startup_flags & _CRT0_FLAG_UNIX_SBRK) == 0)); |
| 51 |
if (((_crt0_startup_flags & _CRT0_FLAG_NONMOVE_SBRK) == _CRT0_FLAG_NONMOVE_SBRK) && ((_crt0_startup_flags & _CRT0_FLAG_UNIX_SBRK) != 0)) { |
| 52 |
m_libc_check_failure = true; |
| 53 |
} |
| 54 |
assert((_crt0_startup_flags & _CRT0_DISABLE_SBRK_ADDRESS_WRAP) == _CRT0_DISABLE_SBRK_ADDRESS_WRAP); |
| 55 |
if ((_crt0_startup_flags & _CRT0_DISABLE_SBRK_ADDRESS_WRAP) != _CRT0_DISABLE_SBRK_ADDRESS_WRAP) { |
| 56 |
m_libc_check_failure = true; |
| 57 |
} |
| 58 |
if ((_crt0_startup_flags & _CRT0_FLAG_LOCK_MEMORY) == _CRT0_FLAG_LOCK_MEMORY) { |
| 59 |
m_libc_has_implicit_code_locking = true; |
| 60 |
} |
| 61 |
} |
| 62 |
libc_checker(const libc_checker &) = delete; |
| 63 |
libc_checker & operator=(const libc_checker &) = delete; |
| 64 |
[[nodiscard]] inline bool is_ok() const noexcept { |
| 65 |
return !m_libc_check_failure; |
| 66 |
} |
| 67 |
[[nodiscard]] inline bool has_implicit_code_locking() const noexcept { |
| 68 |
return m_libc_has_implicit_code_locking; |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
#if MPT_COMPILER_CLANG |
| 73 |
#pragma clang diagnostic push |
| 74 |
#pragma clang diagnostic ignored "-Wglobal-constructors" |
| 75 |
#endif // MPT_COMPILER_CLANG |
| 76 |
inline const libc_checker g_libc_checker; |
| 77 |
#if MPT_COMPILER_CLANG |
| 78 |
#pragma clang diagnostic pop |
| 79 |
#endif // MPT_COMPILER_CLANG |
| 80 |
|
| 81 |
} // namespace detail |
| 82 |
|
| 83 |
[[nodiscard]] inline const mpt::platform::detail::libc_checker & libc() noexcept { |
| 84 |
return mpt::platform::detail::g_libc_checker; |
| 85 |
} |
| 86 |
|
| 87 |
} // namespace platform |
| 88 |
|
| 89 |
#endif // MPT_LIBC_DJGPP |
| 90 |
|
| 91 |
|
| 92 |
} // namespace MPT_INLINE_NS |
| 93 |
} // namespace mpt |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
#endif // MPT_BASE_CHECK_PLATFORM_HPP |