Skip to content

Commit

Permalink
fix(alloc): fix broken build resulting from removal of libcollections
Browse files Browse the repository at this point in the history
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
hawkw committed Jun 18, 2017
1 parent 7849c39 commit cbe8107
Show file tree
Hide file tree
Showing 21 changed files with 295 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -61,8 +61,8 @@ features = ["spin_no_std"]
path = "vga"
features = ["kinfo", "system_term"]

[dependencies.alloc]
path = "alloc"
[dependencies.sos_alloc]
path = "sos_alloc"
features = ["buddy", "system", "borrow", "buddy_as_system"]

[dependencies.clippy]
Expand Down
2 changes: 1 addition & 1 deletion Xargo.toml
@@ -1,3 +1,3 @@
[target.x86_64-sos-kernel-gnu.dependencies]
collections = {}
alloc = {}
# std = {}
2 changes: 1 addition & 1 deletion paging/Cargo.toml
Expand Up @@ -22,7 +22,7 @@ panic = "abort"
[dependencies]
util = { path = "../util" }
memory = { path = "../memory" }
alloc = { path = "../alloc" }
sos_alloc = { path = "../sos_alloc" }
elf = { path = "../elf" }
cpu = { path = "../cpu" }
params = { path = "../params" }
Expand Down
2 changes: 1 addition & 1 deletion paging/src/lib.rs
Expand Up @@ -23,7 +23,7 @@ extern crate spin;

extern crate util;
extern crate memory;
extern crate alloc;
extern crate sos_alloc as alloc;
extern crate cpu;
extern crate elf;
extern crate params;
Expand Down
2 changes: 1 addition & 1 deletion alloc/Cargo.toml → sos_alloc/Cargo.toml
@@ -1,5 +1,5 @@
[package]
name = "alloc"
name = "sos_alloc"
version = "0.1.0"
authors = ["Eliza Weisman <eliza@elizas.website>"]

Expand Down
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.
33 changes: 33 additions & 0 deletions sos_alloc/src/free.rs
@@ -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
}
}
2 changes: 1 addition & 1 deletion alloc/src/lib.rs → sos_alloc/src/lib.rs
Expand Up @@ -31,7 +31,7 @@
//! [`Layout`]: struct.Layout.html
//! [RFC 1398]: https://github.com/rust-lang/rfcs/blob/master/text/1398-kinds-of-allocators.md
//! [tracking issue]: https://github.com/rust-lang/rust/issues/32838
#![crate_name = "alloc"]
#![crate_name = "sos_alloc"]
#![crate_type = "lib"]

// The compiler needs to be instructed that this crate is an allocator in order
Expand Down
File renamed without changes.
File renamed without changes.
126 changes: 126 additions & 0 deletions sos_intrusive/src/stack/mod.rs
@@ -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
}
}
125 changes: 125 additions & 0 deletions sos_intrusive/src/stack/test.rs
@@ -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);
}


}
8 changes: 4 additions & 4 deletions src/main.rs
Expand Up @@ -30,7 +30,7 @@
, associated_consts
, type_ascription
, custom_derive )]
#![feature(collections)]
#![feature(alloc)]

#![cfg_attr(feature="clippy", feature(plugin))]
#![cfg_attr(feature="clippy", plugin(clippy))]
Expand All @@ -45,14 +45,14 @@
#[macro_use] extern crate bitflags;
#[macro_use] extern crate log;

extern crate collections;
extern crate alloc;
extern crate rlibc;
extern crate spin;

// -- SOS dependencies ------------------------------------------------------
#[macro_use] extern crate vga;

extern crate alloc;
extern crate sos_alloc;
extern crate cpu;
extern crate elf;
extern crate paging;
Expand Down Expand Up @@ -112,7 +112,7 @@ pub fn kernel_main() -> ! {
/// +---------------------------------------------------------------+
/// ```
pub fn kernel_init(params: &InitParams) {
use alloc::frame::mem_map::MemMapAllocator;
use sos_alloc::frame::mem_map::MemMapAllocator;
use ::paging::kernel_remap;

kinfoln!("Hello from the kernel!");
Expand Down

0 comments on commit cbe8107

Please sign in to comment.