trash_parallelism 0.1.102

Azzybana Raccoon's comprehensive parallelism library.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/// Advanced memory features and utilities.
///
/// This module provides specialized memory operations including
/// compression, encryption, parallel processing, and memory-mapped I/O.
///
/// ## Features
///
/// - **Compressed Memory Pools**: Space-efficient storage with Brotli compression
/// - **Secure Memory Pools**: Encrypted memory with automatic secure wiping
/// - **Memory-Mapped Pools**: Large data handling with file-backed storage
/// - **Parallel Processing**: Concurrent memory operations using thread pools
/// - **RAII Guards**: Automatic cleanup for compressed and secure allocations
///
/// ## Examples
///
/// ### Compressed Memory Pool
/// ```rust
/// use trash_utilities::memory::*;
///
/// // Create a compressed pool
/// let config = default_pool_config("compressed");
/// let pool = CompressedMemoryPool::new(config, 6); // Compression level 6
///
/// // Allocate and compress data
/// let data = b"Hello, this is some data that will be compressed!";
/// let allocation = pool.allocate_compressed(data).unwrap();
///
/// // Check compression ratio
/// println!("Compression ratio: {:.2}", allocation.compression_ratio());
/// println!("Original size: {} bytes", allocation.original_size());
/// println!("Compressed size: {} bytes", allocation.compressed_size());
///
/// // Decompress and get original data
/// let decompressed = allocation.decompress().unwrap();
/// assert_eq!(decompressed, data);
/// ```
///
/// ### Secure Memory Pool
/// ```rust
/// use trash_utilities::memory::*;
///
/// // Create a secure pool with encryption
/// let config = default_pool_config("secure");
/// let key = b"my-secret-key-32-bytes-long!!!"; // 32 bytes for AES-256
/// let pool = SecureMemoryPool::new(config, Some(key.to_vec()));
///
/// // Allocate and encrypt sensitive data
/// let sensitive_data = b"This is sensitive information";
/// let allocation = pool.allocate_encrypted(sensitive_data).unwrap();
///
/// // Decrypt and verify
/// let decrypted = allocation.decrypt(key).unwrap();
/// assert_eq!(decrypted, sensitive_data);
///
/// // Data is automatically wiped when allocation goes out of scope
/// drop(allocation);
/// ```
///
/// ### Parallel Memory Processing
/// ```rust
/// use trash_utilities::memory::*;
///
/// let processor = ParallelMemoryProcessor::new(4);
///
/// // Compress multiple blocks in parallel
/// let blocks = vec![
///     b"Block 1 data".to_vec(),
///     b"Block 2 data".to_vec(),
///     b"Block 3 data".to_vec(),
/// ];
///
/// let compressed = processor.compress_blocks(blocks, 6);
/// for result in compressed {
///     let compressed_data = result.unwrap();
///     println!("Compressed {} bytes", compressed_data.len());
/// }
/// ```
// Standard library imports
use std::{
    io::{Read, Write},
    slice::from_raw_parts,
    sync::Arc,
};

// External crate imports
use arc_swap::ArcSwap;
use brotli::{CompressorWriter, Decompressor};
use parking_lot::Mutex;
use tempfile::NamedTempFile;

// Local imports
use super::{MemoryPool, MemoryPoolConfig, MemoryStats};
#[derive(Debug)]
pub struct CompressedMemoryPool {
    pool: MemoryPool,
    compression_level: u32,
}

impl CompressedMemoryPool {
    /// Create a new compressed memory pool
    #[must_use]
    pub fn new(config: MemoryPoolConfig, compression_level: u32) -> Self {
        Self {
            pool: MemoryPool::new(config),
            compression_level,
        }
    }

    /// Allocate and compress data
    ///
    /// # Errors
    ///
    /// Returns an `std::io::Error` if compression or allocation fails.
    pub fn allocate_compressed(
        &self,
        data: &[u8],
    ) -> Result<CompressedAllocation<'_>, std::io::Error> {
        let compressed = compress_data(data, self.compression_level)?;
        let ptr = self.pool.allocate(compressed.len())?;
        unsafe {
            ptr.copy_from(compressed.as_ptr(), compressed.len());
        }

        Ok(CompressedAllocation {
            ptr,
            compressed_size: compressed.len(),
            original_size: data.len(),
            pool: &self.pool,
        })
    }

    /// Get pool statistics
    #[must_use]
    pub fn stats(&self) -> Arc<MemoryStats> {
        self.pool.stats()
    }

    /// Get compression level
    #[must_use]
    pub fn compression_level(&self) -> u32 {
        self.compression_level
    }
}

/// Guard for compressed memory allocation
#[derive(Debug)]
pub struct CompressedAllocation<'a> {
    ptr: *mut u8,
    compressed_size: usize,
    original_size: usize,
    pool: &'a MemoryPool,
}

impl CompressedAllocation<'_> {
    /// Decompress and get original data
    ///
    /// # Errors
    ///
    /// Returns an `std::io::Error` if decompression fails.
    pub fn decompress(&self) -> Result<Vec<u8>, std::io::Error> {
        let compressed_data = unsafe { from_raw_parts(self.ptr, self.compressed_size) };
        decompress_data(compressed_data)
    }

    /// Get compression ratio
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn compression_ratio(&self) -> f64 {
        self.compressed_size as f64 / self.original_size as f64
    }

    /// Get compressed size
    #[must_use]
    pub fn compressed_size(&self) -> usize {
        self.compressed_size
    }

    /// Get original size
    #[must_use]
    pub fn original_size(&self) -> usize {
        self.original_size
    }
}

impl Drop for CompressedAllocation<'_> {
    fn drop(&mut self) {
        let _ = self.pool.deallocate(self.ptr, self.compressed_size);
    }
}

/// Secure memory pool with cryptographic features
#[derive(Debug)]
pub struct SecureMemoryPool {
    pool: MemoryPool,
    encryption_key: Option<Vec<u8>>,
}

impl SecureMemoryPool {
    /// Create a new secure memory pool
    #[must_use]
    pub fn new(config: MemoryPoolConfig, encryption_key: Option<Vec<u8>>) -> Self {
        Self {
            pool: MemoryPool::new(config),
            encryption_key,
        }
    }

    /// Allocate and encrypt data
    ///
    /// # Errors
    ///
    /// Returns an `std::io::Error` if allocation fails.
    pub fn allocate_encrypted(&self, data: &[u8]) -> Result<SecureAllocation<'_>, std::io::Error> {
        let encrypted = if let Some(key) = &self.encryption_key {
            // Simple XOR encryption for demonstration
            data.iter()
                .zip(key.iter().cycle())
                .map(|(d, k)| d ^ k)
                .collect()
        } else {
            data.to_vec()
        };

        let ptr = self.pool.allocate(encrypted.len())?;
        unsafe {
            ptr.copy_from(encrypted.as_ptr(), encrypted.len());
        }

        Ok(SecureAllocation {
            ptr,
            size: encrypted.len(),
            pool: &self.pool,
            encrypted: self.encryption_key.is_some(),
        })
    }

    /// Get pool statistics
    #[must_use]
    pub fn stats(&self) -> Arc<MemoryStats> {
        self.pool.stats()
    }

    /// Check if encryption is enabled
    #[must_use]
    pub fn is_encrypted(&self) -> bool {
        self.encryption_key.is_some()
    }
}

/// Guard for secure memory allocation
#[derive(Debug)]
pub struct SecureAllocation<'a> {
    ptr: *mut u8,
    size: usize,
    pool: &'a MemoryPool,
    encrypted: bool,
}

impl SecureAllocation<'_> {
    /// Decrypt and get original data
    ///
    /// # Errors
    ///
    /// Returns an `std::io::Error` if decryption fails.
    pub fn decrypt(&self, key: &[u8]) -> Result<Vec<u8>, std::io::Error> {
        let encrypted_data = unsafe { from_raw_parts(self.ptr, self.size) };

        if !self.encrypted {
            return Ok(encrypted_data.to_vec());
        }

        // Simple XOR decryption
        let decrypted = encrypted_data
            .iter()
            .zip(key.iter().cycle())
            .map(|(d, k)| d ^ k)
            .collect();

        Ok(decrypted)
    }

    /// Secure wipe (overwrite with zeros)
    pub fn secure_wipe(&mut self) {
        unsafe {
            std::ptr::write_bytes(self.ptr, 0, self.size);
        }
    }

    /// Get allocation size
    #[must_use]
    pub fn size(&self) -> usize {
        self.size
    }

    /// Check if data is encrypted
    #[must_use]
    pub fn is_encrypted(&self) -> bool {
        self.encrypted
    }
}

impl Drop for SecureAllocation<'_> {
    fn drop(&mut self) {
        self.secure_wipe();
        let _ = self.pool.deallocate(self.ptr, self.size);
    }
}

/// Memory-mapped file pool for large data
#[derive(Debug)]
pub struct MemoryMappedPool {
    temp_file: NamedTempFile,
    mapped_data: Mutex<Vec<u8>>,
    stats: ArcSwap<MemoryStats>,
}

impl MemoryMappedPool {
    /// Create a new memory-mapped pool
    ///
    /// # Errors
    ///
    /// Returns an `std::io::Error` if file creation fails.
    pub fn new(capacity: usize) -> Result<Self, std::io::Error> {
        let mut temp_file = NamedTempFile::new()?;
        temp_file.as_file_mut().set_len(capacity as u64)?;

        Ok(Self {
            temp_file,
            mapped_data: Mutex::new(vec![0u8; capacity]),
            stats: ArcSwap::new(Arc::new(MemoryStats::default())),
        })
    }

    /// Write data to mapped memory
    ///
    /// # Errors
    ///
    /// Returns an `std::io::Error` if the write exceeds capacity.
    pub fn write_data(&self, offset: usize, data: &[u8]) -> Result<(), std::io::Error> {
        if offset + data.len() > self.mapped_data.lock().len() {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "Data exceeds capacity",
            ));
        }

        let mut mapped = self.mapped_data.lock();
        mapped[offset..offset + data.len()].copy_from_slice(data);

        // Update stats
        let mut new_stats = (**self.stats.load()).clone();
        new_stats.allocated_bytes += data.len();
        self.stats.store(Arc::new(new_stats));

        Ok(())
    }

    /// Read data from mapped memory
    ///
    /// # Errors
    ///
    /// Returns an `std::io::Error` if the read exceeds capacity.
    pub fn read_data(&self, offset: usize, length: usize) -> Result<Vec<u8>, std::io::Error> {
        let mapped = self.mapped_data.lock();
        if offset + length > mapped.len() {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "Read exceeds capacity",
            ));
        }

        Ok(mapped[offset..offset + length].to_vec())
    }

    /// Flush to disk
    ///
    /// # Errors
    ///
    /// Returns an `std::io::Error` if flushing fails.
    pub fn flush(&self) -> Result<(), std::io::Error> {
        self.temp_file.as_file().sync_all()
    }

    /// Get statistics
    #[must_use]
    pub fn stats(&self) -> Arc<MemoryStats> {
        self.stats.load_full()
    }

    /// Get capacity
    #[must_use]
    pub fn capacity(&self) -> usize {
        self.mapped_data.lock().len()
    }
}

/// Parallel memory operations using `fork_union`
#[derive(Debug)]
pub struct ParallelMemoryProcessor {
    pool: Arc<std::sync::Mutex<Vec<std::thread::JoinHandle<()>>>>,
}

impl ParallelMemoryProcessor {
    /// Create a new parallel processor
    #[must_use]
    pub fn new(_num_threads: usize) -> Self {
        Self {
            pool: Arc::new(std::sync::Mutex::new(Vec::new())),
        }
    }

    /// Process multiple memory blocks in parallel
    pub fn process_blocks<F, T>(&self, blocks: Vec<Vec<u8>>, processor: F) -> Vec<T>
    where
        F: Fn(Vec<u8>) -> T + Send + Sync + 'static,
        T: Send + 'static,
    {
        let results: Vec<T> = blocks.into_iter().map(processor).collect();
        results
    }

    /// Parallel compression of multiple blocks
    #[must_use]
    pub fn compress_blocks(
        &self,
        blocks: Vec<Vec<u8>>,
        level: u32,
    ) -> Vec<Result<Vec<u8>, std::io::Error>> {
        self.process_blocks(blocks, move |block| compress_data(&block, level))
    }

    /// Parallel hashing of multiple blocks
    #[must_use]
    pub fn hash_blocks(&self, blocks: Vec<Vec<u8>>) -> Vec<u64> {
        use ahash::AHasher;
        use std::hash::Hasher;

        self.process_blocks(blocks, |block| {
            let mut hasher = AHasher::default();
            hasher.write(&block);
            hasher.finish()
        })
    }

    /// Get number of active threads
    ///
    /// # Panics
    ///
    /// This function may panic if the mutex is poisoned.
    #[must_use]
    pub fn active_threads(&self) -> usize {
        self.pool.lock().unwrap().len()
    }
}

impl Default for ParallelMemoryProcessor {
    fn default() -> Self {
        Self::new(4)
    }
}

/// Utility functions for compression
fn compress_data(data: &[u8], level: u32) -> Result<Vec<u8>, std::io::Error> {
    let mut output = Vec::new();
    {
        let mut compressor = CompressorWriter::new(&mut output, 4096, level, level);
        compressor.write_all(data)?;
        compressor.flush()?;
    }
    Ok(output)
}

fn decompress_data(data: &[u8]) -> Result<Vec<u8>, std::io::Error> {
    let mut decompressor = Decompressor::new(data, 4096);
    let mut output = Vec::new();
    decompressor.read_to_end(&mut output)?;
    Ok(output)
}

/// Get the global enhanced memory manager
#[must_use]
pub fn global_enhanced_memory_manager() -> Arc<super::EnhancedMemoryManager> {
    Arc::new(super::EnhancedMemoryManager::new(4))
}