Broom 1.0.0
A thread-local C++ Garbage Collector
Loading...
Searching...
No Matches
globals.h
Go to the documentation of this file.
1#pragma once
2
3#include <bit>
4#include <cstdint>
5#include <limits>
6#include <type_traits>
7
8#include "src/macros.h"
9
10namespace broom {
11constexpr const uintptr_t kNullUintPtr = 0;
12constexpr const intptr_t kNullIntPtr = 0;
13using MemoryAddress = uintptr_t;
14using UintPtr = uintptr_t;
17constexpr const int kPointerSize = sizeof(void*);
18constexpr const int kUintPtrSize = sizeof(UintPtr);
19constexpr const int kIntPtrSize = sizeof(IntPtr);
20
21constexpr const size_t KB = 1024;
22constexpr const size_t MB = KB * 1024;
23constexpr const size_t GB = MB * 1024;
24constexpr const size_t TB = GB * 1024;
25
26constexpr const int kPageSize = 4 * KB;
27constexpr const int kHugePageSize = 2 * MB;
28
29template <typename T, typename U>
30concept StaticCastable = requires(T t) {
31 { static_cast<U>(t) } -> std::same_as<U>;
32};
33
34template <typename T, typename U>
35concept ReinterpretCastable = requires(T t) {
36 { reinterpret_cast<U>(t) } -> std::same_as<U>;
37};
38
41
42template <typename T>
43concept IsAllocatorMetadata = std::is_same_v<T, AllocatorFrontMetadata> ||
44 std::is_same_v<T, AllocatorCommonMetadata>;
45
46template <typename T, typename U>
48constexpr inline T SafeCast(U v) {
49 return static_cast<T>(v);
50}
51
52template <typename T, typename U>
56 return reinterpret_cast<T>(v);
57}
58
59template <typename T, typename U>
60constexpr inline T UnsafeCast(U v) {
61 return std::bit_cast<T>(v);
62}
63
64template <size_t N, typename T>
65 requires std::is_integral_v<T> && (N > 0) && (N % 2 == 0)
67 return (value & (N - 1)) == 0;
68}
69
70template <size_t N, typename T>
71 requires(N > 0) && (N % 2 == 0)
75
76template <size_t N, typename T>
77 requires std::is_integral_v<T> && (N > 0) && (N % 2 == 0)
79 return value & static_cast<T>(-SafeCast<std::make_signed_t<T>>(N));
80}
81
82template <size_t N, typename T>
83 requires std::is_integral_v<T> && (N > 0) && (N % 2 == 0)
85 BASSERT_GE(std::numeric_limits<T>::max() - value, N - 1,
86 "Unexpected overflow");
87 return RoundDown<N, T>(static_cast<T>(value + (N - 1)));
88}
89
90template <typename T>
91 requires std::is_unsigned_v<T>
92constexpr inline bool AddOverflowChecked(T lhs, T rhs, T& destination) {
93 if (rhs > 0 && lhs > std::numeric_limits<T>::max() - rhs) return true;
95 return false;
96}
97} // namespace broom
#define BASSERT_GE(lhs, rhs, m,...)
Definition macros.h:59
constexpr const intptr_t kNullIntPtr
Definition globals.h:12
constexpr const size_t TB
Definition globals.h:24
constexpr const size_t KB
Definition globals.h:21
constexpr const size_t MB
Definition globals.h:22
constexpr T RoundUp(T value)
Definition globals.h:84
uintptr_t MemoryAddress
Definition globals.h:13
constexpr const int kPageSize
Definition globals.h:26
constexpr const MemoryAddress kNullMemoryAddress
Definition globals.h:16
constexpr const int kHugePageSize
Definition globals.h:27
constexpr bool IsAligned(T value)
Definition globals.h:66
constexpr const size_t GB
Definition globals.h:23
constexpr bool AddOverflowChecked(T lhs, T rhs, T &destination)
Definition globals.h:92
constexpr const int kPointerSize
Definition globals.h:17
constexpr T RoundDown(T value)
Definition globals.h:78
constexpr const uintptr_t kNullUintPtr
Definition globals.h:11
uintptr_t UintPtr
Definition globals.h:14
constexpr T SafeCast(U v)
Definition globals.h:48
constexpr const int kIntPtrSize
Definition globals.h:19
intptr_t IntPtr
Definition globals.h:15
std::queue< T, broom::deque< T > > queue
Definition broom-queue.h:12
constexpr const int kUintPtrSize
Definition globals.h:18
constexpr T UnsafeCast(U v)
Definition globals.h:60