1
1
use crate :: sys;
2
2
3
- use alloc:: slice:: SliceIndex ;
4
- use alloc:: sync:: Arc ;
5
- use alloc:: vec;
6
- use alloc:: vec:: Vec ;
7
3
use core:: cmp;
8
- use core:: ops:: { Index , IndexMut } ;
9
4
use linked_list_allocator:: LockedHeap ;
10
- use spin:: Mutex ;
11
5
use x86_64:: structures:: paging:: {
12
6
mapper:: MapToError , page:: PageRangeInclusive ,
13
7
FrameAllocator , Mapper , OffsetPageTable , Page , PageTableFlags , Size4KiB ,
@@ -19,18 +13,18 @@ static ALLOCATOR: LockedHeap = LockedHeap::empty();
19
13
20
14
pub const HEAP_START : u64 = 0x4444_4444_0000 ;
21
15
22
- fn max_memory ( ) -> u64 {
16
+ fn max_memory ( ) -> usize {
23
17
// Default to 32 MB
24
- option_env ! ( "MOROS_MEMORY" ) . unwrap_or ( "32" ) . parse :: < u64 > ( ) . unwrap ( ) << 20
18
+ option_env ! ( "MOROS_MEMORY" ) . unwrap_or ( "32" ) . parse :: < usize > ( ) . unwrap ( ) << 20
25
19
}
26
20
27
21
pub fn init_heap ( ) -> Result < ( ) , MapToError < Size4KiB > > {
28
- let mapper = sys :: mem :: mapper ( ) ;
29
- let mut frame_allocator = sys :: mem :: frame_allocator ( ) ;
22
+ let mapper = super :: mapper ( ) ;
23
+ let mut frame_allocator = super :: frame_allocator ( ) ;
30
24
31
25
// Use half of the memory for the heap caped to 16 MB by default
32
26
// because the allocator is slow.
33
- let heap_size = cmp:: min ( sys :: mem :: memory_size ( ) , max_memory ( ) ) / 2 ;
27
+ let heap_size = ( cmp:: min ( super :: memory_size ( ) , max_memory ( ) ) / 2 ) as u64 ;
34
28
let heap_start = VirtAddr :: new ( HEAP_START ) ;
35
29
sys:: process:: init_process_addr ( HEAP_START + heap_size) ;
36
30
@@ -117,83 +111,15 @@ pub fn free_pages(mapper: &mut OffsetPageTable, addr: u64, size: usize) {
117
111
}
118
112
}
119
113
120
- #[ derive( Clone ) ]
121
- pub struct PhysBuf {
122
- buf : Arc < Mutex < Vec < u8 > > > ,
123
- }
124
-
125
- impl PhysBuf {
126
- pub fn new ( len : usize ) -> Self {
127
- Self :: from ( vec ! [ 0 ; len] )
128
- }
129
-
130
- // Realloc vec until it uses a chunk of contiguous physical memory
131
- fn from ( vec : Vec < u8 > ) -> Self {
132
- let buffer_end = vec. len ( ) - 1 ;
133
- let memory_end = phys_addr ( & vec[ buffer_end] ) - phys_addr ( & vec[ 0 ] ) ;
134
- if buffer_end == memory_end as usize {
135
- Self {
136
- buf : Arc :: new ( Mutex :: new ( vec) ) ,
137
- }
138
- } else {
139
- Self :: from ( vec. clone ( ) ) // Clone vec and try again
140
- }
141
- }
142
-
143
- pub fn addr ( & self ) -> u64 {
144
- phys_addr ( & self . buf . lock ( ) [ 0 ] )
145
- }
146
- }
147
-
148
- pub fn phys_addr ( ptr : * const u8 ) -> u64 {
149
- let virt_addr = VirtAddr :: new ( ptr as u64 ) ;
150
- let phys_addr = sys:: mem:: virt_to_phys ( virt_addr) . unwrap ( ) ;
151
- phys_addr. as_u64 ( )
152
- }
153
-
154
- impl < I : SliceIndex < [ u8 ] > > Index < I > for PhysBuf {
155
- type Output = I :: Output ;
156
-
157
- #[ inline]
158
- fn index ( & self , index : I ) -> & Self :: Output {
159
- Index :: index ( & * * self , index)
160
- }
161
- }
162
-
163
- impl < I : SliceIndex < [ u8 ] > > IndexMut < I > for PhysBuf {
164
- #[ inline]
165
- fn index_mut ( & mut self , index : I ) -> & mut Self :: Output {
166
- IndexMut :: index_mut ( & mut * * self , index)
167
- }
168
- }
169
-
170
- impl core:: ops:: Deref for PhysBuf {
171
- type Target = [ u8 ] ;
172
-
173
- fn deref ( & self ) -> & [ u8 ] {
174
- let vec = self . buf . lock ( ) ;
175
- unsafe { alloc:: slice:: from_raw_parts ( vec. as_ptr ( ) , vec. len ( ) ) }
176
- }
177
- }
178
-
179
- impl core:: ops:: DerefMut for PhysBuf {
180
- fn deref_mut ( & mut self ) -> & mut [ u8 ] {
181
- let mut vec = self . buf . lock ( ) ;
182
- unsafe {
183
- alloc:: slice:: from_raw_parts_mut ( vec. as_mut_ptr ( ) , vec. len ( ) )
184
- }
185
- }
186
- }
187
-
188
- pub fn memory_size ( ) -> usize {
114
+ pub fn heap_size ( ) -> usize {
189
115
ALLOCATOR . lock ( ) . size ( )
190
116
}
191
117
192
- pub fn memory_used ( ) -> usize {
118
+ pub fn heap_used ( ) -> usize {
193
119
ALLOCATOR . lock ( ) . used ( )
194
120
}
195
121
196
- pub fn memory_free ( ) -> usize {
122
+ pub fn heap_free ( ) -> usize {
197
123
ALLOCATOR . lock ( ) . free ( )
198
124
}
199
125
0 commit comments