Skip to content

Commit 1fb5ec4

Browse files
authored
Merge pull request #584 from mkroening/paging-repr-rust
feat: make page types `repr(transparent)` and range types `repr(Rust)`
2 parents a96374a + 4400472 commit 1fb5ec4

4 files changed

Lines changed: 19 additions & 6 deletions

File tree

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- `OffsetPageTable`'s `PageTableFrameMapping` implementation is now public as `PhysOffset`.
1010
- [make range types `!Copy`](https://github.com/rust-osdev/x86_64/pull/581)
1111
- To migrate, use `.clone()` if necessary.
12+
- [make page types `repr(transparent)` and range types `repr(Rust)`](https://github.com/rust-osdev/x86_64/pull/584)
1213

1314
# 0.15.4 – 2025-11-24
1415

src/addr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const ADDRESS_SPACE_SIZE: u64 = 0x1_0000_0000_0000;
2828
/// On `x86_64`, only the 48 lower bits of a virtual address can be used. The top 16 bits need
2929
/// to be copies of bit 47, i.e. the most significant bit. Addresses that fulfil this criterion
3030
/// are called “canonical”. This type guarantees that it always represents a canonical address.
31+
///
32+
/// # Representation
33+
///
34+
/// This struct has the same representation as a [`u64`].
3135
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
3236
#[repr(transparent)]
3337
pub struct VirtAddr(u64);
@@ -41,6 +45,10 @@ pub struct VirtAddr(u64);
4145
///
4246
/// On `x86_64`, only the 52 lower bits of a physical address can be used. The top 12 bits need
4347
/// to be zero. This type guarantees that it always represents a valid physical address.
48+
///
49+
/// # Representation
50+
///
51+
/// This struct has the same representation as a [`u64`].
4452
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
4553
#[repr(transparent)]
4654
pub struct PhysAddr(u64);

src/structures/paging/frame.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ use core::marker::PhantomData;
88
use core::ops::{Add, AddAssign, Sub, SubAssign};
99

1010
/// A physical memory frame.
11+
///
12+
/// # Representation
13+
///
14+
/// This struct has the same representation as a [`u64`].
1115
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
12-
#[repr(C)]
16+
#[repr(transparent)]
1317
pub struct PhysFrame<S: PageSize = Size4KiB> {
1418
// TODO: Make private when our minimum supported stable Rust version is 1.61
1519
pub(crate) start_address: PhysAddr,
@@ -134,7 +138,6 @@ impl<S: PageSize> Sub<PhysFrame<S>> for PhysFrame<S> {
134138

135139
/// An range of physical memory frames, exclusive the upper bound.
136140
#[derive(Clone, PartialEq, Eq, Hash)]
137-
#[repr(C)]
138141
pub struct PhysFrameRange<S: PageSize = Size4KiB> {
139142
/// The start of the range, inclusive.
140143
pub start: PhysFrame<S>,
@@ -192,7 +195,6 @@ impl<S: PageSize> fmt::Debug for PhysFrameRange<S> {
192195

193196
/// An range of physical memory frames, inclusive the upper bound.
194197
#[derive(Clone, PartialEq, Eq, Hash)]
195-
#[repr(C)]
196198
pub struct PhysFrameRangeInclusive<S: PageSize = Size4KiB> {
197199
/// The start of the range, inclusive.
198200
pub start: PhysFrame<S>,

src/structures/paging/page.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,12 @@ impl PageSize for Size1GiB {
6262
impl Sealed for super::Size1GiB {}
6363

6464
/// A virtual memory page.
65+
///
66+
/// # Representation
67+
///
68+
/// This struct has the same representation as a [`u64`].
6569
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
66-
#[repr(C)]
70+
#[repr(transparent)]
6771
pub struct Page<S: PageSize = Size4KiB> {
6872
start_address: VirtAddr,
6973
size: PhantomData<S>,
@@ -326,7 +330,6 @@ impl<S: PageSize> Step for Page<S> {
326330

327331
/// A range of pages with exclusive upper bound.
328332
#[derive(Clone, PartialEq, Eq, Hash)]
329-
#[repr(C)]
330333
pub struct PageRange<S: PageSize = Size4KiB> {
331334
/// The start of the range, inclusive.
332335
pub start: Page<S>,
@@ -395,7 +398,6 @@ impl<S: PageSize> fmt::Debug for PageRange<S> {
395398

396399
/// A range of pages with inclusive upper bound.
397400
#[derive(Clone, PartialEq, Eq, Hash)]
398-
#[repr(C)]
399401
pub struct PageRangeInclusive<S: PageSize = Size4KiB> {
400402
/// The start of the range, inclusive.
401403
pub start: Page<S>,

0 commit comments

Comments
 (0)