Skip to main content

memscope_rs/facade/
macros.rs

1//! Facade Macros - Convenient macros for memory tracking
2//!
3//! This module provides macros that simplify common memory tracking
4/// operations, making the API more ergonomic and reducing boilerplate.
5use std::collections::hash_map::DefaultHasher;
6use std::hash::{Hash, Hasher};
7use std::thread;
8
9/// Helper function to hash thread ID
10#[doc(hidden)]
11pub fn get_heap_ptr_if_trackable<T>(_value: &T) -> Option<usize> {
12    // This function is a placeholder. In a real implementation,
13    // we would check if T implements Trackable and call get_heap_ptr()
14    // For now, we return None to indicate we should use stack address
15    None
16}
17
18/// Helper function to hash thread ID
19#[doc(hidden)]
20pub fn hash_thread_id() -> u64 {
21    let thread_id = thread::current().id();
22    let thread_id_str = format!("{:?}", thread_id);
23    let mut hasher = DefaultHasher::new();
24    thread_id_str.hash(&mut hasher);
25    hasher.finish()
26}
27
28/// Get a memory snapshot and display summary
29///
30/// This macro creates a quick summary of current memory usage,
31/// useful for debugging and monitoring.
32///
33/// # Examples
34///
35/// ```rust
36/// use memscope_rs::memscope_summary;
37///
38/// memscope_summary!();
39/// ```
40#[macro_export]
41macro_rules! memscope_summary {
42    () => {
43        let summary = $crate::facade::compat::get_memory_summary();
44        println!("Memory Summary:");
45        println!("  Total Allocations: {}", summary.total_allocations);
46        println!("  Active Allocations: {}", summary.active_allocations);
47        println!("  Current Memory: {} bytes", summary.current_memory);
48        println!("  Peak Memory: {} bytes", summary.peak_memory);
49        println!("  Thread Count: {}", summary.thread_count);
50    };
51}
52
53/// Get top allocations and display them
54///
55/// This macro shows the largest memory allocations, useful for
56/// identifying memory usage patterns.
57///
58/// # Examples
59///
60/// ```rust
61/// use memscope_rs::show_top_allocations;
62///
63/// show_top_allocations!(10);
64/// ```
65#[macro_export]
66macro_rules! show_top_allocations {
67    ($limit:expr) => {
68        let allocations = $crate::facade::compat::get_top_allocations($limit);
69        println!("Top {} Allocations by Size:", $limit);
70        for (i, alloc) in allocations.iter().enumerate() {
71            println!("  {}. {} bytes at {:x}", i + 1, alloc.size, alloc.ptr);
72        }
73    };
74}
75
76/// Export memory data to JSON and print the result
77///
78/// This macro is a convenient way to export memory data
79/// in JSON format for external processing or analysis.
80///
81/// # Examples
82///
83/// ```rust
84/// use memscope_rs::export_memory_json;
85///
86/// export_memory_json!(true);
87/// ```
88#[macro_export]
89macro_rules! export_memory_json {
90    ($verbose:expr) => {
91        match $crate::facade::compat::export_json($verbose) {
92            Ok(json) => {
93                println!("Memory data exported to JSON:");
94                println!("{}", json);
95            }
96            Err(e) => {
97                eprintln!("Failed to export memory data: {}", e);
98            }
99        }
100    };
101}
102
103#[cfg(test)]
104mod tests {
105    #[test]
106    fn test_memscope_summary_macro_compiles() {
107        // This test just ensures the macro compiles correctly
108        // We don't actually run it to avoid printing to stdout during tests
109    }
110}