#![allow(dead_code)]
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use std::collections::HashMap;
pub type CommentMap = HashMap<usize, String>;
pub struct AssemblyCommentsRegistry {
lock: Mutex<Vec<(usize, usize, CommentMap)>>,
}
impl AssemblyCommentsRegistry {
pub fn new() -> Self {
AssemblyCommentsRegistry {
lock: Mutex::new(Vec::new()),
}
}
pub fn singleton() -> &'static Self {
static INSTANCE: Lazy<AssemblyCommentsRegistry> =
Lazy::new(|| AssemblyCommentsRegistry::new());
&INSTANCE
}
pub fn comment<'a>(&self, in_code: *const u8) -> Option<String> {
let comments = self.lock.lock();
comments
.iter()
.find(|(start, end, _)| in_code as usize >= *start && in_code as usize <= *end)
.and_then(|(_, _, comment_map)| comment_map.get(&(in_code as usize)).map(|s| s.clone()))
}
pub fn unregister_code_range(&self, start: *const u8, end: *const u8) {
let start = start as usize;
let end = end as usize;
let mut comments = self.lock.lock();
let mut found_end = 0;
comments.retain(|(k, v, _)| {
if *k == start && *v == end {
found_end = *v;
return false;
}
true
});
assert_eq!(found_end, end);
}
pub fn register_code_range(&self, start: *const u8, end: *const u8, new_comments: CommentMap) {
let start = start as usize;
let end = end as usize;
let mut comments = self.lock.lock();
for (pos, comment) in new_comments.iter() {
println!("{:x}: {}", pos, comment);
}
comments.push((start, end, new_comments));
}
}