The Pedigree Project 0.1
TargetInfo.h
1/*
2 * Copyright (c) 2026, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef KERNEL_TARGETINFO_H
21#define KERNEL_TARGETINFO_H
22#include "pedigree/kernel/processor/types.h"
23
24#include <config.h>
25
26#if !defined(TARGET_IS_LITTLE_ENDIAN)
27#error TARGET_IS_LITTLE_ENDIAN not defined
28#endif
29
31class TargetInfo final {
32 public:
33 TargetInfo() = delete;
34
40 static constexpr size_t getPageSize() noexcept {
41 return PEDIGREE_TARGET_PAGE_SIZE;
42 }
43
44 static constexpr size_t getPageShift() noexcept {
45 size_t pageSize = getPageSize();
46 size_t shift = 0;
47 while (pageSize > 1) {
48 pageSize >>= 1;
49 ++shift;
50 }
51 return shift;
52 }
53
54 static constexpr size_t getPageOffsetMask() noexcept {
55 return getPageSize() - 1;
56 }
57
58 static constexpr size_t getPointerBits() noexcept {
59 return sizeof(void*) * __CHAR_BIT__;
60 }
61
62 static constexpr bool isLittleEndian() noexcept {
63 return TARGET_IS_LITTLE_ENDIAN != 0;
64 }
65};
66
67static_assert(TargetInfo::getPageSize() != 0, "Target page size must be nonzero.");
68static_assert(TargetInfo::getPageSize() == PAGE_SIZE,
69 "The compatibility page-size macro must match TargetInfo.");
70static_assert((TargetInfo::getPageSize() & TargetInfo::getPageOffsetMask()) == 0,
71 "Target page size must be a power of two.");
72static_assert((size_t{1} << TargetInfo::getPageShift()) == TargetInfo::getPageSize(),
73 "Target page size and shift must agree.");
74static_assert(sizeof(uintptr_t) == sizeof(void*), "uintptr_t must have the target pointer width.");
75static_assert(sizeof(size_t) == sizeof(uintptr_t),
76 "size_t and uintptr_t must have the same target width.");
77
78#if defined(BITS_64) && BITS_64
79static_assert(TargetInfo::getPointerBits() == 64, "BITS_64 disagrees with the compiler target.");
80#endif
81
82#if defined(BITS_32) && BITS_32
83static_assert(TargetInfo::getPointerBits() == 32, "BITS_32 disagrees with the compiler target.");
84#endif
85
86#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__)
87static_assert(TargetInfo::isLittleEndian() == (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__),
88 "Configured target endianness disagrees with the compiler target.");
89#endif
90
91#endif
static constexpr size_t getPageSize() noexcept
Definition TargetInfo.h:40