1use parking_lot::Mutex;
4use serde::{Deserialize, Serialize};
5use std::collections::VecDeque;
6use std::sync::Arc;
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct MemoryConfig {
11 pub pool_size: usize,
12 pub max_allocation: usize,
13 pub enable_monitoring: bool,
14}
15
16impl Default for MemoryConfig {
17 fn default() -> Self {
18 Self {
19 pool_size: 1024,
20 max_allocation: 100 * 1024 * 1024, enable_monitoring: true,
22 }
23 }
24}
25
26#[derive(Debug, Clone, Default, Serialize, Deserialize)]
28pub struct MemoryStats {
29 pub allocations: u64,
30 pub deallocations: u64,
31 pub current_usage: usize,
32 pub peak_usage: usize,
33}
34
35pub struct MemoryPool {
37 config: MemoryConfig,
38 available: Arc<Mutex<VecDeque<Vec<u8>>>>,
39 stats: Arc<Mutex<MemoryStats>>,
40}
41
42impl MemoryPool {
43 pub fn new(config: MemoryConfig) -> Self {
44 Self {
45 config,
46 available: Arc::new(Mutex::new(VecDeque::new())),
47 stats: Arc::new(Mutex::new(MemoryStats::default())),
48 }
49 }
50
51 pub fn allocate(&self, size: usize) -> Vec<u8> {
52 let mut buffer = self
53 .available
54 .lock()
55 .pop_front()
56 .unwrap_or_else(|| Vec::with_capacity(size));
57
58 buffer.resize(size, 0);
59
60 if self.config.enable_monitoring {
61 let mut stats = self.stats.lock();
62 stats.allocations += 1;
63 stats.current_usage += size;
64 stats.peak_usage = stats.peak_usage.max(stats.current_usage);
65 }
66
67 buffer
68 }
69
70 pub fn deallocate(&self, mut buffer: Vec<u8>) {
71 let buffer_len = buffer.len();
72
73 if buffer.capacity() <= self.config.max_allocation {
74 buffer.clear();
75 self.available.lock().push_back(buffer);
76 }
77
78 if self.config.enable_monitoring {
79 let mut stats = self.stats.lock();
80 stats.deallocations += 1;
81 stats.current_usage = stats.current_usage.saturating_sub(buffer_len);
82 }
83 }
84
85 pub fn get_stats(&self) -> MemoryStats {
86 self.stats.lock().clone()
87 }
88}
89
90pub struct MemoryManager {
92 pool: MemoryPool,
93}
94
95impl MemoryManager {
96 pub fn new(config: MemoryConfig) -> Self {
97 Self {
98 pool: MemoryPool::new(config),
99 }
100 }
101
102 pub fn allocate(&self, size: usize) -> Vec<u8> {
103 self.pool.allocate(size)
104 }
105
106 pub fn deallocate(&self, buffer: Vec<u8>) {
107 self.pool.deallocate(buffer);
108 }
109
110 pub fn get_stats(&self) -> MemoryStats {
111 self.pool.get_stats()
112 }
113}