forked from GitHub/bdwgc-rust
Initialize src
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
target
|
||||||
16
Cargo.lock
generated
Normal file
16
Cargo.lock
generated
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
[[package]]
|
||||||
|
name = "gc"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.51"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
|
[metadata]
|
||||||
|
"checksum libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)" = "bedcc7a809076656486ffe045abeeac163da1b558e963a31e29fbfbeba916917"
|
||||||
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "gc"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["swgillespie <sean.william.g@gmail.com>", "Yota Toyama <raviqqe@gmail.com>"]
|
||||||
|
publish = false
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
libc = "0.2"
|
||||||
22
src/error.rs
Normal file
22
src/error.rs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Error {
|
||||||
|
description: &'static str,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error {
|
||||||
|
pub fn new(description: &'static str) -> Error {
|
||||||
|
Error { description }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for Error {
|
||||||
|
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
write!(formatter, "{}", self.description)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for Error {
|
||||||
|
fn description(&self) -> &str {
|
||||||
|
self.description
|
||||||
|
}
|
||||||
|
}
|
||||||
79
src/lib.rs
Normal file
79
src/lib.rs
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
extern crate libc;
|
||||||
|
|
||||||
|
mod error;
|
||||||
|
|
||||||
|
use libc::{c_int, c_void, size_t};
|
||||||
|
use std::alloc::{GlobalAlloc, Layout};
|
||||||
|
|
||||||
|
const GC_SUCCESS: c_int = 0;
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
struct GCStackBase {
|
||||||
|
mem_base: *const c_void,
|
||||||
|
reg_base: *const c_void,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[link(name = "gc")]
|
||||||
|
extern "C" {
|
||||||
|
fn GC_allow_register_threads() -> c_void;
|
||||||
|
fn GC_free(ptr: *mut c_void);
|
||||||
|
fn GC_get_stack_base(stack_base: *mut GCStackBase) -> c_int;
|
||||||
|
fn GC_init() -> c_void;
|
||||||
|
fn GC_malloc(size: size_t) -> *mut c_void;
|
||||||
|
fn GC_malloc_uncollectable(size: size_t) -> *mut c_void;
|
||||||
|
fn GC_register_my_thread(stack_base: *const GCStackBase) -> c_int;
|
||||||
|
fn GC_unregister_my_thread();
|
||||||
|
}
|
||||||
|
|
||||||
|
static mut GC_STARTED: bool = false;
|
||||||
|
pub struct Allocator;
|
||||||
|
|
||||||
|
impl Allocator {
|
||||||
|
pub unsafe fn initialize() {
|
||||||
|
GC_init();
|
||||||
|
GC_allow_register_threads();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub unsafe fn start_gc() {
|
||||||
|
GC_STARTED = true
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn register_current_thread() -> Result<(), error::Error> {
|
||||||
|
let mut base = GCStackBase {
|
||||||
|
mem_base: std::ptr::null(),
|
||||||
|
reg_base: std::ptr::null(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if unsafe { GC_get_stack_base(&mut base) } != GC_SUCCESS {
|
||||||
|
return Err(error::Error::new("failed to get stack base"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if unsafe { GC_register_my_thread(&base) } != GC_SUCCESS {
|
||||||
|
return Err(error::Error::new("failed to register a thread for GC"));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unregister_current_thread() {
|
||||||
|
unsafe { GC_unregister_my_thread() }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn alloc(size: usize) -> *mut u8 {
|
||||||
|
unsafe { Allocator.alloc(Layout::from_size_align_unchecked(size, 8)) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe impl GlobalAlloc for Allocator {
|
||||||
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||||
|
return if GC_STARTED {
|
||||||
|
GC_malloc(layout.size())
|
||||||
|
} else {
|
||||||
|
GC_malloc_uncollectable(layout.size())
|
||||||
|
} as *mut u8;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
|
||||||
|
GC_free(ptr as *mut c_void)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user