1use std::sync::Arc;
4use std::collections::VecDeque;
5use parking_lot::Mutex;
6use serde::{Deserialize, Serialize};
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.available.lock().pop_front()
53 .unwrap_or_else(|| Vec::with_capacity(size));
54
55 buffer.resize(size, 0);
56
57 if self.config.enable_monitoring {
58 let mut stats = self.stats.lock();
59 stats.allocations += 1;
60 stats.current_usage += size;
61 stats.peak_usage = stats.peak_usage.max(stats.current_usage);
62 }
63
64 buffer
65 }
66
67 pub fn deallocate(&self, mut buffer: Vec<u8>) {
68 let buffer_len = buffer.len();
69
70 if buffer.capacity() <= self.config.max_allocation {
71 buffer.clear();
72 self.available.lock().push_back(buffer);
73 }
74
75 if self.config.enable_monitoring {
76 let mut stats = self.stats.lock();
77 stats.deallocations += 1;
78 stats.current_usage = stats.current_usage.saturating_sub(buffer_len);
79 }
80 }
81
82 pub fn get_stats(&self) -> MemoryStats {
83 self.stats.lock().clone()
84 }
85}
86
87pub struct MemoryManager {
89 pool: MemoryPool,
90}
91
92impl MemoryManager {
93 pub fn new(config: MemoryConfig) -> Self {
94 Self {
95 pool: MemoryPool::new(config),
96 }
97 }
98
99 pub fn allocate(&self, size: usize) -> Vec<u8> {
100 self.pool.allocate(size)
101 }
102
103 pub fn deallocate(&self, buffer: Vec<u8>) {
104 self.pool.deallocate(buffer);
105 }
106
107 pub fn get_stats(&self) -> MemoryStats {
108 self.pool.get_stats()
109 }
110}