20
20
use core:: alloc;
21
21
use core:: alloc:: { GlobalAlloc , Layout } ;
22
22
23
- use crate :: utils:: sync:: Mutex ;
24
-
25
23
use super :: paging:: FRAME_ALLOCATOR ;
26
24
use super :: vmalloc;
27
25
use crate :: mem:: paging:: * ;
28
26
29
- #[ repr( C ) ]
30
- struct SlabHeader {
31
- ptr : * mut Slab ,
32
- }
33
-
34
- /// The slab is the primary unit of currency in the slab allocator.
35
- struct Slab {
36
- size : usize ,
37
- first_free : usize ,
38
- }
39
-
40
- impl Slab {
41
- const fn new ( size : usize ) -> Self {
42
- Self {
43
- size,
44
- first_free : 0 ,
45
- }
46
- }
47
-
48
- fn init ( & mut self ) {
49
- let frame: PhysFrame < Size4KiB > = FRAME_ALLOCATOR
50
- . allocate_frame ( )
51
- . expect ( "slab_init: failed to allocate frame" ) ;
52
-
53
- self . first_free = frame. start_address ( ) . as_hhdm_virt ( ) . as_u64 ( ) as usize ;
54
-
55
- let hdr_size = core:: mem:: size_of :: < SlabHeader > ( ) as u64 ;
56
- let aligned_hdr_size = align_up ( hdr_size, self . size as u64 ) as usize ;
57
-
58
- let avl_size = Size4KiB :: SIZE as usize - aligned_hdr_size;
59
-
60
- let slab_ptr = unsafe { & mut * ( self . first_free as * mut SlabHeader ) } ;
61
- slab_ptr. ptr = self as * mut Slab ;
62
-
63
- self . first_free += aligned_hdr_size;
64
-
65
- let arr_ptr = self . first_free as * mut usize ;
66
- let array = unsafe { core:: slice:: from_raw_parts_mut ( arr_ptr, avl_size) } ;
67
-
68
- // A slab is built by allocating a 4KiB page, placing the slab data at
69
- // the end, and dividing the rest into equal-size buffers:
70
- //
71
- // ------------------------------------------------------
72
- // | buffer | buffer | buffer | buffer | slab header
73
- // ------------------------------------------------------
74
- // one page
75
- let max = avl_size / self . size - 1 ;
76
- let fact = self . size / 8 ;
77
-
78
- for i in 0 ..max {
79
- unsafe {
80
- array[ i * fact] = array. as_ptr ( ) . add ( ( i + 1 ) * fact) as usize ;
81
- }
82
- }
83
-
84
- array[ max * fact] = 0 ;
85
- }
86
-
87
- fn alloc ( & mut self ) -> * mut u8 {
88
- if self . first_free == 0 {
89
- self . init ( ) ;
90
- }
91
-
92
- let old_free = self . first_free as * mut usize ;
93
-
94
- unsafe {
95
- self . first_free = * old_free;
96
- }
97
-
98
- old_free as * mut u8
99
- }
100
-
101
- fn dealloc ( & mut self , ptr : * mut u8 ) {
102
- if ptr == core:: ptr:: null_mut ( ) {
103
- panic ! ( "dealloc: attempted to free a nullptr" )
104
- }
105
-
106
- let new_head = ptr as * mut usize ;
107
-
108
- unsafe {
109
- * new_head = self . first_free ;
110
- }
111
-
112
- self . first_free = new_head as usize ;
113
- }
114
- }
115
-
116
- struct ProtectedAllocator {
117
- slabs : [ Slab ; 10 ] ,
118
- }
119
-
120
- struct Allocator {
121
- inner : Mutex < ProtectedAllocator > ,
122
- }
27
+ struct Allocator { }
123
28
124
29
impl Allocator {
125
30
const fn new ( ) -> Self {
126
- Self {
127
- inner : Mutex :: new ( ProtectedAllocator {
128
- slabs : [
129
- Slab :: new ( 8 ) ,
130
- Slab :: new ( 16 ) ,
131
- Slab :: new ( 24 ) ,
132
- Slab :: new ( 32 ) ,
133
- Slab :: new ( 48 ) ,
134
- Slab :: new ( 64 ) ,
135
- Slab :: new ( 128 ) ,
136
- Slab :: new ( 256 ) ,
137
- Slab :: new ( 512 ) ,
138
- Slab :: new ( 1024 ) ,
139
- ] ,
140
- } ) ,
141
- }
31
+ Self { }
142
32
}
143
33
144
34
fn alloc ( & self , layout : Layout ) -> * mut u8 {
145
- let mut inner = self . inner . lock_irq ( ) ;
146
-
147
- let slab = inner
148
- . slabs
149
- . iter_mut ( )
150
- . find ( |slab| slab. size >= ( 8 + layout. size ( ) ) ) ;
151
-
152
- if let Some ( slab) = slab {
153
- slab. alloc ( )
35
+ if layout. size ( ) <= 4096 {
36
+ let frame: PhysFrame < Size4KiB > = FRAME_ALLOCATOR . allocate_frame ( ) . unwrap ( ) ;
37
+ frame. start_address ( ) . as_hhdm_virt ( ) . as_mut_ptr ( )
154
38
} else {
155
- // the vmalloc allocator may require reverse dependency
156
- core:: mem:: drop ( inner) ;
157
-
158
39
let size = align_up ( layout. size ( ) as _ , Size4KiB :: SIZE ) / Size4KiB :: SIZE ;
159
40
160
41
vmalloc:: get_vmalloc ( )
@@ -172,12 +53,7 @@ impl Allocator {
172
53
return ;
173
54
}
174
55
175
- let slab_header = ( ptr as usize & !( 0xfff ) ) as * mut SlabHeader ;
176
-
177
- let slab_header = unsafe { & mut * slab_header } ;
178
- let slab = unsafe { & mut * slab_header. ptr } ;
179
-
180
- slab. dealloc ( ptr) ;
56
+ // TODO: free the slab.
181
57
}
182
58
}
183
59
0 commit comments