Skip to content

Commit 24a3824

Browse files
authored
Merge pull request #15 from fitzgen/prev_cell_raw
A couple small clean ups
2 parents e40d57f + 633ad98 commit 24a3824

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

wee_alloc/src/lib.rs

+30-28
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ extra_only! {
283283
#[repr(C)]
284284
struct CellHeader {
285285
next_cell_raw: ptr::NonNull<CellHeader>,
286-
prev_cell: *mut CellHeader,
286+
prev_cell_raw: *mut CellHeader,
287287
}
288288

289289
#[repr(C)]
@@ -415,10 +415,10 @@ impl CellHeader {
415415
}
416416

417417
fn prev_cell(&self) -> Option<*mut CellHeader> {
418-
if self.prev_cell.is_null() {
418+
if self.prev_cell_raw.is_null() {
419419
None
420420
} else {
421-
Some(self.prev_cell)
421+
Some(self.prev_cell_raw)
422422
}
423423
}
424424

@@ -460,7 +460,7 @@ impl FreeCell {
460460
// true:
461461
//
462462
// * `FreeCell::next_free_raw` (and'd with the mask) is not null.
463-
// * `FreeCell::next_free_raw` is the adjacent `CellHeader::prev_cell`.
463+
// * `FreeCell::next_free_raw` is the adjacent `CellHeader::prev_cell_raw`.
464464
//
465465
// Therefore, this free cell can be merged into a single, larger, contiguous
466466
// free cell with its previous neighbor, which is also the next cell in the
@@ -505,7 +505,7 @@ impl FreeCell {
505505
FreeCell {
506506
header: CellHeader {
507507
next_cell_raw: next_cell,
508-
prev_cell,
508+
prev_cell_raw: prev_cell,
509509
},
510510
next_free_raw: next_free,
511511
},
@@ -575,7 +575,7 @@ impl FreeCell {
575575

576576
if let Some(next) = self.header.next_cell() {
577577
unsafe {
578-
(*next).prev_cell = &mut remainder.header;
578+
(*next).prev_cell_raw = &mut remainder.header;
579579
}
580580
}
581581
self.header.next_cell_raw =
@@ -676,7 +676,7 @@ extra_only! {
676676
if let Some(cell_ref) = cell.as_ref() {
677677
assert!(cell_ref.size() >= size_of::<usize>());
678678

679-
if let Some(prev) = cell_ref.prev_cell.as_ref() {
679+
if let Some(prev) = cell_ref.prev_cell().and_then(|p| p.as_ref()) {
680680
assert!(prev.size() >= size_of::<usize>());
681681
assert!(!prev.next_cell_is_invalid());
682682
assert_eq!(prev.next_cell_unchecked(), cell, "next(prev(cell)) == cell");
@@ -686,7 +686,7 @@ extra_only! {
686686
assert!(!next.is_null());
687687
let next = &*next;
688688
assert!(next.size() >= size_of::<usize>());
689-
assert_eq!(next.prev_cell, cell, "prev(next(cell)) == cell");
689+
assert_eq!(next.prev_cell_raw, cell, "prev(next(cell)) == cell");
690690
}
691691

692692
if let Some(free) = cell_ref.as_free_cell() {
@@ -844,27 +844,29 @@ where
844844

845845
let mut current_free = &mut *current_free;
846846

847-
if policy.should_merge_adjacent_free_cells() {
848-
// Now check if this cell can merge with the next cell in the free
849-
// list. We do this after the initial allocation attempt so that we
850-
// don't merge, only to immediately split the cell again right
851-
// afterwards.
852-
while current_free.next_free_can_merge() {
853-
let prev_adjacent = current_free.header.prev_cell as *mut FreeCell;
854-
extra_assert_eq!(prev_adjacent, current_free.next_free());
855-
let prev_adjacent = &mut *prev_adjacent;
856-
857-
(*prev_adjacent).header.next_cell_raw = current_free.header.next_cell_raw;
858-
if let Some(next) = current_free.header.next_cell() {
859-
(*next).prev_cell = &mut prev_adjacent.header;
860-
}
847+
// Now check if this cell can merge with the next cell in the free
848+
// list.
849+
//
850+
// We don't re-check `policy.should_merge_adjacent_free_cells()` because
851+
// the `NEXT_FREE_CELL_CAN_MERGE` bit only gets set after checking with
852+
// the policy.
853+
while current_free.next_free_can_merge() {
854+
extra_assert!(policy.should_merge_adjacent_free_cells());
855+
856+
let prev_adjacent = current_free.header.prev_cell_raw as *mut FreeCell;
857+
extra_assert_eq!(prev_adjacent, current_free.next_free());
858+
let prev_adjacent = &mut *prev_adjacent;
859+
860+
(*prev_adjacent).header.next_cell_raw = current_free.header.next_cell_raw;
861+
if let Some(next) = current_free.header.next_cell() {
862+
(*next).prev_cell_raw = &mut prev_adjacent.header;
863+
}
861864

862-
*previous_free = prev_adjacent;
863-
current_free = prev_adjacent;
865+
*previous_free = prev_adjacent;
866+
current_free = prev_adjacent;
864867

865-
write_free_pattern(current_free, policy);
866-
assert_local_cell_invariants(&mut current_free.header);
867-
}
868+
write_free_pattern(current_free, policy);
869+
assert_local_cell_invariants(&mut current_free.header);
868870
}
869871

870872
if let Some(result) = f(previous_free, current_free) {
@@ -1055,7 +1057,7 @@ unsafe impl<'a> Alloc for &'a WeeAlloc {
10551057
{
10561058
prev.header.next_cell_raw = free.header.next_cell_raw;
10571059
if let Some(next) = free.header.next_cell() {
1058-
(*next).prev_cell = &mut prev.header;
1060+
(*next).prev_cell_raw = &mut prev.header;
10591061
}
10601062

10611063
write_free_pattern(prev, policy);

0 commit comments

Comments
 (0)