-
Notifications
You must be signed in to change notification settings - Fork 53
Use new nightly GlobalAlloc API #11
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
use std::prelude::v1::*; | ||
use std::mem::{size_of, align_of}; | ||
use alloc::allocator::Layout; | ||
use core::alloc::Layout; | ||
use super::*; | ||
|
||
fn new_heap() -> Heap { | ||
|
@@ -46,7 +46,7 @@ fn allocate_double_usize() { | |
let layout = Layout::from_size_align(size, align_of::<usize>()); | ||
let addr = heap.allocate_first_fit(layout.unwrap()); | ||
assert!(addr.is_ok()); | ||
let addr = addr.unwrap() as usize; | ||
let addr = addr.unwrap().as_ptr() as usize; | ||
assert!(addr == heap.bottom); | ||
let (hole_addr, hole_size) = heap.holes.first_hole().expect("ERROR: no hole left"); | ||
assert!(hole_addr == heap.bottom + size); | ||
|
@@ -64,7 +64,7 @@ fn allocate_and_free_double_usize() { | |
let layout = Layout::from_size_align(size_of::<usize>() * 2, align_of::<usize>()).unwrap(); | ||
let x = heap.allocate_first_fit(layout.clone()).unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't just remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like I'll have to add asserts here, or write a function that panics when pointer is null |
||
unsafe { | ||
*(x as *mut (usize, usize)) = (0xdeafdeadbeafbabe, 0xdeafdeadbeafbabe); | ||
*(x.as_ptr() as *mut (usize, usize)) = (0xdeafdeadbeafbabe, 0xdeafdeadbeafbabe); | ||
|
||
heap.deallocate(x, layout.clone()); | ||
assert_eq!((*(heap.bottom as *const Hole)).size, heap.size); | ||
|
@@ -83,11 +83,11 @@ fn deallocate_right_before() { | |
|
||
unsafe { | ||
heap.deallocate(y, layout.clone()); | ||
assert_eq!((*(y as *const Hole)).size, layout.size()); | ||
assert_eq!((*(y.as_ptr() as *const Hole)).size, layout.size()); | ||
heap.deallocate(x, layout.clone()); | ||
assert_eq!((*(x as *const Hole)).size, layout.size() * 2); | ||
assert_eq!((*(x.as_ptr() as *const Hole)).size, layout.size() * 2); | ||
heap.deallocate(z, layout.clone()); | ||
assert_eq!((*(x as *const Hole)).size, heap.size); | ||
assert_eq!((*(x.as_ptr() as *const Hole)).size, heap.size); | ||
} | ||
} | ||
|
||
|
@@ -103,11 +103,11 @@ fn deallocate_right_behind() { | |
|
||
unsafe { | ||
heap.deallocate(x, layout.clone()); | ||
assert_eq!((*(x as *const Hole)).size, size); | ||
assert_eq!((*(x.as_ptr() as *const Hole)).size, size); | ||
heap.deallocate(y, layout.clone()); | ||
assert_eq!((*(x as *const Hole)).size, size * 2); | ||
assert_eq!((*(x.as_ptr() as *const Hole)).size, size * 2); | ||
heap.deallocate(z, layout.clone()); | ||
assert_eq!((*(x as *const Hole)).size, heap.size); | ||
assert_eq!((*(x.as_ptr() as *const Hole)).size, heap.size); | ||
} | ||
} | ||
|
||
|
@@ -124,14 +124,14 @@ fn deallocate_middle() { | |
|
||
unsafe { | ||
heap.deallocate(x, layout.clone()); | ||
assert_eq!((*(x as *const Hole)).size, size); | ||
assert_eq!((*(x.as_ptr() as *const Hole)).size, size); | ||
heap.deallocate(z, layout.clone()); | ||
assert_eq!((*(x as *const Hole)).size, size); | ||
assert_eq!((*(z as *const Hole)).size, size); | ||
assert_eq!((*(x.as_ptr() as *const Hole)).size, size); | ||
assert_eq!((*(z.as_ptr() as *const Hole)).size, size); | ||
heap.deallocate(y, layout.clone()); | ||
assert_eq!((*(x as *const Hole)).size, size * 3); | ||
assert_eq!((*(x.as_ptr() as *const Hole)).size, size * 3); | ||
heap.deallocate(a, layout.clone()); | ||
assert_eq!((*(x as *const Hole)).size, heap.size); | ||
assert_eq!((*(x.as_ptr() as *const Hole)).size, heap.size); | ||
} | ||
} | ||
|
||
|
@@ -167,9 +167,9 @@ fn allocate_multiple_sizes() { | |
|
||
let x = heap.allocate_first_fit(layout_1.clone()).unwrap(); | ||
let y = heap.allocate_first_fit(layout_2.clone()).unwrap(); | ||
assert_eq!(y as usize, x as usize + base_size * 2); | ||
assert_eq!(y.as_ptr() as usize, x.as_ptr() as usize + base_size * 2); | ||
let z = heap.allocate_first_fit(layout_3.clone()).unwrap(); | ||
assert_eq!(z as usize % (base_size * 4), 0); | ||
assert_eq!(z.as_ptr() as usize % (base_size * 4), 0); | ||
|
||
unsafe { | ||
heap.deallocate(x, layout_1.clone()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should be able to do just
self.0.lock
without the staticLOCKED_ALLOCATOR
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, Mutex lock() uses &self and returns MutexGuard, so static is not needed here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the &'a needed in impl? It works without it.