Skip to content

Commit bf6daef

Browse files
committed
[remake] PoolAllocator
FILO型にしてキャッシュヒット率を上げるのとprevエントリを削除した。
1 parent b2b965b commit bf6daef

File tree

1 file changed

+16
-41
lines changed

1 file changed

+16
-41
lines changed
Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
1-
/*
2-
* Pool Allocator
3-
* allocator for fixed size(in future, maybe able to alloc several size...)
4-
* Allow nullptr for accessing Physical Address:0
5-
*/
6-
7-
/*TODO: think about mutex*/
8-
9-
use core::marker::PhantomData;
10-
use core::mem::size_of;
1+
//!
2+
//! Pool Allocator
3+
//! An allocator for fixed size(in future, maybe able to alloc several size...)
4+
//! This allows nullptr for accessing Physical Address:0
5+
//!
116
127
pub struct PoolAllocator<T> {
138
linked_count: usize,
149
object_size: usize,
1510
head: Option<*mut FreeList>,
16-
phantom: PhantomData<T>,
11+
phantom: core::marker::PhantomData<T>,
1712
}
1813

1914
struct FreeList {
20-
prev: Option<*mut FreeList>,
2115
next: Option<*mut FreeList>,
2216
}
2317

18+
/// PoolAllocator
19+
///
20+
/// This allocator is FILO(First In Last Out) to increase the probability of cache-hit.
2421
impl<T> PoolAllocator<T> {
2522
const SIZE_CHECK_HOOK: () = Self::size_check();
2623

2724
const fn size_check() {
28-
if size_of::<T>() < size_of::<FreeList>() {
25+
if core::mem::size_of::<T>() < core::mem::size_of::<FreeList>() {
2926
panic!("PoolAllocator can process the struct bigger than FreeList only.");
3027
//static_assert
3128
}
@@ -35,24 +32,22 @@ impl<T> PoolAllocator<T> {
3532
let _c = Self::SIZE_CHECK_HOOK;
3633
Self {
3734
linked_count: 0,
38-
object_size: size_of::<T>(),
35+
object_size: core::mem::size_of::<T>(),
3936
head: None,
40-
phantom: PhantomData,
37+
phantom: core::marker::PhantomData,
4138
}
4239
}
4340

4441
pub unsafe fn set_initial_pool(&mut self, pool_address: usize, pool_size: usize) {
4542
assert_eq!(self.linked_count, 0);
4643
let mut address = pool_address;
4744
let mut prev_entry = address as *mut FreeList;
48-
(*prev_entry).prev = None;
4945
(*prev_entry).next = None;
5046
self.head = Some(prev_entry.clone());
5147
self.linked_count = 1;
5248
address += self.object_size;
5349
for _i in 1..(pool_size / self.object_size) {
5450
let entry = address as *mut FreeList;
55-
(*entry).prev = Some(prev_entry);
5651
(*entry).next = None;
5752
(*prev_entry).next = Some(entry.clone());
5853
self.linked_count += 1;
@@ -71,18 +66,11 @@ impl<T> PoolAllocator<T> {
7166

7267
pub fn alloc_ptr(&mut self) -> Result<*mut T, ()> {
7368
if self.linked_count == 0 {
74-
/*add: alloc page from manager*/
7569
return Err(());
7670
}
7771
//assert!(self.head.is_some());
7872
let e = self.head.unwrap().clone();
79-
if let Some(next) = unsafe { &mut *e }.next.clone() {
80-
unsafe { &mut *next }.prev = None;
81-
self.head = Some(next);
82-
} else {
83-
assert_eq!(self.linked_count, 1);
84-
self.head = None;
85-
}
73+
self.head = unsafe { (&mut *e).next };
8674
self.linked_count -= 1;
8775
Ok(e as usize as *mut T)
8876
}
@@ -93,23 +81,10 @@ impl<T> PoolAllocator<T> {
9381

9482
pub fn free_ptr(&mut self, target: *mut T) {
9583
/*do not use target after free */
96-
use core::usize;
97-
assert!(self.linked_count < usize::MAX);
84+
assert!(self.linked_count < core::usize::MAX);
9885
let e = target as usize as *mut FreeList;
99-
if let Some(mut first_entry) = self.head {
100-
unsafe {
101-
(*e).next = Some(first_entry);
102-
(*first_entry).prev = Some(e.clone());
103-
self.head = Some(e);
104-
}
105-
} else {
106-
assert_eq!(self.linked_count, 0);
107-
unsafe {
108-
(*e).prev = None;
109-
(*e).next = None;
110-
}
111-
self.head = Some(e);
112-
}
86+
unsafe { (&mut *e).next = self.head };
87+
self.head = Some(e);
11388
self.linked_count += 1;
11489
}
11590
}

0 commit comments

Comments
 (0)