use std::alloc::{GlobalAlloc, Layout};
use crate::raw;
fn allocation_free_panic(message: &'static str) -> ! {
use std::os::unix::io::AsRawFd;
let _ = nix::unistd::write(std::io::stderr().as_raw_fd(), message.as_bytes());
std::process::abort();
}
const REDIS_ALLOCATOR_NOT_AVAILABLE_MESSAGE: &str =
"Critical error: the Redis Allocator isn't available.\n";
#[derive(Copy, Clone)]
pub struct RedisAlloc;
unsafe impl GlobalAlloc for RedisAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let size = (layout.size() + layout.align() - 1) & (!(layout.align() - 1));
match raw::RedisModule_Alloc {
Some(alloc) => alloc(size).cast(),
None => allocation_free_panic(REDIS_ALLOCATOR_NOT_AVAILABLE_MESSAGE),
}
}
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
match raw::RedisModule_Free {
Some(f) => f(ptr.cast()),
None => allocation_free_panic(REDIS_ALLOCATOR_NOT_AVAILABLE_MESSAGE),
};
}
}