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
Generated
+1
View File
@@ -596,6 +596,7 @@ name = "unistd"
version = "0.1.0"
dependencies = [
"cbindgen 0.5.2",
"errno 0.1.0",
"platform 0.1.0",
"stdio 0.1.0",
"string 0.1.0",
+1
View File
@@ -11,6 +11,7 @@ fn main() {
.flag("-I")
.flag(&format!("{}/include", crate_dir))
.flag("-fno-stack-protector")
.file("src/c/dlmalloc.c")
.file("src/c/fcntl.c")
.file("src/c/stdio.c")
.compile("relibc_c");
+6312
View File
File diff suppressed because it is too large Load Diff
-2
View File
@@ -47,5 +47,3 @@ int execle(const char *path, const char* argv0, ...)
return execve(path, argv, envp);
}
}
#endif
+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 {
+1
View File
@@ -8,6 +8,7 @@ build = "build.rs"
cbindgen = { path = "../../cbindgen" }
[dependencies]
errno = { path = "../errno" }
platform = { path = "../platform" }
stdio = { path = "../stdio" }
string = { path = "../string" }
+41
View File
@@ -0,0 +1,41 @@
use core::ptr;
use errno::ENOMEM;
use platform;
use platform::types::*;
static mut BRK: *mut c_void = ptr::null_mut();
#[no_mangle]
pub unsafe extern "C" fn brk(addr: *mut c_void) -> c_int {
BRK = platform::brk(addr);
if BRK < addr {
platform::errno = ENOMEM;
return -1;
}
0
}
#[no_mangle]
pub unsafe extern "C" fn sbrk(incr: intptr_t) -> *mut c_void {
if BRK == ptr::null_mut() {
BRK = platform::brk(ptr::null_mut());
}
let old_brk = BRK;
if incr != 0 {
let addr = old_brk.offset(incr);
BRK = platform::brk(addr);
if BRK < addr {
platform::errno = ENOMEM;
return -1isize as *mut c_void
}
}
old_brk as *mut c_void
}
+3 -10
View File
@@ -5,16 +5,19 @@
#[cfg(target_os = "redox")]
extern crate alloc;
extern crate errno;
extern crate platform;
extern crate stdio;
extern crate string;
extern crate sys_utsname;
pub use brk::*;
pub use getopt::*;
pub use platform::types::*;
use core::ptr;
mod brk;
mod getopt;
pub const R_OK: c_int = 1;
@@ -53,11 +56,6 @@ pub extern "C" fn alarm(seconds: c_uint) -> c_uint {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn brk(addr: *mut c_void) -> c_int {
platform::brk(addr)
}
#[no_mangle]
pub extern "C" fn chdir(path: *const c_char) -> c_int {
platform::chdir(path)
@@ -469,11 +467,6 @@ pub extern "C" fn rmdir(path: *const c_char) -> c_int {
platform::rmdir(path)
}
#[no_mangle]
pub extern "C" fn sbrk(incr: intptr_t) -> *mut c_void {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn setgid(gid: gid_t) -> c_int {
platform::setregid(gid, gid)