Skip to content

Commit 448f2c6

Browse files
committed
Tidy up definition and add docs
1 parent bee5c09 commit 448f2c6

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

src/symbolize/gimli.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ impl Mapping {
330330

331331
type Cache = Vec<(PathBuf, Mapping)>;
332332

333-
/// Warning: this function is not threadsafe and needs to be externally synchronized
333+
// unsafe because this is required to be externally synchronized
334334
unsafe fn with_cache(f: impl FnOnce(&mut Cache)) {
335335
// A very small, very simple LRU cache for debug info mappings.
336336
//
@@ -347,6 +347,7 @@ unsafe fn with_cache(f: impl FnOnce(&mut Cache)) {
347347
f(MAPPINGS_CACHE.get_or_insert_with(|| Vec::with_capacity(MAPPINGS_CACHE_SIZE)))
348348
}
349349

350+
// unsafe because this is required to be externally synchronized
350351
pub unsafe fn clear_symbol_cache() {
351352
with_cache(|cache| cache.clear());
352353
}

src/symbolize/mod.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -434,13 +434,35 @@ cfg_if::cfg_if! {
434434
}
435435
}
436436

437+
/// Attempt to reclaim that cached memory used to symbolicate addresses.
438+
///
439+
/// This method will attempt to release any global data structures that have
440+
/// otherwise been cached globally or in the thread which typically represent
441+
/// parsed DWARF information or similar.
442+
///
443+
/// # Caveats
444+
///
445+
/// While this function is always available it doesn't actually do anything on
446+
/// most implementations. Libraries like dbghelp or libbacktrace do not provide
447+
/// facilities to deallocate state and manage the allocated memory. For now the
448+
/// `gimli-symbolize` feature of this crate is the only feature where this
449+
/// function has any effect.
450+
#[cfg(feature = "std")]
451+
pub fn clear_symbol_cache() {
452+
let _guard = crate::lock::lock();
453+
unsafe {
454+
clear_symbol_cache_imp();
455+
}
456+
}
457+
437458
mod dladdr;
438459

439460
cfg_if::cfg_if! {
440461
if #[cfg(all(windows, target_env = "msvc", feature = "dbghelp"))] {
441462
mod dbghelp;
442463
use self::dbghelp::resolve as resolve_imp;
443464
use self::dbghelp::Symbol as SymbolImp;
465+
unsafe fn clear_symbol_cache_imp() {}
444466
} else if #[cfg(all(
445467
feature = "std",
446468
feature = "gimli-symbolize",
@@ -453,6 +475,7 @@ cfg_if::cfg_if! {
453475
mod gimli;
454476
use self::gimli::resolve as resolve_imp;
455477
use self::gimli::Symbol as SymbolImp;
478+
use self::gimli::clear_symbol_cache as clear_symbol_cache_imp;
456479
// Note that we only enable coresymbolication on iOS when debug assertions
457480
// are enabled because it's helpful in debug mode but it looks like apps get
458481
// rejected from the app store if they use this API, see #92 for more info
@@ -462,50 +485,26 @@ cfg_if::cfg_if! {
462485
mod coresymbolication;
463486
use self::coresymbolication::resolve as resolve_imp;
464487
use self::coresymbolication::Symbol as SymbolImp;
488+
unsafe fn clear_symbol_cache_imp() {}
465489
} else if #[cfg(all(feature = "libbacktrace",
466490
any(unix, all(windows, not(target_vendor = "uwp"), target_env = "gnu")),
467491
not(target_os = "fuchsia"),
468492
not(target_os = "emscripten")))] {
469493
mod libbacktrace;
470494
use self::libbacktrace::resolve as resolve_imp;
471495
use self::libbacktrace::Symbol as SymbolImp;
496+
unsafe fn clear_symbol_cache_imp() {}
472497
} else if #[cfg(all(unix,
473498
not(target_os = "emscripten"),
474499
feature = "dladdr"))] {
475500
mod dladdr_resolve;
476501
use self::dladdr_resolve::resolve as resolve_imp;
477502
use self::dladdr_resolve::Symbol as SymbolImp;
503+
unsafe fn clear_symbol_cache_imp() {}
478504
} else {
479505
mod noop;
480506
use self::noop::resolve as resolve_imp;
481507
use self::noop::Symbol as SymbolImp;
508+
unsafe fn clear_symbol_cache_imp() {}
482509
}
483510
}
484-
485-
cfg_if::cfg_if! {
486-
if #[cfg(all(
487-
feature = "std",
488-
feature = "gimli-symbolize",
489-
any(
490-
target_os = "linux",
491-
target_os = "macos",
492-
windows,
493-
),
494-
not(all(windows, target_env = "msvc", feature = "dbghelp")),
495-
))] {
496-
/// clear_symbol_cache tries to reclaim that cached memory.
497-
/// Note: for now, only the Gimli implementation is able to clear its cache.
498-
pub fn clear_symbol_cache() {
499-
let _guard = crate::lock::lock();
500-
unsafe {
501-
self::gimli::clear_symbol_cache();
502-
}
503-
}
504-
} else if #[cfg(feature = "std")] {
505-
/// clear_symbol_cache tries to reclaim that cached memory.
506-
/// Note: for now, only the Gimli implementation is able to clear its cache.
507-
pub fn clear_symbol_cache() {
508-
// noop
509-
}
510-
}
511-
}

0 commit comments

Comments
 (0)