Skip to content

Commit 583f008

Browse files
authored
Merge pull request #129 from AntoineSebert/master
Add `#[inline]` attribute to small functions
2 parents 55f6b58 + 4525fb8 commit 583f008

File tree

16 files changed

+143
-0
lines changed

16 files changed

+143
-0
lines changed

src/addr.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ impl VirtAddr {
6969
/// This function performs sign extension of bit 47 to make the address canonical, so
7070
/// bits 48 to 64 are overwritten. If you want to check that these bits contain no data,
7171
/// use `new` or `try_new`.
72+
#[inline]
7273
pub const fn new_unchecked(addr: u64) -> VirtAddr {
7374
// Rust doesn't accept shift operators in const functions at the moment,
7475
// so we use a multiplication and division by 0x1_0000 instead of `<< 16` and `>> 16`.
@@ -78,11 +79,13 @@ impl VirtAddr {
7879
}
7980

8081
/// Creates a virtual address that points to `0`.
82+
#[inline]
8183
pub const fn zero() -> VirtAddr {
8284
VirtAddr(0)
8385
}
8486

8587
/// Converts the address to an `u64`.
88+
#[inline]
8689
pub const fn as_u64(self) -> u64 {
8790
self.0
8891
}
@@ -93,25 +96,29 @@ impl VirtAddr {
9396
// on this function. At least for 32- and 64-bit we know the `as u64` cast
9497
// doesn't truncate.
9598
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
99+
#[inline]
96100
pub fn from_ptr<T>(ptr: *const T) -> Self {
97101
Self::new(ptr as u64)
98102
}
99103

100104
/// Converts the address to a raw pointer.
101105
#[cfg(target_pointer_width = "64")]
106+
#[inline]
102107
pub fn as_ptr<T>(self) -> *const T {
103108
self.as_u64() as *const T
104109
}
105110

106111
/// Converts the address to a mutable raw pointer.
107112
#[cfg(target_pointer_width = "64")]
113+
#[inline]
108114
pub fn as_mut_ptr<T>(self) -> *mut T {
109115
self.as_ptr::<T>() as *mut T
110116
}
111117

112118
/// Aligns the virtual address upwards to the given alignment.
113119
///
114120
/// See the `align_up` function for more information.
121+
#[inline]
115122
pub fn align_up<U>(self, align: U) -> Self
116123
where
117124
U: Into<u64>,
@@ -122,6 +129,7 @@ impl VirtAddr {
122129
/// Aligns the virtual address downwards to the given alignment.
123130
///
124131
/// See the `align_down` function for more information.
132+
#[inline]
125133
pub fn align_down<U>(self, align: U) -> Self
126134
where
127135
U: Into<u64>,
@@ -130,6 +138,7 @@ impl VirtAddr {
130138
}
131139

132140
/// Checks whether the virtual address has the demanded alignment.
141+
#[inline]
133142
pub fn is_aligned<U>(self, align: U) -> bool
134143
where
135144
U: Into<u64>,
@@ -138,26 +147,31 @@ impl VirtAddr {
138147
}
139148

140149
/// Returns the 12-bit page offset of this virtual address.
150+
#[inline]
141151
pub fn page_offset(&self) -> PageOffset {
142152
PageOffset::new_truncate(self.0 as u16)
143153
}
144154

145155
/// Returns the 9-bit level 1 page table index.
156+
#[inline]
146157
pub fn p1_index(&self) -> PageTableIndex {
147158
PageTableIndex::new_truncate((self.0 >> 12) as u16)
148159
}
149160

150161
/// Returns the 9-bit level 2 page table index.
162+
#[inline]
151163
pub fn p2_index(&self) -> PageTableIndex {
152164
PageTableIndex::new_truncate((self.0 >> 12 >> 9) as u16)
153165
}
154166

155167
/// Returns the 9-bit level 3 page table index.
168+
#[inline]
156169
pub fn p3_index(&self) -> PageTableIndex {
157170
PageTableIndex::new_truncate((self.0 >> 12 >> 9 >> 9) as u16)
158171
}
159172

160173
/// Returns the 9-bit level 4 page table index.
174+
#[inline]
161175
pub fn p4_index(&self) -> PageTableIndex {
162176
PageTableIndex::new_truncate((self.0 >> 12 >> 9 >> 9 >> 9) as u16)
163177
}
@@ -252,6 +266,7 @@ impl PhysAddr {
252266
}
253267

254268
/// Creates a new physical address, throwing bits 52..64 away.
269+
#[inline]
255270
pub const fn new_truncate(addr: u64) -> PhysAddr {
256271
PhysAddr(addr % (1 << 52))
257272
}
@@ -267,18 +282,21 @@ impl PhysAddr {
267282
}
268283

269284
/// Converts the address to an `u64`.
285+
#[inline]
270286
pub fn as_u64(self) -> u64 {
271287
self.0
272288
}
273289

274290
/// Convenience method for checking if a physical address is null.
291+
#[inline]
275292
pub fn is_null(&self) -> bool {
276293
self.0 == 0
277294
}
278295

279296
/// Aligns the physical address upwards to the given alignment.
280297
///
281298
/// See the `align_up` function for more information.
299+
#[inline]
282300
pub fn align_up<U>(self, align: U) -> Self
283301
where
284302
U: Into<u64>,
@@ -289,6 +307,7 @@ impl PhysAddr {
289307
/// Aligns the physical address downwards to the given alignment.
290308
///
291309
/// See the `align_down` function for more information.
310+
#[inline]
292311
pub fn align_down<U>(self, align: U) -> Self
293312
where
294313
U: Into<u64>,
@@ -297,6 +316,7 @@ impl PhysAddr {
297316
}
298317

299318
/// Checks whether the physical address has the demanded alignment.
319+
#[inline]
300320
pub fn is_aligned<U>(self, align: U) -> bool
301321
where
302322
U: Into<u64>,
@@ -402,6 +422,7 @@ impl Sub<PhysAddr> for PhysAddr {
402422
///
403423
/// Returns the greatest x with alignment `align` so that x <= addr. The alignment must be
404424
/// a power of 2.
425+
#[inline]
405426
pub fn align_down(addr: u64, align: u64) -> u64 {
406427
assert!(align.is_power_of_two(), "`align` must be a power of two");
407428
addr & !(align - 1)

src/instructions/interrupts.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Enabling and disabling interrupts
22
33
/// Returns whether interrupts are enabled.
4+
#[inline]
45
pub fn are_enabled() -> bool {
56
use crate::registers::rflags::{self, RFlags};
67

@@ -58,6 +59,7 @@ pub fn disable() {
5859
/// });
5960
/// // interrupts are enabled again
6061
/// ```
62+
#[inline]
6163
pub fn without_interrupts<F, R>(f: F) -> R
6264
where
6365
F: FnOnce() -> R,

src/instructions/segmentation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub unsafe fn swap_gs() {
8989
}
9090

9191
/// Returns the current value of the code segment register.
92+
#[inline]
9293
pub fn cs() -> SegmentSelector {
9394
#[cfg(feature = "inline_asm")]
9495
{

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ impl PrivilegeLevel {
5656
/// Creates a `PrivilegeLevel` from a numeric value. The value must be in the range 0..4.
5757
///
5858
/// This function panics if the passed value is >3.
59+
#[inline]
5960
pub fn from_u16(value: u16) -> PrivilegeLevel {
6061
match value {
6162
0 => PrivilegeLevel::Ring0,

src/registers/control.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,13 @@ mod x86_64 {
133133

134134
impl Cr0 {
135135
/// Read the current set of CR0 flags.
136+
#[inline]
136137
pub fn read() -> Cr0Flags {
137138
Cr0Flags::from_bits_truncate(Self::read_raw())
138139
}
139140

140141
/// Read the current raw CR0 value.
142+
#[inline]
141143
pub fn read_raw() -> u64 {
142144
let value: u64;
143145

@@ -158,6 +160,7 @@ mod x86_64 {
158160
///
159161
/// Preserves the value of reserved fields. Unsafe because it's possible to violate memory
160162
/// safety by e.g. disabling paging.
163+
#[inline]
161164
pub unsafe fn write(flags: Cr0Flags) {
162165
let old_value = Self::read_raw();
163166
let reserved = old_value & !(Cr0Flags::all().bits());
@@ -170,6 +173,7 @@ mod x86_64 {
170173
///
171174
/// Does _not_ preserve any values, including reserved fields. Unsafe because it's possible to violate memory
172175
/// safety by e.g. disabling paging.
176+
#[inline]
173177
pub unsafe fn write_raw(value: u64) {
174178
#[cfg(feature = "inline_asm")]
175179
asm!("mov $0, %cr0" :: "r" (value) : "memory");
@@ -182,6 +186,7 @@ mod x86_64 {
182186
///
183187
/// Preserves the value of reserved fields. Unsafe because it's possible to violate memory
184188
/// safety by e.g. disabling paging.
189+
#[inline]
185190
pub unsafe fn update<F>(f: F)
186191
where
187192
F: FnOnce(&mut Cr0Flags),
@@ -194,6 +199,7 @@ mod x86_64 {
194199

195200
impl Cr2 {
196201
/// Read the current page fault linear address from the CR3 register.
202+
#[inline]
197203
pub fn read() -> VirtAddr {
198204
let value: u64;
199205

@@ -237,6 +243,7 @@ mod x86_64 {
237243
/// ## Safety
238244
/// Changing the level 4 page table is unsafe, because it's possible to violate memory safety by
239245
/// changing the page mapping.
246+
#[inline]
240247
pub unsafe fn write(frame: PhysFrame, flags: Cr3Flags) {
241248
let addr = frame.start_address();
242249
let value = addr.as_u64() | flags.bits();
@@ -251,11 +258,13 @@ mod x86_64 {
251258

252259
impl Cr4 {
253260
/// Read the current set of CR4 flags.
261+
#[inline]
254262
pub fn read() -> Cr4Flags {
255263
Cr4Flags::from_bits_truncate(Self::read_raw())
256264
}
257265

258266
/// Read the current raw CR4 value.
267+
#[inline]
259268
pub fn read_raw() -> u64 {
260269
let value: u64;
261270

@@ -276,6 +285,7 @@ mod x86_64 {
276285
///
277286
/// Preserves the value of reserved fields. Unsafe because it's possible to violate memory
278287
/// safety by e.g. physical address extension.
288+
#[inline]
279289
pub unsafe fn write(flags: Cr4Flags) {
280290
let old_value = Self::read_raw();
281291
let reserved = old_value & !(Cr4Flags::all().bits());
@@ -288,6 +298,7 @@ mod x86_64 {
288298
///
289299
/// Does _not_ preserve any values, including reserved fields. Unsafe because it's possible to violate memory
290300
/// safety by e.g. physical address extension.
301+
#[inline]
291302
pub unsafe fn write_raw(value: u64) {
292303
#[cfg(feature = "inline_asm")]
293304
asm!("mov $0, %cr4" :: "r" (value) : "memory");
@@ -300,6 +311,7 @@ mod x86_64 {
300311
///
301312
/// Preserves the value of reserved fields. Unsafe because it's possible to violate memory
302313
/// safety by e.g. physical address extension.
314+
#[inline]
303315
pub unsafe fn update<F>(f: F)
304316
where
305317
F: FnOnce(&mut Cr4Flags),

0 commit comments

Comments
 (0)