1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
use crate::*;
use core::sync::atomic::{AtomicU64, Ordering};
/// A trait defining bitfield operations we need for tracking allocated objects within a page.
pub(crate) trait Bitfield {
fn initialize(&mut self, for_size: usize, capacity: usize);
fn first_fit(
&self,
base_addr: usize,
layout: Layout,
page_size: usize,
metadata_size: usize,
) -> Option<(usize, usize)>;
fn is_allocated(&self, idx: usize) -> bool;
fn set_bit(&self, idx: usize);
fn clear_bit(&self, idx: usize);
fn is_full(&self) -> bool;
fn all_free(&self, relevant_bits: usize) -> bool;
}
/// Implementation of bit operations on u64 slices.
///
/// We allow deallocations (i.e. clearing a bit in the field)
/// from any thread. That's why the bitfield is a bunch of AtomicU64.
impl Bitfield for [AtomicU64] {
/// Initialize the bitfield
///
/// # Arguments
/// * `for_size`: Object size we want to allocate
/// * `capacity`: Maximum size of the buffer the bitmap maintains.
///
/// Ensures that we only have free slots for what we can allocate
/// within the page (by marking everything else allocated).
fn initialize(&mut self, for_size: usize, capacity: usize) {
// Set everything to allocated
for bitmap in self.iter_mut() {
*bitmap = AtomicU64::new(u64::max_value());
}
// Mark actual slots as free
let relevant_bits = core::cmp::min(capacity / for_size, self.len() * 64);
for idx in 0..relevant_bits {
self.clear_bit(idx);
}
}
/// Tries to find a free block of memory that satisfies `alignment` requirement.
///
/// # Notes
/// * We pass size here to be able to calculate the resulting address within `data`.
fn first_fit(
&self,
base_addr: usize,
layout: Layout,
page_size: usize,
metadata_size: usize
) -> Option<(usize, usize)> {
for (base_idx, b) in self.iter().enumerate() {
let bitval = b.load(Ordering::Relaxed);
if bitval == u64::max_value() {
continue;
} else {
let negated = !bitval;
let first_free = negated.trailing_zeros() as usize;
let idx: usize = base_idx * 64 + first_free;
let offset = idx * layout.size();
// TODO(bad): psize needs to be passed as arg
let offset_inside_data_area = offset <= (page_size - metadata_size - layout.size());
if !offset_inside_data_area {
return None;
}
let addr: usize = base_addr + offset;
let alignment_ok = addr % layout.align() == 0;
let block_is_free = bitval & (1 << first_free) == 0;
if alignment_ok && block_is_free {
return Some((idx, addr));
}
}
}
None
}
/// Check if the bit `idx` is set.
#[inline(always)]
fn is_allocated(&self, idx: usize) -> bool {
let base_idx = idx / 64;
let bit_idx = idx % 64;
(self[base_idx].load(Ordering::Relaxed) & (1 << bit_idx)) > 0
}
/// Sets the bit number `idx` in the bit-field.
#[inline(always)]
fn set_bit(&self, idx: usize) {
let base_idx = idx / 64;
let bit_idx = idx % 64;
self[base_idx].fetch_or(1 << bit_idx, Ordering::Relaxed);
}
/// Clears bit number `idx` in the bit-field.
#[inline(always)]
fn clear_bit(&self, idx: usize) {
let base_idx = idx / 64;
let bit_idx = idx % 64;
self[base_idx].fetch_and(!(1 << bit_idx), Ordering::Relaxed);
}
/// Checks if we could allocate more objects of a given `alloc_size` within the
/// `capacity` of the memory allocator.
///
/// # Note
/// The ObjectPage will make sure to mark the top-most bits as allocated
/// for large sizes (i.e., a size 512 SCAllocator will only really need 3 bits)
/// to track allocated objects). That's why this function can be simpler
/// than it would need to be in practice.
#[inline(always)]
fn is_full(&self) -> bool {
self.iter()
.filter(|&x| x.load(Ordering::Relaxed) != u64::max_value())
.count()
== 0
}
/// Checks if the page has currently no allocations.
///
/// This is called `all_free` rather than `is_emtpy` because
/// we already have an is_empty fn as part of the slice.
fn all_free(&self, relevant_bits: usize) -> bool {
for (idx, bitmap) in self.iter().enumerate() {
let checking_bit_range = (idx * 64, (idx + 1) * 64);
if relevant_bits >= checking_bit_range.0 && relevant_bits < checking_bit_range.1 {
// Last relevant bitmap, here we only have to check that a subset of bitmap is marked free
// the rest will be marked full
let bits_that_should_be_free = relevant_bits - checking_bit_range.0;
let free_mask = (1 << bits_that_should_be_free) - 1;
return (free_mask & bitmap.load(Ordering::Relaxed)) == 0;
}
if bitmap.load(Ordering::Relaxed) == 0 {
continue;
} else {
return false;
}
}
true
}
}
/// This trait is used to define a page from which objects are allocated
/// in an `SCAllocator`.
///
/// The implementor of this trait needs to provide access to the page meta-data,
/// which consists of:
/// - `MappedPages8k` object that owns this memory
/// - A bitfield (to track allocations),
/// - `prev` and `next` pointers to insert the page in free lists
pub trait AllocablePage {
/// The total size (in bytes) of the page.
///
/// # Note
/// We also assume that the address of the page will be aligned to `SIZE`.
const SIZE: usize;
const METADATA_SIZE: usize;
const HEAP_ID_OFFSET: usize;
fn new(mp: MappedPages8k, heap_id: usize) -> Self
where
Self: core::marker::Sized;
fn retrieve_mapped_pages(&mut self) -> Option<MappedPages8k>;
fn clear_metadata(&mut self);
fn set_heap_id(&mut self, heap_id: usize);
fn heap_id(&self) -> usize;
fn bitfield(&self) -> &[AtomicU64; 8];
fn bitfield_mut(&mut self) -> &mut [AtomicU64; 8];
fn prev(&mut self) -> &mut Rawlink<Self>
where
Self: core::marker::Sized;
fn next(&mut self) -> &mut Rawlink<Self>
where
Self: core::marker::Sized;
fn buffer_size() -> usize;
/// Tries to find a free block within `data` that satisfies `alignment` requirement.
fn first_fit(&self, layout: Layout) -> Option<(usize, usize)> {
let base_addr = (self as *const Self as *const u8) as usize;
self.bitfield().first_fit(base_addr, layout, Self::SIZE, Self::METADATA_SIZE)
}
/// Tries to allocate an object within this page.
///
/// In case the slab is full, returns a null ptr.
fn allocate(&mut self, layout: Layout) -> *mut u8 {
match self.first_fit(layout) {
Some((idx, addr)) => {
self.bitfield().set_bit(idx);
addr as *mut u8
}
None => ptr::null_mut(),
}
}
/// Checks if we can still allocate more objects of a given layout within the page.
fn is_full(&self) -> bool {
self.bitfield().is_full()
}
/// Checks if the page has currently no allocations.
fn is_empty(&self, relevant_bits: usize) -> bool {
self.bitfield().all_free(relevant_bits)
}
/// Deallocates a memory object within this page.
fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) -> Result<(), &'static str> {
// trace!(
// "AllocablePage deallocating ptr = {:p} with {:?}",
// ptr,
// layout
// );
let page_offset = (ptr.as_ptr() as usize) & (Self::SIZE - 1);
assert!(page_offset % layout.size() == 0);
let idx = page_offset / layout.size();
assert!(
self.bitfield().is_allocated(idx),
"{:p} not marked allocated?",
ptr
);
self.bitfield().clear_bit(idx);
Ok(())
}
}
/// Holds allocated data within 2 4-KiB pages.
///
/// Has a data-section where objects are allocated from
/// and a small amount of meta-data in form of a bitmap
/// to track allocations at the end of the page.
///
/// # Notes
/// An object of this type will be exactly 8 KiB.
/// It is marked `repr(C)` because we rely on a well defined order of struct
/// members (e.g., dealloc does a cast to find the bitfield).
#[repr(C)]
pub struct ObjectPage8k<'a> {
/// Holds memory objects.
#[allow(dead_code)]
data: [u8; ObjectPage8k::SIZE - ObjectPage8k::METADATA_SIZE],
/// The MappedPages this memory area belongs to
pub mp: Option<MappedPages8k>,
pub heap_id: usize,
/// Next element in list (used by `PageList`).
next: Rawlink<ObjectPage8k<'a>>,
/// Previous element in list (used by `PageList`)
prev: Rawlink<ObjectPage8k<'a>>,
/// A bit-field to track free/allocated memory within `data`.
pub(crate) bitfield: [AtomicU64; 8],
}
// These needs some more work to be really safe...
unsafe impl<'a> Send for ObjectPage8k<'a> {}
unsafe impl<'a> Sync for ObjectPage8k<'a> {}
impl<'a> AllocablePage for ObjectPage8k<'a> {
const SIZE: usize = 8192;
const METADATA_SIZE: usize = core::mem::size_of::<Option<MappedPages8k>>() + core::mem::size_of::<usize>() + (2*core::mem::size_of::<Rawlink<ObjectPage8k<'a>>>()) + (8*8);
const HEAP_ID_OFFSET: usize = Self::SIZE - (core::mem::size_of::<usize>() + (2*core::mem::size_of::<Rawlink<ObjectPage8k<'a>>>()) + (8*8));
/// Creates a new 8KiB allocable page and stores the MappedPages object in the metadata portion.
fn new(mp: MappedPages8k, heap_id: usize) -> ObjectPage8k<'a> {
ObjectPage8k {
data: [0; ObjectPage8k::SIZE -ObjectPage8k::METADATA_SIZE],
mp: Some(mp),
heap_id,
next: Rawlink::default(),
prev: Rawlink::default(),
bitfield: [AtomicU64::new(0), AtomicU64::new(0), AtomicU64::new(0), AtomicU64::new(0), AtomicU64::new(0), AtomicU64::new(0), AtomicU64::new(0),AtomicU64::new(0) ],
}
}
/// Returns the MappedPages object that was stored in the metadata portion of the page.
fn retrieve_mapped_pages(&mut self) -> Option<MappedPages8k> {
let mut mp = None;
core::mem::swap(&mut self.mp, &mut mp);
mp
}
/// clears the metadata section of the page
fn clear_metadata(&mut self) {
self.heap_id = 0;
self.next = Rawlink::default();
self.prev = Rawlink::default();
for bf in &self.bitfield {
bf.store(0, Ordering::SeqCst);
}
}
fn set_heap_id(&mut self, heap_id: usize){
self.heap_id = heap_id;
}
fn heap_id(&self) -> usize {
self.heap_id
}
fn bitfield(&self) -> &[AtomicU64; 8] {
&self.bitfield
}
fn bitfield_mut(&mut self) -> &mut [AtomicU64; 8] {
&mut self.bitfield
}
fn prev(&mut self) -> &mut Rawlink<Self> {
&mut self.prev
}
fn next(&mut self) -> &mut Rawlink<Self> {
&mut self.next
}
fn buffer_size() -> usize {
ObjectPage8k::SIZE - ObjectPage8k::METADATA_SIZE
}
}
impl<'a> Default for ObjectPage8k<'a> {
fn default() -> ObjectPage8k<'a> {
unsafe { mem::MaybeUninit::zeroed().assume_init() }
}
}
impl<'a> fmt::Debug for ObjectPage8k<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ObjectPage8k")
}
}
/// A wrapper type around MappedPages which ensures that the MappedPages
/// have a size and alignment of 8 KiB and are writable.
pub struct MappedPages8k(MappedPages);
impl MappedPages8k {
pub const SIZE: usize = ObjectPage8k::SIZE;
pub const BUFFER_SIZE: usize = ObjectPage8k::SIZE - ObjectPage8k::METADATA_SIZE;
pub const METADATA_SIZE: usize = ObjectPage8k::METADATA_SIZE;
pub const HEAP_ID_OFFSET: usize = ObjectPage8k::HEAP_ID_OFFSET;
/// Creates a MappedPages8k object from MappedPages that have a size and alignment of 8 KiB and are writable.
pub fn new(mp: MappedPages) -> Result<MappedPages8k, &'static str> {
let vaddr = mp.start_address().value();
// check that the mapped pages are aligned to 8k
if vaddr % Self::SIZE != 0 {
error!("Trying to create a MappedPages8k but MappedPages were not aligned at 8k bytes");
return Err("Trying to create a MappedPages8k but MappedPages were not aligned at 8k bytes");
}
// check that the mapped pages is writable
if !mp.flags().is_writable() {
error!("Trying to create a MappedPages8k but MappedPages were not writable (flags: {:?})", mp.flags());
return Err("Trying to create a MappedPages8k but MappedPages were not writable");
}
// check that the mapped pages size is equal in size to the page
if Self::SIZE != mp.size_in_bytes() {
error!("Trying to create a MappedPages8k but MappedPages were not 8 KiB (size: {} bytes)", mp.size_in_bytes());
return Err("Trying to create a MappedPages8k but MappedPages were not 8 KiB");
}
let mut mp_8k = MappedPages8k(mp);
mp_8k.as_objectpage8k_mut().clear_metadata();
Ok(mp_8k)
}
// /// Return the pages represented by the MappedPages8k as an ObjectPage8k reference
// fn as_objectpage8k(&self) -> &ObjectPage8k {
// // SAFE: we guarantee the size and lifetime are within that of this MappedPages object
// unsafe {
// mem::transmute(self.0.start_address())
// }
// }
/// Return the pages represented by the MappedPages8k as a mutable ObjectPage8k reference
fn as_objectpage8k_mut(&mut self) -> &mut ObjectPage8k {
// SAFE: we guarantee the size and lifetime are within that of this MappedPages object
unsafe {
mem::transmute(self.0.start_address())
}
}
pub fn start_address(&self) -> VirtualAddress {
self.0.start_address()
}
}
/// A list of pages.
pub(crate) struct PageList<'a, T: AllocablePage> {
/// Points to the head of the list.
pub(crate) head: Option<&'a mut T>,
/// Number of elements in the list.
pub(crate) elements: usize,
}
impl<'a, T: AllocablePage> PageList<'a, T> {
#[cfg(feature = "unstable")]
pub(crate) const fn new() -> PageList<'a, T> {
PageList {
head: None,
elements: 0,
}
}
#[cfg(not(feature = "unstable"))]
pub(crate) fn new() -> PageList<'a, T> {
PageList {
head: None,
elements: 0,
}
}
pub(crate) fn iter_mut<'b: 'a>(&mut self) -> ObjectPageIterMut<'b, T> {
let m = match self.head {
None => Rawlink::none(),
Some(ref mut m) => Rawlink::some(*m),
};
ObjectPageIterMut {
head: m,
phantom: core::marker::PhantomData,
}
}
/// Inserts `new_head` at the front of the list.
pub(crate) fn insert_front<'b>(&'b mut self, mut new_head: &'a mut T) {
match self.head {
None => {
*new_head.prev() = Rawlink::none();
self.head = Some(new_head);
}
Some(ref mut head) => {
*new_head.prev() = Rawlink::none();
*head.prev() = Rawlink::some(new_head);
mem::swap(head, &mut new_head);
*head.next() = Rawlink::some(new_head);
}
}
self.elements += 1;
}
/// Removes `slab_page` from the list.
pub(crate) fn remove_from_list(&mut self, slab_page: &mut T) {
unsafe {
match slab_page.prev().resolve_mut() {
None => {
self.head = slab_page.next().resolve_mut();
}
Some(prev) => {
*prev.next() = match slab_page.next().resolve_mut() {
None => Rawlink::none(),
Some(next) => Rawlink::some(next),
};
}
}
match slab_page.next().resolve_mut() {
None => (),
Some(next) => {
*next.prev() = match slab_page.prev().resolve_mut() {
None => Rawlink::none(),
Some(prev) => Rawlink::some(prev),
};
}
}
}
*slab_page.prev() = Rawlink::none();
*slab_page.next() = Rawlink::none();
self.elements -= 1;
}
/// Removes `slab_page` from the list.
pub(crate) fn pop<'b>(&'b mut self) -> Option<&'a mut T> {
match self.head {
None => None,
Some(ref mut head) => {
let head_next = head.next();
let mut new_head = unsafe { head_next.resolve_mut() };
mem::swap(&mut self.head, &mut new_head);
let _ = self.head.as_mut().map(|n| {
*n.prev() = Rawlink::none();
});
self.elements -= 1;
new_head.map(|node| {
*node.prev() = Rawlink::none();
*node.next() = Rawlink::none();
node
})
}
}
}
/// Does the list contain `s`?
pub(crate) fn contains(&mut self, s: *const T) -> bool {
for slab_page in self.iter_mut() {
if core::ptr::eq(slab_page, s) {
return true;
}
}
false
}
}
/// Iterate over all the pages inside a slab allocator
pub(crate) struct ObjectPageIterMut<'a, P: AllocablePage> {
head: Rawlink<P>,
phantom: core::marker::PhantomData<&'a P>,
}
impl<'a, P: AllocablePage + 'a> Iterator for ObjectPageIterMut<'a, P> {
type Item = &'a mut P;
#[inline]
fn next(&mut self) -> Option<&'a mut P> {
unsafe {
self.head.resolve_mut().map(|next| {
self.head = match next.next().resolve_mut() {
None => Rawlink::none(),
Some(ref mut sp) => Rawlink::some(*sp),
};
next
})
}
}
}
/// Rawlink is a type like `Option<T>` but for holding a raw pointer.
///
/// We use it to link AllocablePages together. You probably won't need
/// to use this type if you're not implementing AllocablePage
/// for a custom page-size.
pub struct Rawlink<T> {
p: *mut T,
}
impl<T> Default for Rawlink<T> {
fn default() -> Self {
Rawlink { p: ptr::null_mut() }
}
}
impl<T> Rawlink<T> {
/// Like Option::None for Rawlink
pub(crate) fn none() -> Rawlink<T> {
Rawlink { p: ptr::null_mut() }
}
/// Like Option::Some for Rawlink
pub(crate) fn some(n: &mut T) -> Rawlink<T> {
Rawlink { p: n }
}
/// Convert the `Rawlink` into an Option value
///
/// **unsafe** because:
///
/// - Dereference of raw pointer.
/// - Returns reference of arbitrary lifetime.
#[allow(dead_code)]
pub(crate) unsafe fn resolve<'a>(&self) -> Option<&'a T> {
self.p.as_ref()
}
/// Convert the `Rawlink` into an Option value
///
/// **unsafe** because:
///
/// - Dereference of raw pointer.
/// - Returns reference of arbitrary lifetime.
pub(crate) unsafe fn resolve_mut<'a>(&mut self) -> Option<&'a mut T> {
self.p.as_mut()
}
/// Return the `Rawlink` and replace with `Rawlink::none()`
#[allow(dead_code)]
pub(crate) fn take(&mut self) -> Rawlink<T> {
mem::replace(self, Rawlink::none())
}
}