Skip to content

Commit 4dc8e47

Browse files
authored
Merge pull request #55 from alexcrichton/wrapping-math
Change to `wrapping_{add,sub}`
2 parents 8295084 + 0b24a23 commit 4dc8e47

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/dlmalloc.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl<A: Allocator> Dlmalloc<A> {
240240
fn align_as_chunk(&self, ptr: *mut u8) -> *mut Chunk {
241241
unsafe {
242242
let chunk = Chunk::to_mem(ptr.cast());
243-
ptr.add(self.align_offset(chunk)).cast()
243+
ptr.wrapping_add(self.align_offset(chunk)).cast()
244244
}
245245
}
246246

@@ -428,7 +428,7 @@ impl<A: Allocator> Dlmalloc<A> {
428428
} else {
429429
self.least_addr = cmp::min(tbase, self.least_addr);
430430
let mut sp: *mut Segment = &mut self.seg;
431-
while !sp.is_null() && (*sp).base != tbase.add(tsize) {
431+
while !sp.is_null() && (*sp).base != tbase.wrapping_add(tsize) {
432432
sp = (*sp).next;
433433
}
434434
if !sp.is_null() && !Segment::is_extern(sp) && Segment::sys_flags(sp) == flags {
@@ -571,15 +571,15 @@ impl<A: Allocator> Dlmalloc<A> {
571571
let newmmsize =
572572
self.mmap_align(nb + 6 * mem::size_of::<usize>() + self.malloc_alignment() - 1);
573573
let ptr = self.system_allocator.remap(
574-
oldp.cast::<u8>().sub(offset),
574+
oldp.cast::<u8>().wrapping_sub(offset),
575575
oldmmsize,
576576
newmmsize,
577577
can_move,
578578
);
579579
if ptr.is_null() {
580580
return ptr::null_mut();
581581
}
582-
let newp = ptr.add(offset).cast::<Chunk>();
582+
let newp = ptr.wrapping_add(offset).cast::<Chunk>();
583583
let psize = newmmsize - offset - self.mmap_foot_pad();
584584
(*newp).head = psize;
585585
(*Chunk::plus_offset(newp, psize)).head = Chunk::fencepost_head();
@@ -622,7 +622,7 @@ impl<A: Allocator> Dlmalloc<A> {
622622
let pos = if (br as usize - p as usize) > self.min_chunk_size() {
623623
br.cast::<u8>()
624624
} else {
625-
br.cast::<u8>().add(alignment)
625+
br.cast::<u8>().wrapping_add(alignment)
626626
};
627627
let newp = pos.cast::<Chunk>();
628628
let leadsize = pos as usize - p as usize;
@@ -670,7 +670,7 @@ impl<A: Allocator> Dlmalloc<A> {
670670
psize += prevsize + self.mmap_foot_pad();
671671
if self
672672
.system_allocator
673-
.free(p.cast::<u8>().sub(prevsize), psize)
673+
.free(p.cast::<u8>().wrapping_sub(prevsize), psize)
674674
{
675675
self.footprint -= psize;
676676
}
@@ -794,10 +794,10 @@ impl<A: Allocator> Dlmalloc<A> {
794794
let old_end = Segment::top(oldsp);
795795
let ssize = self.pad_request(mem::size_of::<Segment>());
796796
let offset = ssize + mem::size_of::<usize>() * 4 + self.malloc_alignment() - 1;
797-
let rawsp = old_end.sub(offset);
797+
let rawsp = old_end.wrapping_sub(offset);
798798
let offset = self.align_offset(Chunk::to_mem(rawsp.cast()));
799-
let asp = rawsp.add(offset);
800-
let csp = if asp < old_top.add(self.min_chunk_size()) {
799+
let asp = rawsp.wrapping_add(offset);
800+
let csp = if asp < old_top.wrapping_add(self.min_chunk_size()) {
801801
old_top
802802
} else {
803803
asp
@@ -969,13 +969,13 @@ impl<A: Allocator> Dlmalloc<A> {
969969
unsafe fn smallbin_at(&mut self, idx: u32) -> *mut Chunk {
970970
let idx = usize::try_from(idx * 2).unwrap();
971971
debug_assert!(idx < self.smallbins.len());
972-
self.smallbins.as_mut_ptr().add(idx).cast()
972+
self.smallbins.as_mut_ptr().wrapping_add(idx).cast()
973973
}
974974

975975
unsafe fn treebin_at(&mut self, idx: u32) -> *mut *mut TreeChunk {
976976
let idx = usize::try_from(idx).unwrap();
977977
debug_assert!(idx < self.treebins.len());
978-
self.treebins.as_mut_ptr().add(idx)
978+
self.treebins.as_mut_ptr().wrapping_add(idx)
979979
}
980980

981981
fn compute_tree_index(&self, size: usize) -> u32 {
@@ -1221,7 +1221,7 @@ impl<A: Allocator> Dlmalloc<A> {
12211221
psize += prevsize + self.mmap_foot_pad();
12221222
if self
12231223
.system_allocator
1224-
.free(p.cast::<u8>().sub(prevsize), psize)
1224+
.free(p.cast::<u8>().wrapping_sub(prevsize), psize)
12251225
{
12261226
self.footprint -= psize;
12271227
}
@@ -1364,8 +1364,8 @@ impl<A: Allocator> Dlmalloc<A> {
13641364
let psize = Chunk::size(p);
13651365
// We can unmap if the first chunk holds the entire segment and
13661366
// isn't pinned.
1367-
let chunk_top = p.cast::<u8>().add(psize);
1368-
let top = base.add(size - self.top_foot_size());
1367+
let chunk_top = p.cast::<u8>().wrapping_add(psize);
1368+
let top = base.wrapping_add(size - self.top_foot_size());
13691369
if !Chunk::inuse(p) && chunk_top >= top {
13701370
let tp = p.cast::<TreeChunk>();
13711371
debug_assert!(Segment::holds(sp, sp.cast()));
@@ -1727,11 +1727,11 @@ impl Chunk {
17271727
}
17281728

17291729
unsafe fn next(me: *mut Chunk) -> *mut Chunk {
1730-
me.cast::<u8>().add((*me).head & !FLAG_BITS).cast()
1730+
me.cast::<u8>().wrapping_add((*me).head & !FLAG_BITS).cast()
17311731
}
17321732

17331733
unsafe fn prev(me: *mut Chunk) -> *mut Chunk {
1734-
me.cast::<u8>().sub((*me).prev_foot).cast()
1734+
me.cast::<u8>().wrapping_sub((*me).prev_foot).cast()
17351735
}
17361736

17371737
unsafe fn cinuse(me: *mut Chunk) -> bool {
@@ -1786,23 +1786,23 @@ impl Chunk {
17861786
}
17871787

17881788
unsafe fn plus_offset(me: *mut Chunk, offset: usize) -> *mut Chunk {
1789-
me.cast::<u8>().add(offset).cast()
1789+
me.cast::<u8>().wrapping_add(offset).cast()
17901790
}
17911791

17921792
unsafe fn minus_offset(me: *mut Chunk, offset: usize) -> *mut Chunk {
1793-
me.cast::<u8>().sub(offset).cast()
1793+
me.cast::<u8>().wrapping_sub(offset).cast()
17941794
}
17951795

17961796
unsafe fn to_mem(me: *mut Chunk) -> *mut u8 {
1797-
me.cast::<u8>().add(Chunk::mem_offset())
1797+
me.cast::<u8>().wrapping_add(Chunk::mem_offset())
17981798
}
17991799

18001800
fn mem_offset() -> usize {
18011801
2 * mem::size_of::<usize>()
18021802
}
18031803

18041804
unsafe fn from_mem(mem: *mut u8) -> *mut Chunk {
1805-
mem.sub(2 * mem::size_of::<usize>()).cast()
1805+
mem.wrapping_sub(2 * mem::size_of::<usize>()).cast()
18061806
}
18071807
}
18081808

@@ -1849,7 +1849,7 @@ impl Segment {
18491849
}
18501850

18511851
unsafe fn top(seg: *mut Segment) -> *mut u8 {
1852-
(*seg).base.add((*seg).size)
1852+
(*seg).base.wrapping_add((*seg).size)
18531853
}
18541854
}
18551855

0 commit comments

Comments
 (0)