Add dlmalloc

This commit is contained in:
Jeremy Soller
2018-07-04 10:10:34 -06:00
parent 3ef9599af5
commit ea5f8d59de
13 changed files with 6462 additions and 73 deletions
+1
View File
@@ -6,6 +6,7 @@ authors = ["Jeremy Soller <jackpot51@gmail.com>"]
[dependencies.ralloc]
path = "../../ralloc"
default-features = false
optional = true
[target.'cfg(target_os = "linux")'.dependencies]
sc = "0.2"
+38
View File
@@ -0,0 +1,38 @@
use core::alloc::{GlobalAlloc, Layout};
use types::*;
extern "C" {
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;
unsafe impl<'a> GlobalAlloc for Allocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
dlmemalign(layout.align(), layout.size()) as *mut u8
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
dlfree(ptr as *mut c_void)
}
}
pub unsafe fn alloc(size: usize) -> *mut c_void {
dlmalloc(size)
}
pub unsafe fn alloc_align(size: usize, alignment: usize) -> *mut c_void {
dlmemalign(alignment, size)
}
pub unsafe fn realloc(ptr: *mut c_void, size: size_t) -> *mut c_void {
dlrealloc(ptr, size)
}
pub unsafe fn free(ptr: *mut c_void) {
dlfree(ptr)
}
+48
View File
@@ -0,0 +1,48 @@
extern crate ralloc;
pub use ralloc::Allocator;
unsafe fn alloc_inner(size: usize, offset: usize, align: usize) -> *mut c_void {
let ptr = ralloc::alloc(size + offset, align);
if !ptr.is_null() {
*(ptr as *mut u64) = (size + offset) as u64;
*(ptr as *mut u64).offset(1) = align as u64;
ptr.offset(offset as isize) as *mut c_void
} else {
ptr as *mut c_void
}
}
pub unsafe fn alloc(size: usize) -> *mut c_void {
alloc_inner(size, 16, 8)
}
pub unsafe fn alloc_align(size: usize, alignment: usize) -> *mut c_void {
let mut align = 32;
while align <= alignment {
align *= 2;
}
alloc_inner(size, align/2, align)
}
pub unsafe fn realloc(ptr: *mut c_void, size: size_t) -> *mut c_void {
let old_ptr = (ptr as *mut u8).offset(-16);
let old_size = *(old_ptr as *mut u64);
let align = *(old_ptr as *mut u64).offset(1);
let ptr = ralloc::realloc(old_ptr, old_size as usize, size + 16, align as usize);
if !ptr.is_null() {
*(ptr as *mut u64) = (size + 16) as u64;
*(ptr as *mut u64).offset(1) = align;
ptr.offset(16) as *mut c_void
} else {
ptr as *mut c_void
}
}
pub unsafe fn free(ptr: *mut c_void) {
let ptr = (ptr as *mut u8).offset(-16);
let size = *(ptr as *mut u64);
let _align = *(ptr as *mut u64).offset(1);
ralloc::free(ptr, size as usize);
}
+12 -50
View File
@@ -1,11 +1,8 @@
//! fcntl implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/fcntl.h.html
#![no_std]
#![allow(non_camel_case_types)]
#![feature(allocator_api)]
//TODO #![feature(thread_local)]
extern crate ralloc;
#[cfg(all(not(feature = "no_std"), target_os = "linux"))]
#[macro_use]
extern crate sc;
@@ -14,6 +11,16 @@ extern crate sc;
#[macro_use]
pub extern crate syscall;
pub use alloc::*;
#[cfg(not(feature = "ralloc"))]
#[path = "alloc/dlmalloc.rs"]
mod alloc;
#[cfg(feature = "ralloc")]
#[path = "alloc/ralloc.rs"]
mod alloc;
pub use sys::*;
#[cfg(all(not(feature = "no_std"), target_os = "linux"))]
@@ -31,7 +38,7 @@ use core::fmt;
use types::*;
#[global_allocator]
static ALLOCATOR: ralloc::Allocator = ralloc::Allocator;
static ALLOCATOR: Allocator = Allocator;
//TODO #[thread_local]
#[allow(non_upper_case_globals)]
@@ -97,51 +104,6 @@ pub unsafe fn memcpy(s1: *mut c_void, s2: *const c_void, n: usize) -> *mut c_voi
s1
}
unsafe fn alloc_inner(size: usize, offset: usize, align: usize) -> *mut c_void {
let ptr = ralloc::alloc(size + offset, align);
if !ptr.is_null() {
*(ptr as *mut u64) = (size + offset) as u64;
*(ptr as *mut u64).offset(1) = align as u64;
ptr.offset(offset as isize) as *mut c_void
} else {
ptr as *mut c_void
}
}
pub unsafe fn alloc(size: usize) -> *mut c_void {
alloc_inner(size, 16, 8)
}
pub unsafe fn alloc_align(size: usize, alignment: usize) -> *mut c_void {
let mut align = 32;
while align <= alignment {
align *= 2;
}
alloc_inner(size, align/2, align)
}
pub unsafe fn realloc(ptr: *mut c_void, size: size_t) -> *mut c_void {
let old_ptr = (ptr as *mut u8).offset(-16);
let old_size = *(old_ptr as *mut u64);
let align = *(old_ptr as *mut u64).offset(1);
let ptr = ralloc::realloc(old_ptr, old_size as usize, size + 16, align as usize);
if !ptr.is_null() {
*(ptr as *mut u64) = (size + 16) as u64;
*(ptr as *mut u64).offset(1) = align;
ptr.offset(16) as *mut c_void
} else {
ptr as *mut c_void
}
}
pub unsafe fn free(ptr: *mut c_void) {
let ptr = (ptr as *mut u8).offset(-16);
let size = *(ptr as *mut u64);
let _align = *(ptr as *mut u64).offset(1);
ralloc::free(ptr, size as usize);
}
pub trait Write: fmt::Write {
fn write_u8(&mut self, byte: u8) -> fmt::Result;
}
+2 -9
View File
@@ -19,15 +19,8 @@ pub fn e(sys: usize) -> usize {
}
}
pub fn brk(addr: *const c_void) -> c_int {
unsafe {
let newbrk = syscall!(BRK, addr);
if newbrk < addr as usize {
-1
} else {
0
}
}
pub fn brk(addr: *mut c_void) -> *mut c_void {
unsafe { syscall!(BRK, addr) as *mut c_void }
}
pub fn chdir(path: *const c_char) -> c_int {
+2 -2
View File
@@ -22,8 +22,8 @@ pub fn e(sys: Result<usize, syscall::Error>) -> usize {
}
}
pub fn brk(addr: *const c_void) -> c_int {
e(unsafe { syscall::brk(addr as usize) }) as c_int
pub fn brk(addr: *mut c_void) -> *mut c_void {
unsafe { syscall::brk(addr as usize) } as *mut c_void
}
pub fn chdir(path: *const c_char) -> c_int {