2019-05-24 16:21:42 -07:00
|
|
|
use crate::alloc;
|
|
|
|
|
|
|
|
use alloc::{Alloc, AllocErr};
|
|
|
|
use std::alloc::Layout;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct BPFAllocator {
|
|
|
|
heap: Vec<u8>,
|
2019-08-23 11:03:53 -07:00
|
|
|
start: u64,
|
|
|
|
len: u64,
|
|
|
|
pos: u64,
|
2019-05-24 16:21:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BPFAllocator {
|
2019-08-23 11:03:53 -07:00
|
|
|
pub fn new(heap: Vec<u8>, virtual_address: u64) -> Self {
|
|
|
|
let len = heap.len() as u64;
|
|
|
|
Self {
|
|
|
|
heap,
|
|
|
|
start: virtual_address,
|
|
|
|
len,
|
|
|
|
pos: 0,
|
|
|
|
}
|
2019-05-24 16:21:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Alloc for BPFAllocator {
|
2019-08-23 11:03:53 -07:00
|
|
|
fn alloc(&mut self, layout: Layout) -> Result<u64, AllocErr> {
|
|
|
|
if self.pos + layout.size() as u64 <= self.len {
|
|
|
|
let addr = self.start + self.pos;
|
|
|
|
self.pos += layout.size() as u64;
|
|
|
|
Ok(addr)
|
2019-05-24 16:21:42 -07:00
|
|
|
} else {
|
|
|
|
Err(AllocErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 11:03:53 -07:00
|
|
|
fn dealloc(&mut self, _addr: u64, _layout: Layout) {
|
2019-05-24 16:21:42 -07:00
|
|
|
// It's a bump allocator, free not supported
|
|
|
|
}
|
|
|
|
}
|