Skip to content

Commit ccfd122

Browse files
committed
Rename next_free to len and add more checks in tests
Signed-off-by: Joe Richey <[email protected]>
1 parent 6226245 commit ccfd122

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/structures/gdt.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use crate::registers::segmentation::{Segment, CS, SS};
4747
#[derive(Debug, Clone)]
4848
pub struct GlobalDescriptorTable {
4949
table: [u64; 8],
50-
next_free: usize,
50+
len: usize,
5151
}
5252

5353
impl GlobalDescriptorTable {
@@ -56,7 +56,7 @@ impl GlobalDescriptorTable {
5656
pub const fn new() -> GlobalDescriptorTable {
5757
GlobalDescriptorTable {
5858
table: [0; 8],
59-
next_free: 1,
59+
len: 1,
6060
}
6161
}
6262

@@ -68,29 +68,29 @@ impl GlobalDescriptorTable {
6868
/// * The provided slice **must not be larger than 8 items** (only up to the first 8 will be observed.)
6969
#[inline]
7070
pub const unsafe fn from_raw_slice(slice: &[u64]) -> GlobalDescriptorTable {
71-
let next_free = slice.len();
71+
let len = slice.len();
7272
let mut table = [0; 8];
7373
let mut idx = 0;
7474

7575
assert!(
76-
next_free <= 8,
76+
len <= 8,
7777
"initializing a GDT from a slice requires it to be **at most** 8 elements."
7878
);
7979

80-
while idx != next_free {
80+
while idx < len {
8181
table[idx] = slice[idx];
8282
idx += 1;
8383
}
8484

85-
GlobalDescriptorTable { table, next_free }
85+
GlobalDescriptorTable { table, len }
8686
}
8787

8888
/// Get a reference to the internal table.
8989
///
9090
/// The resulting slice may contain system descriptors, which span two `u64`s.
9191
#[inline]
9292
pub fn as_raw_slice(&self) -> &[u64] {
93-
&self.table[..self.next_free]
93+
&self.table[..self.len]
9494
}
9595

9696
/// Adds the given segment descriptor to the GDT, returning the segment selector.
@@ -101,13 +101,13 @@ impl GlobalDescriptorTable {
101101
pub fn add_entry(&mut self, entry: Descriptor) -> SegmentSelector {
102102
let index = match entry {
103103
Descriptor::UserSegment(value) => {
104-
if self.next_free > self.table.len() - 1 {
104+
if self.len > self.table.len() - 1 {
105105
panic!("GDT full")
106106
}
107107
self.push(value)
108108
}
109109
Descriptor::SystemSegment(value_low, value_high) => {
110-
if self.next_free > self.table.len() - 2 {
110+
if self.len > self.table.len() - 2 {
111111
panic!("GDT requires two free spaces to hold a SystemSegment")
112112
}
113113
let index = self.push(value_low);
@@ -165,9 +165,9 @@ impl GlobalDescriptorTable {
165165
#[inline]
166166
#[cfg_attr(feature = "const_fn", rustversion::attr(all(), const))]
167167
fn push(&mut self, value: u64) -> usize {
168-
let index = self.next_free;
168+
let index = self.len;
169169
self.table[index] = value;
170-
self.next_free += 1;
170+
self.len += 1;
171171
index
172172
}
173173

@@ -178,7 +178,7 @@ impl GlobalDescriptorTable {
178178
use core::mem::size_of;
179179
super::DescriptorTablePointer {
180180
base: crate::VirtAddr::new(self.table.as_ptr() as u64),
181-
limit: (self.next_free * size_of::<u64>() - 1) as u16,
181+
limit: (self.len * size_of::<u64>() - 1) as u16,
182182
}
183183
}
184184
}
@@ -361,6 +361,7 @@ mod tests {
361361
gdt.add_entry(Descriptor::UserSegment(DescriptorFlags::USER_CODE32.bits()));
362362
gdt.add_entry(Descriptor::user_data_segment());
363363
gdt.add_entry(Descriptor::user_code_segment());
364+
assert_eq!(gdt.len, 6);
364365
gdt
365366
}
366367

@@ -369,6 +370,7 @@ mod tests {
369370
fn make_full_gdt() -> GlobalDescriptorTable {
370371
let mut gdt = make_six_entry_gdt();
371372
gdt.add_entry(Descriptor::tss_segment(&TSS));
373+
assert_eq!(gdt.len, 8);
372374
gdt
373375
}
374376

@@ -377,7 +379,9 @@ mod tests {
377379
// Make sure we don't panic with user segments
378380
let mut gdt = make_six_entry_gdt();
379381
gdt.add_entry(Descriptor::user_data_segment());
382+
assert_eq!(gdt.len, 7);
380383
gdt.add_entry(Descriptor::user_data_segment());
384+
assert_eq!(gdt.len, 8);
381385
// Make sure we don't panic with system segments
382386
let _ = make_full_gdt();
383387
}

0 commit comments

Comments
 (0)