trace_recorder_parser/snapshot/
symbol_table.rs

1use crate::types::{ObjectHandle, SymbolString, SymbolTableExt};
2use derive_more::{Binary, Display, Into, LowerHex, Octal, UpperHex};
3use std::collections::BTreeMap;
4
5#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
6pub struct SymbolTable {
7    /// The key is the byte offset of this entry within the originating table in memory,
8    /// referenced by user event payloads
9    pub symbols: BTreeMap<ObjectHandle, SymbolTableEntry>,
10}
11
12impl SymbolTable {
13    /// Up to 64 linked lists within the symbol table
14    /// connecting all entries with the same 6 bit checksum
15    pub(crate) const NUM_LATEST_ENTRY_OF_CHECKSUMS: usize = 64;
16
17    pub fn insert(
18        &mut self,
19        handle: ObjectHandle,
20        channel_index: Option<ObjectHandle>,
21        crc: SymbolCrc6,
22        symbol: SymbolString,
23    ) {
24        self.symbols.insert(
25            handle,
26            SymbolTableEntry {
27                channel_index,
28                crc,
29                symbol,
30            },
31        );
32    }
33
34    pub fn get(&self, handle: ObjectHandle) -> Option<&SymbolTableEntry> {
35        self.symbols.get(&handle)
36    }
37}
38
39impl SymbolTableExt for SymbolTable {
40    fn symbol(&self, handle: ObjectHandle) -> Option<&SymbolString> {
41        self.get(handle).map(|ste| &ste.symbol)
42    }
43}
44
45#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display)]
46#[display(fmt = "{symbol}")]
47pub struct SymbolTableEntry {
48    /// Reference to a symbol table entry, a label for vTracePrintF
49    /// format strings only (the handle of the destination channel)
50    pub channel_index: Option<ObjectHandle>,
51    /// 6-bit CRC of the binary symbol (before lossy UTF8 string conversion)
52    pub crc: SymbolCrc6,
53    /// The symbol (lossy converted to UTF8)
54    pub symbol: SymbolString,
55}
56
57#[derive(
58    Copy,
59    Clone,
60    Eq,
61    PartialEq,
62    Ord,
63    PartialOrd,
64    Hash,
65    Debug,
66    Into,
67    Display,
68    Binary,
69    Octal,
70    LowerHex,
71    UpperHex,
72)]
73#[display(fmt = "{_0:X}")]
74pub struct SymbolCrc6(u8);
75
76impl SymbolCrc6 {
77    pub(crate) fn new(s: &[u8]) -> Self {
78        let mut crc: u32 = 0;
79        for b in s.iter() {
80            crc += *b as u32;
81        }
82        Self((crc & 0x3F) as u8)
83    }
84}