Skip to content

Commit a38cde4

Browse files
committed
chore: remove std::backtrace::Backtrace in no_std
1 parent 40d22a4 commit a38cde4

File tree

7 files changed

+124
-20
lines changed

7 files changed

+124
-20
lines changed

src/allocator/dedicated_block_allocator/mod.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![deny(unsafe_code, clippy::unwrap_used)]
2+
#[cfg(feature = "std")]
3+
use std::{backtrace::Backtrace, sync::Arc};
24

35
#[cfg(feature = "visualizer")]
46
pub(crate) mod visualizer;
@@ -16,6 +18,7 @@ pub(crate) struct DedicatedBlockAllocator {
1618
allocated: u64,
1719
/// Only used if [`crate::AllocatorDebugSettings::store_stack_traces`] is [`true`]
1820
name: Option<String>,
21+
#[cfg(feature = "std")]
1922
backtrace: Arc<Backtrace>,
2023
}
2124

@@ -25,6 +28,7 @@ impl DedicatedBlockAllocator {
2528
size,
2629
allocated: 0,
2730
name: None,
31+
#[cfg(feature = "std")]
2832
backtrace: Arc::new(Backtrace::disabled()),
2933
}
3034
}
@@ -39,8 +43,8 @@ impl SubAllocator for DedicatedBlockAllocator {
3943
_allocation_type: AllocationType,
4044
_granularity: u64,
4145
name: &str,
42-
backtrace: Arc<Backtrace>,
43-
) -> Result<(u64, std::num::NonZeroU64)> {
46+
#[cfg(feature = "std")] backtrace: Arc<Backtrace>,
47+
) -> Result<(u64, core::num::NonZeroU64)> {
4448
if self.allocated != 0 {
4549
return Err(AllocationError::OutOfMemory);
4650
}
@@ -53,7 +57,10 @@ impl SubAllocator for DedicatedBlockAllocator {
5357

5458
self.allocated = size;
5559
self.name = Some(name.to_string());
56-
self.backtrace = backtrace;
60+
#[cfg(feature = "std")]
61+
{
62+
self.backtrace = backtrace;
63+
}
5764

5865
#[allow(clippy::unwrap_used)]
5966
let dummy_id = std::num::NonZeroU64::new(1).unwrap();
@@ -91,6 +98,7 @@ impl SubAllocator for DedicatedBlockAllocator {
9198
let empty = "".to_string();
9299
let name = self.name.as_ref().unwrap_or(&empty);
93100

101+
#[cfg(feature = "std")]
94102
log!(
95103
log_level,
96104
r#"leak detected: {{
@@ -107,7 +115,23 @@ impl SubAllocator for DedicatedBlockAllocator {
107115
self.size,
108116
name,
109117
self.backtrace
110-
)
118+
);
119+
#[cfg(not(feature = "std"))]
120+
log!(
121+
log_level,
122+
r#"leak detected: {{
123+
memory type: {}
124+
memory block: {}
125+
dedicated allocation: {{
126+
size: 0x{:x},
127+
name: {},
128+
}}
129+
}}"#,
130+
memory_type_index,
131+
memory_block_index,
132+
self.size,
133+
name,
134+
);
111135
}
112136

113137
fn report_allocations(&self) -> Vec<AllocationReport> {

src/allocator/free_list_allocator/mod.rs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#[cfg(feature = "visualizer")]
44
pub(crate) mod visualizer;
55

6+
#[cfg(feature = "std")]
67
use std::{
78
backtrace::Backtrace,
89
collections::{HashMap, HashSet},
@@ -32,6 +33,7 @@ pub(crate) struct MemoryChunk {
3233
pub(crate) allocation_type: AllocationType,
3334
pub(crate) name: Option<String>,
3435
/// Only used if [`crate::AllocatorDebugSettings::store_stack_traces`] is [`true`]
36+
#[cfg(feature = "std")]
3537
pub(crate) backtrace: Arc<Backtrace>,
3638
next: Option<std::num::NonZeroU64>,
3739
prev: Option<std::num::NonZeroU64>,
@@ -79,6 +81,7 @@ impl FreeListAllocator {
7981
offset: 0,
8082
allocation_type: AllocationType::Free,
8183
name: None,
84+
#[cfg(feature = "std")]
8285
backtrace: Arc::new(Backtrace::disabled()),
8386
prev: None,
8487
next: None,
@@ -162,8 +165,8 @@ impl SubAllocator for FreeListAllocator {
162165
allocation_type: AllocationType,
163166
granularity: u64,
164167
name: &str,
165-
backtrace: Arc<Backtrace>,
166-
) -> Result<(u64, std::num::NonZeroU64)> {
168+
#[cfg(feature = "std")] backtrace: Arc<Backtrace>,
169+
) -> Result<(u64, core::num::NonZeroU64)> {
167170
let free_size = self.size - self.allocated;
168171
if size > free_size {
169172
return Err(AllocationError::OutOfMemory);
@@ -249,6 +252,7 @@ impl SubAllocator for FreeListAllocator {
249252
offset: free_chunk.offset,
250253
allocation_type,
251254
name: Some(name.to_string()),
255+
#[cfg(feature = "std")]
252256
backtrace,
253257
prev: free_chunk.prev,
254258
next: Some(first_fit_id),
@@ -278,7 +282,10 @@ impl SubAllocator for FreeListAllocator {
278282

279283
chunk.allocation_type = allocation_type;
280284
chunk.name = Some(name.to_string());
281-
chunk.backtrace = backtrace;
285+
#[cfg(feature = "std")]
286+
{
287+
chunk.backtrace = backtrace;
288+
}
282289

283290
self.remove_id_from_free_list(first_fit_id);
284291

@@ -302,7 +309,10 @@ impl SubAllocator for FreeListAllocator {
302309
})?;
303310
chunk.allocation_type = AllocationType::Free;
304311
chunk.name = None;
305-
chunk.backtrace = Arc::new(Backtrace::disabled());
312+
#[cfg(feature = "std")]
313+
{
314+
chunk.backtrace = Arc::new(Backtrace::disabled());
315+
}
306316

307317
self.allocated -= chunk.size;
308318

@@ -363,6 +373,7 @@ impl SubAllocator for FreeListAllocator {
363373
let empty = "".to_string();
364374
let name = chunk.name.as_ref().unwrap_or(&empty);
365375

376+
#[cfg(feature = "std")]
366377
log!(
367378
log_level,
368379
r#"leak detected: {{
@@ -386,6 +397,28 @@ impl SubAllocator for FreeListAllocator {
386397
name,
387398
chunk.backtrace
388399
);
400+
#[cfg(not(feature = "std"))]
401+
log!(
402+
log_level,
403+
r#"leak detected: {{
404+
memory type: {}
405+
memory block: {}
406+
chunk: {{
407+
chunk_id: {},
408+
size: 0x{:x},
409+
offset: 0x{:x},
410+
allocation_type: {:?},
411+
name: {},
412+
}}
413+
}}"#,
414+
memory_type_index,
415+
memory_block_index,
416+
chunk_id,
417+
chunk.size,
418+
chunk.offset,
419+
chunk.allocation_type,
420+
name,
421+
);
389422
}
390423
}
391424

src/allocator/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::{backtrace::Backtrace, fmt, ops::Range, sync::Arc};
22

33
use log::*;
4+
#[cfg(feature = "std")]
5+
use std::{backtrace::Backtrace, sync::Arc};
46

57
use crate::result::*;
68

@@ -113,8 +115,8 @@ pub(crate) trait SubAllocator: SubAllocatorBase + fmt::Debug + Sync + Send {
113115
allocation_type: AllocationType,
114116
granularity: u64,
115117
name: &str,
116-
backtrace: Arc<Backtrace>,
117-
) -> Result<(u64, std::num::NonZeroU64)>;
118+
#[cfg(feature = "std")] backtrace: Arc<Backtrace>,
119+
) -> Result<(u64, core::num::NonZeroU64)>;
118120

119121
fn free(&mut self, chunk_id: Option<std::num::NonZeroU64>) -> Result<()>;
120122

src/d3d12/mod.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
use std::{
2-
backtrace::Backtrace,
1+
#[cfg(feature = "std")]
2+
use std::{backtrace::Backtrace, sync::Arc};
3+
4+
use alloc::{boxed::Box, string::String, vec::Vec};
5+
use core::{
36
fmt,
47
// TODO: Remove when bumping MSRV to 1.80
58
mem::size_of_val,
6-
sync::Arc,
79
};
8-
910
use log::{debug, warn, Level};
11+
1012
use windows::Win32::{
1113
Foundation::E_OUTOFMEMORY,
1214
Graphics::{
@@ -442,7 +444,7 @@ impl MemoryType {
442444
&mut self,
443445
device: &ID3D12DeviceVersion,
444446
desc: &AllocationCreateDesc<'_>,
445-
backtrace: Arc<Backtrace>,
447+
#[cfg(feature = "std")] backtrace: Arc<Backtrace>,
446448
allocation_sizes: &AllocationSizes,
447449
) -> Result<Allocation> {
448450
let allocation_type = AllocationType::Linear;
@@ -485,6 +487,7 @@ impl MemoryType {
485487
allocation_type,
486488
1,
487489
desc.name,
490+
#[cfg(feature = "std")]
488491
backtrace,
489492
)?;
490493

@@ -508,6 +511,7 @@ impl MemoryType {
508511
allocation_type,
509512
1,
510513
desc.name,
514+
#[cfg(feature = "std")]
511515
backtrace.clone(),
512516
);
513517

@@ -558,6 +562,7 @@ impl MemoryType {
558562
allocation_type,
559563
1,
560564
desc.name,
565+
#[cfg(feature = "std")]
561566
backtrace,
562567
);
563568
let (offset, chunk_id) = match allocation {
@@ -735,6 +740,7 @@ impl Allocator {
735740
let size = desc.size;
736741
let alignment = desc.alignment;
737742

743+
#[cfg(feature = "std")]
738744
let backtrace = Arc::new(if self.debug_settings.store_stack_traces {
739745
Backtrace::force_capture()
740746
} else {
@@ -746,6 +752,7 @@ impl Allocator {
746752
"Allocating `{}` of {} bytes with an alignment of {}.",
747753
&desc.name, size, alignment
748754
);
755+
#[cfg(feature = "std")]
749756
if self.debug_settings.log_stack_traces {
750757
let backtrace = Backtrace::force_capture();
751758
debug!("Allocation stack trace: {}", backtrace);
@@ -771,13 +778,20 @@ impl Allocator {
771778
})
772779
.ok_or(AllocationError::NoCompatibleMemoryTypeFound)?;
773780

774-
memory_type.allocate(&self.device, desc, backtrace, &self.allocation_sizes)
781+
memory_type.allocate(
782+
&self.device,
783+
desc,
784+
#[cfg(feature = "std")]
785+
backtrace,
786+
&self.allocation_sizes,
787+
)
775788
}
776789

777790
pub fn free(&mut self, allocation: Allocation) -> Result<()> {
778791
if self.debug_settings.log_frees {
779792
let name = allocation.name.as_deref().unwrap_or("<null>");
780793
debug!("Freeing `{}`.", name);
794+
#[cfg(feature = "std")]
781795
if self.debug_settings.log_stack_traces {
782796
let backtrace = Backtrace::force_capture();
783797
debug!("Free stack trace: {}", backtrace);

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,14 @@ pub struct AllocatorDebugSettings {
264264
/// Stores a copy of the full backtrace for every allocation made, this makes it easier to debug leaks
265265
/// or other memory allocations, but storing stack traces has a RAM overhead so should be disabled
266266
/// in shipping applications.
267+
#[cfg(feature = "std")]
267268
pub store_stack_traces: bool,
268269
/// Log out every allocation as it's being made with log level Debug, rather spammy so off by default
269270
pub log_allocations: bool,
270271
/// Log out every free that is being called with log level Debug, rather spammy so off by default
271272
pub log_frees: bool,
272273
/// Log out stack traces when either `log_allocations` or `log_frees` is enabled.
274+
#[cfg(feature = "std")]
273275
pub log_stack_traces: bool,
274276
}
275277

@@ -278,9 +280,11 @@ impl Default for AllocatorDebugSettings {
278280
Self {
279281
log_memory_information: false,
280282
log_leaks_on_shutdown: true,
283+
#[cfg(feature = "std")]
281284
store_stack_traces: false,
282285
log_allocations: false,
283286
log_frees: false,
287+
#[cfg(feature = "std")]
284288
log_stack_traces: false,
285289
}
286290
}

src/metal/mod.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[cfg(feature = "std")]
12
use std::{backtrace::Backtrace, sync::Arc};
23

34
#[cfg(feature = "visualizer")]
@@ -225,7 +226,7 @@ impl MemoryType {
225226
&mut self,
226227
device: &ProtocolObject<dyn MTLDevice>,
227228
desc: &AllocationCreateDesc<'_>,
228-
backtrace: Arc<Backtrace>,
229+
#[cfg(feature = "std")] backtrace: Arc<Backtrace>,
229230
allocation_sizes: &AllocationSizes,
230231
) -> Result<Allocation> {
231232
let allocation_type = allocator::AllocationType::Linear;
@@ -268,6 +269,7 @@ impl MemoryType {
268269
allocation_type,
269270
1,
270271
desc.name,
272+
#[cfg(feature = "std")]
271273
backtrace,
272274
)?;
273275

@@ -291,6 +293,7 @@ impl MemoryType {
291293
allocation_type,
292294
1,
293295
desc.name,
296+
#[cfg(feature = "std")]
294297
backtrace.clone(),
295298
);
296299

@@ -341,6 +344,7 @@ impl MemoryType {
341344
allocation_type,
342345
1,
343346
desc.name,
347+
#[cfg(feature = "std")]
344348
backtrace,
345349
);
346350
let (offset, chunk_id) = match allocation {
@@ -452,6 +456,7 @@ impl Allocator {
452456
let size = desc.size;
453457
let alignment = desc.alignment;
454458

459+
#[cfg(feature = "std")]
455460
let backtrace = Arc::new(if self.debug_settings.store_stack_traces {
456461
Backtrace::force_capture()
457462
} else {
@@ -463,6 +468,7 @@ impl Allocator {
463468
"Allocating `{}` of {} bytes with an alignment of {}.",
464469
&desc.name, size, alignment
465470
);
471+
#[cfg(feature = "std")]
466472
if self.debug_settings.log_stack_traces {
467473
let backtrace = Backtrace::force_capture();
468474
debug!("Allocation stack trace: {}", backtrace);
@@ -484,13 +490,20 @@ impl Allocator {
484490
})
485491
.ok_or(AllocationError::NoCompatibleMemoryTypeFound)?;
486492

487-
memory_type.allocate(&self.device, desc, backtrace, &self.allocation_sizes)
493+
memory_type.allocate(
494+
&self.device,
495+
desc,
496+
#[cfg(feature = "std")]
497+
backtrace,
498+
&self.allocation_sizes,
499+
)
488500
}
489501

490502
pub fn free(&mut self, allocation: &Allocation) -> Result<()> {
491503
if self.debug_settings.log_frees {
492504
let name = allocation.name.as_deref().unwrap_or("<null>");
493505
debug!("Freeing `{}`.", name);
506+
#[cfg(feature = "std")]
494507
if self.debug_settings.log_stack_traces {
495508
let backtrace = Backtrace::force_capture();
496509
debug!("Free stack trace: {}", backtrace);

0 commit comments

Comments
 (0)