Make use of mspaces

This commit is contained in:
oddcoder
2020-07-16 19:59:42 +02:00
parent 3a8817072c
commit f4f68a3441
2 changed files with 33 additions and 13 deletions
+13 -8
View File
@@ -2,15 +2,20 @@ use core::{
alloc::{GlobalAlloc, Layout},
sync::atomic::{AtomicUsize, Ordering},
};
use crate::ALLOCATOR;
use super::types::*;
extern "C" {
fn create_mspace(capacity: size_t, locked: c_int) -> usize;
fn dlmalloc(bytes: size_t) -> *mut c_void;
fn dlmemalign(alignment: size_t, bytes: size_t) -> *mut c_void;
fn dlrealloc(oldmem: *mut c_void, bytes: size_t) -> *mut c_void;
fn dlfree(mem: *mut c_void);
fn mspace_malloc(msp: usize, bytes: size_t) -> *mut c_void;
fn mspace_memalign(msp: usize, alignment: size_t, bytes: size_t) -> *mut c_void;
fn mspace_realloc(msp: usize, oldmem: *mut c_void, bytes: size_t) -> *mut c_void;
fn mspace_free(msp: usize, mem: *mut c_void);
//fn dlmalloc(bytes: size_t) -> *mut c_void;
//fn dlmemalign(alignment: size_t, bytes: size_t) -> *mut c_void;
//fn dlrealloc(oldmem: *mut c_void, bytes: size_t) -> *mut c_void;
//fn dlfree(mem: *mut c_void);
}
pub struct Allocator {
@@ -40,19 +45,19 @@ unsafe impl<'a> GlobalAlloc for Allocator {
}
pub unsafe fn alloc(size: usize) -> *mut c_void {
dlmalloc(size)
mspace_malloc(ALLOCATOR.get_book_keeper(), size)
}
pub unsafe fn alloc_align(size: usize, alignment: usize) -> *mut c_void {
dlmemalign(alignment, size)
mspace_memalign(ALLOCATOR.get_book_keeper(), alignment, size)
}
pub unsafe fn realloc(ptr: *mut c_void, size: size_t) -> *mut c_void {
dlrealloc(ptr, size)
mspace_realloc(ALLOCATOR.get_book_keeper(), ptr, size)
}
pub unsafe fn free(ptr: *mut c_void) {
dlfree(ptr)
mspace_free(ALLOCATOR.get_book_keeper(), ptr)
}
pub fn new_mspace() -> usize {