Split BPF loader to match the rest of the programs (#4636)

This commit is contained in:
Jack May
2019-06-12 08:49:59 -07:00
committed by GitHub
parent dd1c3514a8
commit 0dcdc37fec
19 changed files with 52 additions and 57 deletions

View File

@@ -0,0 +1,40 @@
use crate::alloc;
use alloc::{Alloc, AllocErr};
use std::alloc::{self as system_alloc, Layout};
#[derive(Debug)]
pub struct BPFAllocator {
allocated: usize,
size: usize,
}
impl BPFAllocator {
pub fn new(heap: Vec<u8>) -> Self {
Self {
allocated: 0,
size: heap.len(),
}
}
}
impl Alloc for BPFAllocator {
fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
if self.allocated + layout.size() <= self.size {
let ptr = unsafe { system_alloc::alloc(layout) };
if !ptr.is_null() {
self.allocated += layout.size();
return Ok(ptr);
}
}
Err(AllocErr)
}
#[allow(clippy::not_unsafe_ptr_arg_deref)]
fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
self.allocated -= layout.size();
unsafe {
system_alloc::dealloc(ptr, layout);
}
}
}