@@ -2,25 +2,57 @@ use std::prelude::v1::*;
2
2
use std:: mem:: { size_of, align_of} ;
3
3
use super :: * ;
4
4
5
- fn new_heap ( ) -> Heap {
5
+ struct TestHeap {
6
+ heap : Heap ,
7
+ bottom : usize ,
8
+ size : usize
9
+ }
10
+
11
+ impl TestHeap {
12
+ fn top ( & self ) -> usize {
13
+ self . bottom + self . size
14
+ }
15
+
16
+ pub fn size ( & self ) -> usize {
17
+ self . size
18
+ }
19
+
20
+ pub fn allocate_first_fit ( & mut self , mut size : usize , align : usize ) -> Option < * mut u8 > {
21
+ self . heap . allocate_first_fit ( size, align)
22
+ }
23
+
24
+ pub unsafe fn deallocate ( & mut self , ptr : * mut u8 , mut size : usize , _align : usize ) {
25
+ self . heap . deallocate ( ptr, size, _align) ;
26
+ }
27
+
28
+ pub unsafe fn extend ( & mut self , by : usize ) {
29
+ let top = self . top ( ) ;
30
+ self . heap . deallocate ( top as * mut u8 , by, 1 ) ;
31
+ self . size += by;
32
+ }
33
+ }
34
+
35
+ fn new_heap ( ) -> TestHeap {
6
36
const HEAP_SIZE : usize = 1000 ;
7
37
let heap_space = Box :: into_raw ( Box :: new ( [ 0u8 ; HEAP_SIZE ] ) ) ;
8
38
9
- let heap = unsafe { Heap :: new ( heap_space as usize , HEAP_SIZE ) } ;
10
- assert ! ( heap. bottom == heap_space as usize ) ;
11
- assert ! ( heap. size == HEAP_SIZE ) ;
12
- heap
39
+ TestHeap {
40
+ heap : unsafe { Heap :: new ( heap_space as usize , HEAP_SIZE ) } ,
41
+ bottom : heap_space as usize ,
42
+ size : HEAP_SIZE ,
43
+ }
13
44
}
14
45
15
- fn new_max_heap ( ) -> Heap {
46
+ fn new_max_heap ( ) -> TestHeap {
16
47
const HEAP_SIZE : usize = 1024 ;
17
48
const HEAP_SIZE_MAX : usize = 2048 ;
18
49
let heap_space = Box :: into_raw ( Box :: new ( [ 0u8 ; HEAP_SIZE_MAX ] ) ) ;
19
50
20
- let heap = unsafe { Heap :: new ( heap_space as usize , HEAP_SIZE ) } ;
21
- assert ! ( heap. bottom == heap_space as usize ) ;
22
- assert ! ( heap. size == HEAP_SIZE ) ;
23
- heap
51
+ TestHeap {
52
+ heap : unsafe { Heap :: new ( heap_space as usize , HEAP_SIZE ) } ,
53
+ bottom : heap_space as usize ,
54
+ size : HEAP_SIZE ,
55
+ }
24
56
}
25
57
26
58
#[ test]
@@ -45,7 +77,7 @@ fn allocate_double_usize() {
45
77
assert ! ( addr. is_some( ) ) ;
46
78
let addr = addr. unwrap ( ) as usize ;
47
79
assert ! ( addr == heap. bottom) ;
48
- let ( hole_addr, hole_size) = heap. holes . first_hole ( ) . expect ( "ERROR: no hole left" ) ;
80
+ let ( hole_addr, hole_size) = heap. heap . holes . first_hole ( ) . expect ( "ERROR: no hole left" ) ;
49
81
assert ! ( hole_addr == heap. bottom + size) ;
50
82
assert ! ( hole_size == heap. size - size) ;
51
83
0 commit comments