use core::ffi::c_void;
use core::ptr;
use crate::{Alignment, Allocator};
use super::C_ALLOCATOR as c_allocator;
pub static ALLOCATOR: Z = Z;
#[derive(Clone, Copy, Default)]
pub struct Z;
impl Allocator for Z {}
impl Z {
pub fn alloc(
&self, len: usize,
alignment: Alignment,
return_address: usize,
) -> Option<*mut u8> {
let result = c_allocator.raw_alloc(len, alignment, return_address)?;
unsafe { ptr::write_bytes(result, 0, len) };
Some(result)
}
pub fn resize(
&self, buf: &mut [u8],
alignment: Alignment,
new_len: usize,
return_address: usize,
) -> bool {
if !c_allocator.raw_resize(buf, alignment, new_len, return_address) {
return false;
}
let old_len = buf.len();
if new_len > old_len {
unsafe { ptr::write_bytes(buf.as_mut_ptr().add(old_len), 0, new_len - old_len) };
}
true
}
pub fn remap(
self,
_buf: &mut [u8],
_alignment: Alignment,
_new_len: usize,
_return_address: usize,
) -> Option<*mut u8> {
None
}
pub fn free(
self, buf: &mut [u8],
alignment: Alignment,
return_address: usize,
) {
c_allocator.raw_free(buf, alignment, return_address);
}
}
const _: fn() -> *mut c_void = || ptr::null_mut();