From 9a1efda1219f3949a28679ab76ab05caa8765154 Mon Sep 17 00:00:00 2001 From: oddcoder <ahmedsoliman@oddcoder.com> Date: Wed, 15 Jul 2020 10:12:59 +0200 Subject: [PATCH] Initial allocator structure --- src/lib.rs | 4 ++-- src/platform/allocator/dlmalloc.rs | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 589ccb38a..9425e7d14 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,10 +59,10 @@ pub mod platform; pub mod start; pub mod sync; -use crate::platform::{Allocator, Pal, Sys}; +use crate::platform::{Allocator, Pal, Sys, NEWALLOCATOR}; #[global_allocator] -static ALLOCATOR: Allocator = Allocator; +static ALLOCATOR: Allocator = NEWALLOCATOR; #[no_mangle] pub extern "C" fn relibc_panic(pi: &::core::panic::PanicInfo) -> ! { diff --git a/src/platform/allocator/dlmalloc.rs b/src/platform/allocator/dlmalloc.rs index a5a890f7d..97acd7fa6 100644 --- a/src/platform/allocator/dlmalloc.rs +++ b/src/platform/allocator/dlmalloc.rs @@ -1,4 +1,7 @@ -use core::alloc::{GlobalAlloc, Layout}; +use core::{ + alloc::{GlobalAlloc, Layout}, + sync::atomic::{AtomicUsize, Ordering}, +}; use super::types::*; @@ -9,8 +12,22 @@ extern "C" { fn dlfree(mem: *mut c_void); } -pub struct Allocator; +pub struct Allocator { + mstate: AtomicUsize, +} + +pub const NEWALLOCATOR:Allocator = Allocator{ + mstate: AtomicUsize::new(0), +}; +impl Allocator { + pub fn set_bookkeeper(&self, mstate: usize) { + self.mstate.store(mstate, Ordering::Relaxed); + } + fn get_bookKeeper(&self) ->usize { + self.mstate.load(Ordering::Relaxed) + } +} unsafe impl<'a> GlobalAlloc for Allocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { alloc_align(layout.size(), layout.align()) as *mut u8 -- GitLab