Skip to content

Commit fe583a7

Browse files
committed
wip
1 parent c4e5db8 commit fe583a7

File tree

11 files changed

+77
-37
lines changed

11 files changed

+77
-37
lines changed

buddy/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@ struct BuddyMutator {
5252
impl BuddyMutator {
5353
const fn new() -> Self {
5454
Self {
55-
freelist: FreeListAllocator::new(
56-
Lazy::new(|| &Self::plan().freelist_space),
57-
FREELIST_SPACE,
58-
),
55+
freelist: FreeListAllocator::new::<FREELIST_SPACE>(Lazy::new(|| {
56+
&Self::plan().freelist_space
57+
})),
5958
los: LargeObjectAllocator::new(Lazy::new(|| &Self::plan().large_object_space)),
6059
}
6160
}

mallockit/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#![feature(const_for)]
2424

2525
extern crate mallockit_proc_macro;
26+
pub extern crate spin;
2627

2728
#[macro_use]
2829
pub mod log;

mallockit/src/malloc.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::Plan;
2-
use crate::space::SpaceId;
2+
use crate::util::heap::HEAP;
33
use crate::util::Address;
44
use crate::util::Lazy;
55
use crate::Mutator;
@@ -34,16 +34,12 @@ impl<P: Plan> MallocAPI<P> {
3434
Self(plan)
3535
}
3636

37-
pub const fn new_mutator() -> P::Mutator {
38-
P::Mutator::NEW
39-
}
40-
4137
pub fn mutator(&self) -> &'static mut P::Mutator {
4238
P::Mutator::current()
4339
}
4440

45-
pub fn zero_spaceid(a: Address) -> bool {
46-
SpaceId::from(a).is_invalid()
41+
pub fn is_in_mallockit_heap(a: Address) -> bool {
42+
HEAP.contains(a)
4743
}
4844

4945
pub const fn align_up(value: usize, align: usize) -> usize {
@@ -58,7 +54,7 @@ impl<P: Plan> MallocAPI<P> {
5854
pub unsafe fn malloc_size(&self, ptr: Address) -> usize {
5955
let ptr = Address::from(ptr);
6056
#[cfg(target_os = "macos")]
61-
if unlikely(Self::zero_spaceid(ptr.into())) {
57+
if unlikely(Self::is_in_mallockit_heap(ptr.into())) {
6258
return crate::util::macos_malloc_zone::external_memory_size(ptr);
6359
}
6460
P::get_layout(ptr).size()
@@ -89,7 +85,7 @@ impl<P: Plan> MallocAPI<P> {
8985
return;
9086
}
9187
#[cfg(target_os = "macos")]
92-
if unlikely(Self::zero_spaceid(ptr.into())) {
88+
if unlikely(Self::is_in_mallockit_heap(ptr.into())) {
9389
return;
9490
}
9591
self.mutator().dealloc(ptr.into());
@@ -112,7 +108,7 @@ impl<P: Plan> MallocAPI<P> {
112108
let new_size = Self::align_up(new_size, Self::MIN_ALIGNMENT);
113109

114110
#[cfg(target_os = "macos")]
115-
if unlikely(Self::zero_spaceid(ptr.into())) {
111+
if unlikely(Self::is_in_mallockit_heap(ptr.into())) {
116112
let ptr = Address::from(ptr);
117113
let old_size = crate::util::macos_malloc_zone::external_memory_size(ptr);
118114
let new_layout =

mallockit/src/mutator.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::util::Address;
88

99
pub trait Mutator: Sized + 'static + TLS {
1010
type Plan: Plan<Mutator = Self>;
11+
1112
const NEW: Self;
1213

1314
fn current() -> &'static mut Self {

mallockit/src/space/freelist_space.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::{page_resource::BlockPageResource, Allocator, Space, SpaceId};
22
use crate::util::bits::{BitField, BitFieldSlot};
33
use crate::util::freelist::intrusive_freelist::AddressSpaceConfig;
44
use crate::util::freelist::intrusive_freelist::IntrusiveFreeList;
5+
use crate::util::heap::HEAP;
56
use crate::util::*;
67
use spin::Mutex;
78
use std::{ops::Range, sync::atomic::AtomicUsize};
@@ -122,14 +123,16 @@ impl Cell {
122123

123124
pub struct FreeListAllocator {
124125
space: Lazy<&'static FreeListSpace, Local>,
125-
freelist: IntrusiveFreeList<AddressSpace>,
126+
freelist: Lazy<IntrusiveFreeList<AddressSpace>, Local>,
126127
}
127128

128129
impl FreeListAllocator {
129-
pub const fn new(space: Lazy<&'static FreeListSpace, Local>, space_id: SpaceId) -> Self {
130+
pub const fn new<const SPACE_ID: SpaceId>(space: Lazy<&'static FreeListSpace, Local>) -> Self {
130131
Self {
131132
space,
132-
freelist: IntrusiveFreeList::new(false, space_id.address_space().start),
133+
freelist: Lazy::new(|| {
134+
IntrusiveFreeList::new(false, HEAP.get_space_range(SPACE_ID).start)
135+
}),
133136
}
134137
}
135138

mallockit/src/space/mod.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ pub mod large_object_space;
88
pub mod meta;
99
pub mod page_resource;
1010
pub(crate) mod page_table;
11+
use std::marker::ConstParamTy;
1112

1213
#[repr(transparent)]
13-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14-
pub struct SpaceId(u8);
14+
#[derive(Debug, Clone, Copy, PartialEq, Eq, ConstParamTy)]
15+
pub struct SpaceId(pub(crate) u8);
1516

1617
impl SpaceId {
17-
pub(crate) const HEAP_START: Address = Address(1usize << 45);
18-
pub const LOG_MAX_SPACE_SIZE: usize = 41;
18+
pub const LOG_MAX_SPACE_SIZE: usize = 40;
1919
pub(crate) const SHIFT: usize = Self::LOG_MAX_SPACE_SIZE;
2020
pub(crate) const MASK: usize = 0b1111 << Self::SHIFT;
2121

@@ -32,10 +32,6 @@ impl SpaceId {
3232
}
3333
}
3434

35-
pub const fn is_invalid(&self) -> bool {
36-
self.0 == 0 || self.0 == 15
37-
}
38-
3935
pub fn from(addr: Address) -> Self {
4036
let id = (usize::from(addr) & Self::MASK) >> Self::SHIFT;
4137
Self(id as u8)
@@ -44,12 +40,6 @@ impl SpaceId {
4440
pub fn contains(&self, addr: Address) -> bool {
4541
Self::from(addr).0 == self.0
4642
}
47-
48-
pub const fn address_space(&self) -> Range<Address> {
49-
let start = Address(Self::HEAP_START.0 + ((self.0 as usize) << Self::LOG_MAX_SPACE_SIZE));
50-
let end = Address(start.0 + (1usize << Self::LOG_MAX_SPACE_SIZE));
51-
start..end
52-
}
5343
}
5444

5545
pub trait Space: Sized + 'static {

mallockit/src/space/page_resource/block_page_resource.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::super::SpaceId;
22
use super::PageResource;
3+
use crate::util::heap::HEAP;
34
use crate::util::memory::RawMemory;
45
use crate::util::*;
56
use atomic::Atomic;
@@ -23,11 +24,12 @@ impl BlockPageResource {
2324
pub fn new(id: SpaceId, log_bytes: usize) -> Self {
2425
debug_assert!(id.0 < 0b0000_1111);
2526
debug_assert!(log_bytes >= Size4K::LOG_BYTES);
27+
let range = HEAP.get_space_range(id);
2628
Self {
2729
id,
2830
log_bytes,
29-
cursor: Atomic::new(id.address_space().start),
30-
highwater: id.address_space().end,
31+
cursor: Atomic::new(range.start),
32+
highwater: range.end,
3133
recycled_blocks: SegQueue::new(),
3234
reserved_bytes: AtomicUsize::new(0),
3335
}

mallockit/src/space/page_resource/freelist_page_resource.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::super::SpaceId;
22
use super::PageResource;
33
use crate::util::freelist::page_freelist::PageFreeList;
4+
use crate::util::heap::HEAP;
45
use crate::util::memory::RawMemory;
56
use crate::util::*;
67
use spin::mutex::Mutex;
@@ -21,19 +22,22 @@ pub struct FreelistPageResource {
2122
freelist: Mutex<PageFreeList<{ NUM_SIZE_CLASS }>, Yield>,
2223
reserved_bytes: AtomicUsize,
2324
meta: RwLock<Vec<AtomicU32>, Yield>,
25+
base: Address,
2426
}
2527

2628
impl FreelistPageResource {
2729
pub fn new(id: SpaceId) -> Self {
2830
debug_assert!(id.0 < 0b0000_1111);
29-
let base = id.address_space().start;
31+
let range = HEAP.get_space_range(id);
32+
let base = range.start;
3033
let mut freelist = PageFreeList::new(base);
3134
freelist.release_cell(base, 1 << (NUM_SIZE_CLASS - 1));
3235
Self {
3336
id,
3437
freelist: Mutex::new(freelist),
3538
reserved_bytes: AtomicUsize::new(0),
3639
meta: RwLock::new(unsafe { std::mem::transmute(vec![0u32; 1 << 20]) }),
40+
base,
3741
}
3842
}
3943

@@ -63,7 +67,7 @@ impl FreelistPageResource {
6367

6468
fn set_meta<S: PageSize>(&self, start: Page<S>, pages: usize) {
6569
debug_assert!(pages <= u32::MAX as usize);
66-
let index = (start.start() - self.id.address_space().start) >> Page::<Size4K>::LOG_BYTES;
70+
let index = (start.start() - self.base) >> Page::<Size4K>::LOG_BYTES;
6771
let meta = self.meta.upgradeable_read();
6872
if unlikely(index >= meta.len()) {
6973
let mut meta = meta.upgrade();
@@ -76,7 +80,7 @@ impl FreelistPageResource {
7680
}
7781

7882
fn get_meta<S: PageSize>(&self, start: Page<S>) -> usize {
79-
let index = (start.start() - self.id.address_space().start) >> Page::<Size4K>::LOG_BYTES;
83+
let index = (start.start() - self.base) >> Page::<Size4K>::LOG_BYTES;
8084
self.meta.read()[index].load(Ordering::Relaxed) as _
8185
}
8286
}

mallockit/src/util/heap.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use std::ops::Range;
2+
3+
use spin::Lazy;
4+
5+
use crate::space::SpaceId;
6+
7+
use super::{memory::RawMemory, Address};
8+
9+
const LOG_HEAP_SIZE: usize = 45;
10+
const HEAP_SIZE: usize = 1 << LOG_HEAP_SIZE;
11+
12+
pub static HEAP: Lazy<Heap> = Lazy::new(|| Heap::new());
13+
14+
pub struct Heap {
15+
pub(crate) start: Address,
16+
pub(crate) end: Address,
17+
}
18+
19+
impl Heap {
20+
fn new() -> Self {
21+
let start = RawMemory::map_anonymous(HEAP_SIZE).unwrap();
22+
let end = start + HEAP_SIZE;
23+
Self { start, end }
24+
}
25+
26+
pub fn contains(&self, ptr: Address) -> bool {
27+
self.start <= ptr && ptr < self.end
28+
}
29+
30+
pub fn start(&self) -> Address {
31+
self.start
32+
}
33+
34+
pub fn end(&self) -> Address {
35+
self.end
36+
}
37+
38+
pub fn get_space_range(&self, id: SpaceId) -> Range<Address> {
39+
let start = self.start + ((id.0 as usize) << SpaceId::LOG_MAX_SPACE_SIZE);
40+
let end = start + (1usize << SpaceId::LOG_MAX_SPACE_SIZE);
41+
start..end
42+
}
43+
}

mallockit/src/util/lazy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl ThreadLocality for Shared {
3131
}
3232
}
3333

34-
pub struct Lazy<T, TL: ThreadLocality = Shared, F: FnOnce() -> T = fn() -> T> {
34+
pub struct Lazy<T, TL: ThreadLocality = Shared, F = fn() -> T> {
3535
state: AtomicU8,
3636
value: UnsafeCell<MaybeUninit<T>>,
3737
init: Cell<Option<F>>,

mallockit/src/util/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub mod arena;
2525
pub mod bits;
2626
pub mod discrete_tlab;
2727
pub mod freelist;
28+
pub mod heap;
2829
mod layout_utils;
2930
mod lazy;
3031
pub mod memory;

0 commit comments

Comments
 (0)