File tree 2 files changed +25
-2
lines changed
2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -88,10 +88,18 @@ impl<const ORDER: usize> Heap<ORDER> {
88
88
89
89
while current_start + size_of :: < usize > ( ) <= end {
90
90
let lowbit = current_start & ( !current_start + 1 ) ;
91
- let size = min ( lowbit, prev_power_of_two ( end - current_start) ) ;
91
+ let mut size = min ( lowbit, prev_power_of_two ( end - current_start) ) ;
92
+
93
+ // If the order of size is larger than the max order,
94
+ // split it into smaller blocks.
95
+ let mut order = size. trailing_zeros ( ) as usize ;
96
+ if order > ORDER - 1 {
97
+ order = ORDER - 1 ;
98
+ size = 1 << order;
99
+ }
92
100
total += size;
93
101
94
- self . free_list [ size . trailing_zeros ( ) as usize ] . push ( current_start as * mut usize ) ;
102
+ self . free_list [ order ] . push ( current_start as * mut usize ) ;
95
103
current_start += size;
96
104
}
97
105
Original file line number Diff line number Diff line change @@ -60,6 +60,21 @@ fn test_heap_add() {
60
60
assert ! ( addr. is_ok( ) ) ;
61
61
}
62
62
63
+ #[ test]
64
+ fn test_heap_add_large ( ) {
65
+ // Max size of block is 2^7 == 128 bytes
66
+ let mut heap = Heap :: < 8 > :: new ( ) ;
67
+ assert ! ( heap. alloc( Layout :: from_size_align( 1 , 1 ) . unwrap( ) ) . is_err( ) ) ;
68
+
69
+ // 512 bytes of space
70
+ let space: [ u8 ; 512 ] = [ 0 ; 512 ] ;
71
+ unsafe {
72
+ heap. add_to_heap ( space. as_ptr ( ) as usize , space. as_ptr ( ) . add ( 100 ) as usize ) ;
73
+ }
74
+ let addr = heap. alloc ( Layout :: from_size_align ( 1 , 1 ) . unwrap ( ) ) ;
75
+ assert ! ( addr. is_ok( ) ) ;
76
+ }
77
+
63
78
#[ test]
64
79
fn test_heap_oom ( ) {
65
80
let mut heap = Heap :: < 32 > :: new ( ) ;
You can’t perform that action at this time.
0 commit comments