Split BPF loader to match the rest of the programs (#4636)
This commit is contained in:
40
programs/bpf_loader_api/src/allocator_system.rs
Normal file
40
programs/bpf_loader_api/src/allocator_system.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user