Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(alloc): fix broken build resulting from removal of
libcollections
The `collections` crate was merged with `alloc` in rust-lang/rust#42648, which necessitated a change in how we depend on the std library collections in the kernel. This PR also renames the `alloc` crate to `sos_alloc`, to avoid the namespace clash with the std library's `alloc` crate on which we now depend.
- Loading branch information
Showing
21 changed files
with
295 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[target.x86_64-sos-kernel-gnu.dependencies] | ||
collections = {} | ||
alloc = {} | ||
# std = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use core::ptr::Unique; | ||
use core::mem; | ||
|
||
use intrusive::list::{List as IList, Node}; | ||
use intrusive::rawlink::RawLink; | ||
|
||
/// A `FreeList` is a list of unique free blocks | ||
pub type List = IList<Unique<Block>, Block>; | ||
|
||
/// A free block header stores a pointer to the next and previous free blocks. | ||
/// | ||
/// A `Block` can be any size, as long as | ||
pub struct Block { next: RawLink<Block> | ||
, prev: RawLink<Block> | ||
} | ||
impl Block { | ||
#[inline] pub unsafe fn as_ptr(&self) -> *mut u8 { mem::transmute(self) } | ||
} | ||
|
||
impl Node for Block { | ||
#[inline] fn prev(&self) -> &RawLink<Block> { | ||
&self.prev | ||
} | ||
#[inline] fn next(&self) -> &RawLink<Block> { | ||
&self.next | ||
} | ||
#[inline] fn prev_mut(&mut self) -> &mut RawLink<Block> { | ||
&mut self.prev | ||
} | ||
#[inline] fn next_mut(&mut self) -> &mut RawLink<Block> { | ||
&mut self.next | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// | ||
// SOS: the Stupid Operating System | ||
// by Eliza Weisman (eliza@elizas.website) | ||
// | ||
// Copyright (c) 2017 Eliza Weisman | ||
// Released under the terms of the MIT license. See `LICENSE` in the root | ||
// directory of this repository for more information. | ||
// | ||
//! An intrusive singly-linked list implementation using `RawLink`s. | ||
//! | ||
//! An _intrusive_ list is a list structure wherein the type of element stored | ||
//! in the list holds references to other nodes. This means that we don't have | ||
//! to store a separate node data type that holds the stored elements and | ||
//! pointers to other nodes, reducing the amount of memory allocated. We can | ||
//! use intrusive lists in code that runs without the kernel memory allocator, | ||
//! like the allocator implementation itself, since each list element manages | ||
//! its own memory. | ||
use ::{RawLink, OwnedRef}; | ||
|
||
use core::marker::PhantomData; | ||
use core::iter; | ||
#[cfg(test)] mod test; | ||
|
||
|
||
/// This trait defines a node in an intrusive list. | ||
/// | ||
/// A Node must be capable of providing mutable and immutable references to | ||
/// the next node in the stack | ||
pub trait Node: Sized { | ||
fn next(&self) -> &RawLink<Self>; | ||
fn next_mut(&mut self) -> &mut RawLink<Self>; | ||
} | ||
|
||
/// The `Stack` struct is our way of interacting with an intrusive list. | ||
/// | ||
/// It stores a pointer to the head of the stack, the length of the | ||
/// list, and a `PhantomData` marker for the list's `OwnedRef` type. It | ||
/// provides the methods for pushing, popping, and indexing the list. | ||
pub struct Stack<T, N> | ||
where T: OwnedRef<N> | ||
, N: Node { | ||
head: RawLink<N> | ||
, _ty_marker: PhantomData<T> | ||
, length: usize | ||
} | ||
|
||
impl<T, N> Stack<T, N> | ||
where T: OwnedRef<N> | ||
, N: Node { | ||
|
||
/// Construct a new `Stack<T, N>` with zero elements | ||
pub const fn new() -> Self { | ||
Stack { head: RawLink::none() | ||
, _ty_marker: PhantomData | ||
, length: 0 } | ||
} | ||
|
||
/// Returns the length of the list | ||
#[inline] pub fn len(&self) -> usize { | ||
self.length | ||
} | ||
|
||
/// Borrows the first element of the list as an `Option` | ||
/// | ||
/// # Returns | ||
/// - `Some(&N)` if the list has elements | ||
/// - `None` if the list is empty. | ||
#[inline] pub fn peek(&self) -> Option<&N> { | ||
unsafe { self.head.resolve() } | ||
} | ||
|
||
|
||
/// Mutably borrows the first element of the list as an `Option` | ||
/// | ||
/// # Returns | ||
/// - `Some(&mut N)` if the list has elements | ||
/// - `None` if the list is empty. | ||
#[inline] pub fn peek_mut(&mut self) -> Option<&mut N> { | ||
unsafe { self.head.resolve_mut() } | ||
} | ||
|
||
/// Returns true if the list is empty. | ||
#[inline] pub fn is_empty(&self) -> bool { | ||
self.head.is_none() | ||
} | ||
|
||
/// Push an element to the front of the stack | ||
pub fn push(&mut self, mut item: T) { | ||
// set the pushed item to point to the head element of the stack | ||
*item.get_mut().next_mut() = self.head.take(); | ||
// then, set this node's head pointer to point to the pushed item | ||
self.head = RawLink::some(item.get_mut()); | ||
unsafe { item.take(); }; | ||
self.length += 1; | ||
} | ||
|
||
/// Removes and returns the element at the front of the list. | ||
/// | ||
/// # Returns | ||
/// - `Some(T)` containing the element at the front of the list if the | ||
/// list is not empty | ||
/// - `None` if the list is empty | ||
pub fn pop(&mut self) -> Option<T> { | ||
unsafe { | ||
self.head.take().resolve_mut() | ||
.map(|head| { | ||
if let Some(next) = head.next_mut().resolve_mut() { | ||
self.head = RawLink::some(next); | ||
} | ||
self.length -= 1; | ||
T::from_raw(head) | ||
}) | ||
} | ||
} | ||
|
||
} | ||
|
||
impl<T, N> iter::FromIterator<T> for Stack<T, N> | ||
where T: OwnedRef<N> | ||
, N: Node { | ||
fn from_iter<I: IntoIterator<Item=T>>(iterator: I) -> Self { | ||
let mut list: Self = Stack::new(); | ||
for item in iterator { list.push(item) } | ||
list | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// | ||
// SOS: the Stupid Operating System | ||
// by Eliza Weisman (eliza@elizas.website) | ||
// | ||
// Copyright (c) 2017 Eliza Weisman | ||
// Released under the terms of the MIT license. See `LICENSE` in the root | ||
// directory of this repository for more information. | ||
// | ||
|
||
use stack::Node; | ||
use rawlink::RawLink; | ||
|
||
#[derive(Debug)] | ||
pub struct NumberedNode { | ||
pub number: usize, | ||
next: RawLink<NumberedNode>, | ||
} | ||
|
||
impl NumberedNode { | ||
pub fn new(number: usize) -> Self { | ||
NumberedNode { | ||
number: number, | ||
next: RawLink::none(), | ||
} | ||
} | ||
} | ||
|
||
impl Node for NumberedNode { | ||
|
||
fn next(&self) -> &RawLink<Self> { | ||
&self.next | ||
} | ||
|
||
fn next_mut(&mut self) -> &mut RawLink<Self> { | ||
&mut self.next | ||
} | ||
} | ||
|
||
impl PartialEq for NumberedNode { | ||
fn eq(&self, rhs: &Self) -> bool { self.number == rhs.number } | ||
} | ||
|
||
mod boxed { | ||
use std::boxed::Box; | ||
|
||
use super::super::Stack; | ||
use super::*; | ||
|
||
type TestStack = Stack<Box<NumberedNode>, NumberedNode>; | ||
|
||
#[test] | ||
fn not_empty_after_push() { | ||
let mut list = TestStack::new(); | ||
|
||
assert_eq!(list.peek(), None); | ||
|
||
assert!(list.is_empty()); | ||
|
||
list.push(box NumberedNode::new(1)); | ||
|
||
assert!(!list.is_empty()); | ||
} | ||
|
||
#[test] | ||
fn contents_after_first_push() { | ||
let mut list = TestStack::new(); | ||
|
||
list.push(box NumberedNode::new(1)); | ||
|
||
assert_eq!(list.peek().unwrap().number, 1); | ||
} | ||
|
||
|
||
#[test] | ||
fn contents_after_pushes() { | ||
let mut list = TestStack::new(); | ||
|
||
list.push(box NumberedNode::new(0)); | ||
assert_eq!(list.peek().unwrap().number, 0); | ||
list.push(box NumberedNode::new(1)); | ||
|
||
assert_eq!(list.peek().unwrap().number, 1); | ||
|
||
assert!(!list.is_empty()); | ||
} | ||
|
||
#[test] | ||
fn test_pop_front() { | ||
let mut list = TestStack::new(); | ||
|
||
assert_eq!(list.peek(), None); | ||
assert!(list.is_empty()); | ||
|
||
list.push(Box::new(NumberedNode::new(4))); | ||
assert!(!list.is_empty()); | ||
assert_eq!(list.peek().unwrap().number, 4); | ||
|
||
list.push(Box::new(NumberedNode::new(3))); | ||
assert!(!list.is_empty()); | ||
assert_eq!(list.peek().unwrap().number, 3); | ||
|
||
list.push(Box::new(NumberedNode::new(2))); | ||
assert!(!list.is_empty()); | ||
assert_eq!(list.peek().unwrap().number, 2); | ||
|
||
list.push(Box::new(NumberedNode::new(1))); | ||
assert!(!list.is_empty()); | ||
assert_eq!(list.peek().unwrap().number, 1); | ||
|
||
list.push(Box::new(NumberedNode::new(0))); | ||
assert!(!list.is_empty()); | ||
assert_eq!(list.peek().unwrap().number, 0); | ||
|
||
assert_eq!(list.pop().unwrap().number, 0); | ||
assert_eq!(list.pop().unwrap().number, 1); | ||
assert_eq!(list.pop().unwrap().number, 2); | ||
assert_eq!(list.pop().unwrap().number, 3); | ||
assert_eq!(list.pop().unwrap().number, 4); | ||
|
||
assert!(list.is_empty()); | ||
assert_eq!(list.pop(), None); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters