Skip to content

Commit

Permalink
Merge pull request #40 from sunfishcode/sunfishcode/debug-asserts
Browse files Browse the repository at this point in the history
Disable all debug assertions unless the debug feature is enabled
  • Loading branch information
alexcrichton authored Mar 1, 2024
2 parents c527a26 + 2bec38e commit 104c44c
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/dlmalloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
//
// The original source was written by Doug Lea and released to the public domain

macro_rules! debug_assert {
($($arg:tt)*) => {
if cfg!(all(feature = "debug", debug_assertions)) {
assert!($($arg)*);
}
};
}

macro_rules! debug_assert_eq {
($($arg:tt)*) => {
if cfg!(all(feature = "debug", debug_assertions)) {
assert_eq!($($arg)*);
}
};
}

use core::cmp;
use core::mem;
use core::ptr;
Expand Down Expand Up @@ -1376,7 +1392,7 @@ impl<A: Allocator> Dlmalloc<A> {
// Sanity checks

unsafe fn check_any_chunk(&self, p: *mut Chunk) {
if !cfg!(debug_assertions) {
if !cfg!(all(feature = "debug", debug_assertions)) {
return;
}
debug_assert!(
Expand All @@ -1386,7 +1402,7 @@ impl<A: Allocator> Dlmalloc<A> {
}

unsafe fn check_top_chunk(&self, p: *mut Chunk) {
if !cfg!(debug_assertions) {
if !cfg!(all(feature = "debug", debug_assertions)) {
return;
}
let sp = self.segment_holding(p.cast());
Expand All @@ -1407,7 +1423,7 @@ impl<A: Allocator> Dlmalloc<A> {
}

unsafe fn check_malloced_chunk(&self, mem: *mut u8, s: usize) {
if !cfg!(debug_assertions) {
if !cfg!(all(feature = "debug", debug_assertions)) {
return;
}
if mem.is_null() {
Expand All @@ -1433,7 +1449,7 @@ impl<A: Allocator> Dlmalloc<A> {
}

unsafe fn check_mmapped_chunk(&self, p: *mut Chunk) {
if !cfg!(debug_assertions) {
if !cfg!(all(feature = "debug", debug_assertions)) {
return;
}
let sz = Chunk::size(p);
Expand All @@ -1453,7 +1469,7 @@ impl<A: Allocator> Dlmalloc<A> {
}

unsafe fn check_free_chunk(&self, p: *mut Chunk) {
if !cfg!(debug_assertions) {
if !cfg!(all(feature = "debug", debug_assertions)) {
return;
}
let sz = Chunk::size(p);
Expand All @@ -1478,7 +1494,7 @@ impl<A: Allocator> Dlmalloc<A> {
}

unsafe fn check_malloc_state(&mut self) {
if !cfg!(debug_assertions) {
if !cfg!(all(feature = "debug", debug_assertions)) {
return;
}
for i in 0..NSMALLBINS_U32 {
Expand Down Expand Up @@ -1506,7 +1522,7 @@ impl<A: Allocator> Dlmalloc<A> {
}

unsafe fn check_smallbin(&mut self, idx: u32) {
if !cfg!(debug_assertions) {
if !cfg!(all(feature = "debug", debug_assertions)) {
return;
}
let b = self.smallbin_at(idx);
Expand All @@ -1531,7 +1547,7 @@ impl<A: Allocator> Dlmalloc<A> {
}

unsafe fn check_treebin(&mut self, idx: u32) {
if !cfg!(debug_assertions) {
if !cfg!(all(feature = "debug", debug_assertions)) {
return;
}
let t = *self.treebin_at(idx);
Expand All @@ -1545,7 +1561,7 @@ impl<A: Allocator> Dlmalloc<A> {
}

unsafe fn check_tree(&mut self, t: *mut TreeChunk) {
if !cfg!(debug_assertions) {
if !cfg!(all(feature = "debug", debug_assertions)) {
return;
}
let tc = TreeChunk::chunk(t);
Expand Down

0 comments on commit 104c44c

Please sign in to comment.