@@ -120,31 +120,31 @@ impl Cursor {
120
120
alloc_ptr = aligned_addr;
121
121
alloc_size = required_size;
122
122
123
- // Okay, time to move onto the back padding. Here, we are opportunistic -
124
- // if it fits, we sits. Otherwise we just skip adding the back padding, and
125
- // sort of assume that the allocation is actually a bit larger than it
126
- // actually needs to be.
127
- //
128
- // NOTE: Because we always use `HoleList::align_layout`, the size of
129
- // the new allocation is always "rounded up" to cover any partial gaps that
130
- // would have occurred. For this reason, we DON'T need to "round up"
131
- // to account for an unaligned hole spot.
132
- let hole_layout = Layout :: new :: < Hole > ( ) ;
133
- let back_padding_start = align_up ( allocation_end, hole_layout. align ( ) ) ;
134
- let back_padding_end = back_padding_start. wrapping_add ( hole_layout. size ( ) ) ;
135
-
136
- // Will the proposed new back padding actually fit in the old hole slot?
137
- back_padding = if back_padding_end <= hole_end {
138
- // Yes, it does! Place a back padding node
139
- Some ( HoleInfo {
140
- addr : back_padding_start,
141
- size : ( hole_end as usize ) - ( back_padding_start as usize ) ,
142
- } )
143
- } else {
144
- // No, it does not. We are now pretending the allocation now
145
- // holds the extra 0..size_of::<Hole>() bytes that are not
146
- // big enough to hold what SHOULD be back_padding
123
+ // Okay, time to move onto the back padding.
124
+ let back_padding_size = hole_end as usize - allocation_end as usize ;
125
+ back_padding = if back_padding_size == 0 {
147
126
None
127
+ } else {
128
+ // NOTE: Because we always use `HoleList::align_layout`, the size of
129
+ // the new allocation is always "rounded up" to cover any partial gaps that
130
+ // would have occurred. For this reason, we DON'T need to "round up"
131
+ // to account for an unaligned hole spot.
132
+ let hole_layout = Layout :: new :: < Hole > ( ) ;
133
+ let back_padding_start = align_up ( allocation_end, hole_layout. align ( ) ) ;
134
+ let back_padding_end = back_padding_start. wrapping_add ( hole_layout. size ( ) ) ;
135
+
136
+ // Will the proposed new back padding actually fit in the old hole slot?
137
+ if back_padding_end <= hole_end {
138
+ // Yes, it does! Place a back padding node
139
+ Some ( HoleInfo {
140
+ addr : back_padding_start,
141
+ size : back_padding_size,
142
+ } )
143
+ } else {
144
+ // No, it does not. We don't want to leak any heap bytes, so we
145
+ // consider this hole unsuitable for the requested allocation.
146
+ return Err ( self ) ;
147
+ }
148
148
} ;
149
149
}
150
150
@@ -697,34 +697,9 @@ fn deallocate(list: &mut HoleList, addr: *mut u8, size: usize) {
697
697
#[ cfg( test) ]
698
698
pub mod test {
699
699
use super :: HoleList ;
700
- use crate :: { align_down_size, Heap } ;
700
+ use crate :: { align_down_size, test :: new_heap } ;
701
701
use core:: mem:: size_of;
702
- use std:: { alloc:: Layout , convert:: TryInto , mem:: MaybeUninit , prelude:: v1:: * , ptr:: NonNull } ;
703
-
704
- #[ repr( align( 128 ) ) ]
705
- struct Chonk < const N : usize > {
706
- data : [ MaybeUninit < u8 > ; N ] ,
707
- }
708
-
709
- impl < const N : usize > Chonk < N > {
710
- pub fn new ( ) -> Self {
711
- Self {
712
- data : [ MaybeUninit :: uninit ( ) ; N ] ,
713
- }
714
- }
715
- }
716
-
717
- fn new_heap ( ) -> Heap {
718
- const HEAP_SIZE : usize = 1000 ;
719
- let heap_space = Box :: leak ( Box :: new ( Chonk :: < HEAP_SIZE > :: new ( ) ) ) ;
720
- let data = & mut heap_space. data ;
721
- let assumed_location = data. as_mut_ptr ( ) . cast ( ) ;
722
-
723
- let heap = Heap :: from_slice ( data) ;
724
- assert_eq ! ( heap. bottom( ) , assumed_location) ;
725
- assert_eq ! ( heap. size( ) , align_down_size( HEAP_SIZE , size_of:: <usize >( ) ) ) ;
726
- heap
727
- }
702
+ use std:: { alloc:: Layout , convert:: TryInto , prelude:: v1:: * , ptr:: NonNull } ;
728
703
729
704
#[ test]
730
705
fn cursor ( ) {
0 commit comments