use crate::capture::backends::{CaptureBackend, CaptureBackendType};
use crate::event_store::SharedEventStore;
pub struct CaptureEngine {
backend: Box<dyn CaptureBackend>,
event_store: SharedEventStore,
}
impl CaptureEngine {
pub fn new(backend_type: CaptureBackendType, event_store: SharedEventStore) -> Self {
let backend = backend_type.create_backend();
Self {
backend,
event_store,
}
}
pub fn capture_alloc(&self, ptr: usize, size: usize, thread_id: u64) {
let event = self.backend.capture_alloc(ptr, size, thread_id);
self.event_store.record(event);
}
pub fn capture_dealloc(&self, ptr: usize, size: usize, thread_id: u64) {
let event = self.backend.capture_dealloc(ptr, size, thread_id);
self.event_store.record(event);
}
pub fn capture_realloc(&self, ptr: usize, old_size: usize, new_size: usize, thread_id: u64) {
let event = self
.backend
.capture_realloc(ptr, old_size, new_size, thread_id);
self.event_store.record(event);
}
pub fn capture_move(&self, from_ptr: usize, to_ptr: usize, size: usize, thread_id: u64) {
let event = self.backend.capture_move(from_ptr, to_ptr, size, thread_id);
self.event_store.record(event);
}
pub fn event_store(&self) -> &SharedEventStore {
&self.event_store
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::event_store::EventStore;
use std::sync::Arc;
#[test]
fn test_capture_engine_creation() {
let event_store = Arc::new(EventStore::new());
let engine = CaptureEngine::new(CaptureBackendType::Core, event_store);
assert!(engine.event_store().is_empty());
}
#[test]
fn test_capture_alloc() {
let event_store = Arc::new(EventStore::new());
let engine = CaptureEngine::new(CaptureBackendType::Core, event_store.clone());
engine.capture_alloc(0x1000, 1024, 1);
assert_eq!(event_store.len(), 1);
}
#[test]
fn test_capture_dealloc() {
let event_store = Arc::new(EventStore::new());
let engine = CaptureEngine::new(CaptureBackendType::Core, event_store.clone());
engine.capture_dealloc(0x1000, 1024, 1);
assert_eq!(event_store.len(), 1);
}
#[test]
fn test_capture_multiple_events() {
let event_store = Arc::new(EventStore::new());
let engine = CaptureEngine::new(CaptureBackendType::Core, event_store.clone());
engine.capture_alloc(0x1000, 1024, 1);
engine.capture_dealloc(0x1000, 1024, 1);
engine.capture_alloc(0x2000, 2048, 1);
assert_eq!(event_store.len(), 3);
}
}