Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make more functions const. #116

Merged
merged 2 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ edition = "2018"
[dependencies]
bit_field = "0.9.0"
bitflags = "1.0.4"
array-init = "0.0.4"

[dependencies.cast]
version = "0.2.2"
Expand Down
23 changes: 15 additions & 8 deletions src/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,12 @@ impl VirtAddr {
/// This function performs sign extension of bit 47 to make the address canonical, so
/// bits 48 to 64 are overwritten. If you want to check that these bits contain no data,
/// use `new` or `try_new`.
pub fn new_unchecked(mut addr: u64) -> VirtAddr {
if addr.get_bit(47) {
addr.set_bits(48..64, 0xffff);
} else {
addr.set_bits(48..64, 0);
}
VirtAddr(addr)
pub const fn new_unchecked(addr: u64) -> VirtAddr {
// Rust doesn't accept shift operators in const functions at the moment,
// so we use a multiplication and division by 0x1_0000 instead of `<< 16` and `>> 16`.
// By doing the right shift as a signed operation (on a i64), it will
// sign extend the value, repeating the leftmost bit.
VirtAddr((addr.wrapping_mul(0x1_0000) as i64 / 0x1_0000) as u64)
}

/// Creates a virtual address that points to `0`.
Expand All @@ -85,7 +84,7 @@ impl VirtAddr {
}

/// Converts the address to an `u64`.
pub fn as_u64(self) -> u64 {
pub const fn as_u64(self) -> u64 {
self.0
}

Expand Down Expand Up @@ -417,6 +416,14 @@ pub fn align_up(addr: u64, align: u64) -> u64 {
mod tests {
use super::*;

#[test]
pub fn virtaddr_new_unchecked() {
assert_eq!(VirtAddr::new_unchecked(0), VirtAddr(0));
assert_eq!(VirtAddr::new_unchecked(1 << 47), VirtAddr(0xfffff << 47));
assert_eq!(VirtAddr::new_unchecked(123), VirtAddr(123));
assert_eq!(VirtAddr::new_unchecked(123 << 47), VirtAddr(0xfffff << 47));
}

#[test]
pub fn test_align_up() {
// align 1
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![feature(const_fn)]
#![feature(asm)]
#![feature(abi_x86_interrupt)]
#![feature(const_in_array_repeat_expressions)]
#![cfg_attr(not(test), no_std)]
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
#![cfg_attr(feature = "deny-warnings", deny(missing_docs))]
Expand Down
2 changes: 1 addition & 1 deletion src/structures/gdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub struct GlobalDescriptorTable {

impl GlobalDescriptorTable {
/// Creates an empty GDT.
pub fn new() -> GlobalDescriptorTable {
pub const fn new() -> GlobalDescriptorTable {
GlobalDescriptorTable {
table: [0; 8],
next_free: 1,
Expand Down
12 changes: 5 additions & 7 deletions src/structures/paging/page_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ pub struct PageTableEntry {

impl PageTableEntry {
/// Creates an unused page table entry.
pub fn new() -> Self {
pub const fn new() -> Self {
PageTableEntry { entry: 0 }
}

/// Returns whether this entry is zero.
pub fn is_unused(&self) -> bool {
pub const fn is_unused(&self) -> bool {
self.entry == 0
}

Expand All @@ -42,7 +42,7 @@ impl PageTableEntry {
}

/// Returns the flags of this entry.
pub fn flags(&self) -> PageTableFlags {
pub const fn flags(&self) -> PageTableFlags {
PageTableFlags::from_bits_truncate(self.entry)
}

Expand Down Expand Up @@ -176,11 +176,9 @@ pub struct PageTable {

impl PageTable {
/// Creates an empty page table.
pub fn new() -> Self {
use array_init::array_init;

pub const fn new() -> Self {
PageTable {
entries: array_init(|_| PageTableEntry::new()),
entries: [PageTableEntry::new(); ENTRY_COUNT],
}
}

Expand Down