Debian Codegraph @

src/internal/tagged_offset.rs

raw symbols SCIP
1 use std::ptr::NonNull;
2
3 #[cfg(feature = "unstable-tagged-offsets")]
4 const TAG_MASK: u32 = 0xE0000000;
5 #[cfg(feature = "unstable-tagged-offsets")]
6 const OFFSET_MASK: u32 = 0x1FFFFFFF;
7 const TAG_NULL: u32 = 0;
8 pub(crate) const TAG_BYTES: u32 = 1 << 29;
9 pub(crate) const TAG_NODE: u32 = 2 << 29;
10 pub(crate) const TAG_STATE: u32 = 3 << 29;
11 pub(crate) const TAG_CONTEXT: u32 = 4 << 29;
12
13 pub(crate) trait Pointee {
14 const TAG: u32;
15 }
16
17 impl Pointee for u8 {
18 const TAG: u32 = TAG_BYTES;
19 }
20
21 pub(crate) trait MemoryAllocator {
22 fn base_memory_ptr(&self) -> NonNull<u8>;
23 #[cfg(not(feature = "unstable-tagged-offsets"))]
24 fn units_start(&self) -> NonNull<u8>;
25 #[cfg(feature = "unstable-tagged-offsets")]
26 fn size(&self) -> u32;
27 }
28
29 #[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
30 #[repr(transparent)]
31 pub(crate) struct TaggedOffset(u32);
32
33 #[cfg(feature = "unstable-tagged-offsets")]
34 impl TaggedOffset {
35 pub(crate) const fn null() -> TaggedOffset {
36 TaggedOffset(TAG_NULL)
37 }
38
39 #[inline(always)]
40 pub(crate) const fn from_raw(raw: u32) -> Self {
41 Self(raw)
42 }
43
44 #[inline(always)]
45 pub(crate) const fn from_bytes_offset(raw: u32) -> Self {
46 TaggedOffset::from_raw((raw & OFFSET_MASK) | TAG_BYTES)
47 }
48
49 #[inline(always)]
50 pub(crate) unsafe fn from_ptr<T: Pointee, A: MemoryAllocator>(
51 allocator: &A,
52 ptr: NonNull<T>,
53 ) -> TaggedOffset {
54 let offset = ptr.cast().offset_from(allocator.base_memory_ptr());
55 let offset = u32::try_from(offset).expect("Failed to convert ptr to offset");
56 let val = (offset & OFFSET_MASK) | T::TAG;
57 Self(val)
58 }
59
60 #[inline(always)]
61 pub(crate) fn is_null(&self) -> bool {
62 self.0 == TAG_NULL
63 }
64
65 #[inline(always)]
66 pub(crate) fn is_not_null(&self) -> bool {
67 self.0 != TAG_NULL
68 }
69
70 #[inline(always)]
71 pub(crate) fn get_offset(&self) -> u32 {
72 self.0 & OFFSET_MASK
73 }
74
75 #[inline(always)]
76 pub(crate) fn as_raw(&self) -> u32 {
77 self.0
78 }
79
80 #[inline(always)]
81 pub(crate) unsafe fn as_ptr<T: Pointee, A: MemoryAllocator>(
82 &self,
83 allocator: &A,
84 ) -> NonNull<T> {
85 let offset = self.get_offset();
86
87 assert_eq!(self.0 & TAG_MASK, T::TAG, "Mismatched pointer type tag");
88 assert!(offset < allocator.size(), "Out of bound access");
89
90 allocator.base_memory_ptr().offset(offset as isize).cast()
91 }
92
93 pub(crate) fn is_real_context<A: MemoryAllocator>(&self, _allocator: &A) -> bool {
94 (self.0 & TAG_MASK) == TAG_CONTEXT
95 }
96 }
97
98 #[cfg(not(feature = "unstable-tagged-offsets"))]
99 impl TaggedOffset {
100 pub(crate) const fn null() -> TaggedOffset {
101 TaggedOffset(TAG_NULL)
102 }
103
104 #[inline(always)]
105 pub(crate) const fn from_raw(raw: u32) -> Self {
106 Self(raw)
107 }
108
109 #[inline(always)]
110 pub(crate) const fn from_bytes_offset(raw: u32) -> Self {
111 TaggedOffset::from_raw(raw)
112 }
113
114 #[inline(always)]
115 pub(crate) unsafe fn from_ptr<T: Pointee, A: MemoryAllocator>(
116 allocator: &A,
117 ptr: NonNull<T>,
118 ) -> TaggedOffset {
119 let offset = ptr.cast().offset_from(allocator.base_memory_ptr());
120 let offset = u32::try_from(offset).expect("Failed to convert ptr to offset");
121 Self(offset)
122 }
123
124 #[inline(always)]
125 pub(crate) fn is_null(&self) -> bool {
126 self.0 == TAG_NULL
127 }
128
129 #[inline(always)]
130 pub(crate) fn is_not_null(&self) -> bool {
131 self.0 != TAG_NULL
132 }
133
134 #[inline(always)]
135 pub(crate) fn get_offset(&self) -> u32 {
136 self.0
137 }
138
139 #[inline(always)]
140 pub(crate) fn as_raw(&self) -> u32 {
141 self.0
142 }
143
144 #[inline(always)]
145 pub(crate) unsafe fn as_ptr<T: Pointee, A: MemoryAllocator>(
146 &self,
147 allocator: &A,
148 ) -> NonNull<T> {
149 let offset = self.get_offset();
150 allocator.base_memory_ptr().offset(offset as isize).cast()
151 }
152
153 pub(crate) unsafe fn is_real_context<A: MemoryAllocator>(&self, allocator: &A) -> bool {
154 // A "real" context must be in the unit area and not in the text buffer area.
155 let ptr = allocator
156 .base_memory_ptr()
157 .offset(self.get_offset() as isize)
158 .cast();
159 ptr >= allocator.units_start()
160 }
161 }