Skip to content

Commit

Permalink
struct MemPool: Use Vec as a stack instead of LinkedList, since…
Browse files Browse the repository at this point in the history
… no insertions/deletions are done from the middle.
  • Loading branch information
kkysen committed Apr 25, 2024
1 parent 2ca59e5 commit 86e32d9
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/mem.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
use libc::free;
use libc::posix_memalign;
use std::collections::LinkedList;
use std::ffi::c_void;
use std::sync::Mutex;

pub struct MemPool<T> {
bufs: Mutex<LinkedList<Vec<T>>>,
bufs: Mutex<Vec<Vec<T>>>,
}

impl<T> MemPool<T> {
pub const fn new() -> Self {
Self {
bufs: Mutex::new(LinkedList::new()),
bufs: Mutex::new(Vec::new()),
}
}

pub fn pop(&self, size: usize) -> Vec<T> {
if let Some(mut buf) = self.bufs.lock().unwrap().pop_front() {
if let Some(mut buf) = self.bufs.lock().unwrap().pop() {
if size > buf.capacity() {
// TODO fallible allocation
buf.reserve(size - buf.len());
Expand All @@ -28,7 +27,7 @@ impl<T> MemPool<T> {
}

pub fn push(&self, buf: Vec<T>) {
self.bufs.lock().unwrap().push_front(buf);
self.bufs.lock().unwrap().push(buf);
}
}

Expand Down

0 comments on commit 86e32d9

Please sign in to comment.