mirror of
https://github.com/bdwgc/bdwgc-rust.git
synced 2026-05-29 18:08:56 -06:00
Refactor (#253)
This commit is contained in:
11
src/error.rs
11
src/error.rs
@@ -1,3 +1,8 @@
|
|||||||
|
use std::{
|
||||||
|
error,
|
||||||
|
fmt::{self, Display, Formatter},
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Error {
|
pub struct Error {
|
||||||
description: &'static str,
|
description: &'static str,
|
||||||
@@ -9,13 +14,13 @@ impl Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Error {
|
impl Display for Error {
|
||||||
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
|
||||||
write!(formatter, "{}", self.description)
|
write!(formatter, "{}", self.description)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for Error {
|
impl error::Error for Error {
|
||||||
fn description(&self) -> &str {
|
fn description(&self) -> &str {
|
||||||
self.description
|
self.description
|
||||||
}
|
}
|
||||||
|
|||||||
21
src/lib.rs
21
src/lib.rs
@@ -1,9 +1,12 @@
|
|||||||
extern crate libc;
|
#![doc = include_str!("../README.md")]
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
|
|
||||||
|
use alloc::alloc::{GlobalAlloc, Layout};
|
||||||
|
use core::ptr::null;
|
||||||
use libc::{c_int, c_void, size_t};
|
use libc::{c_int, c_void, size_t};
|
||||||
use std::alloc::{GlobalAlloc, Layout};
|
|
||||||
|
|
||||||
const GC_SUCCESS: c_int = 0;
|
const GC_SUCCESS: c_int = 0;
|
||||||
|
|
||||||
@@ -53,9 +56,7 @@ impl Allocator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn register_current_thread() -> Result<(), error::Error> {
|
pub unsafe fn register_current_thread() -> Result<(), error::Error> {
|
||||||
let mut base = GcStackBase {
|
let mut base = GcStackBase { mem_base: null() };
|
||||||
mem_base: std::ptr::null(),
|
|
||||||
};
|
|
||||||
|
|
||||||
if GC_get_stack_base(&mut base) != GC_SUCCESS {
|
if GC_get_stack_base(&mut base) != GC_SUCCESS {
|
||||||
return Err(error::Error::new("failed to get stack base"));
|
return Err(error::Error::new("failed to get stack base"));
|
||||||
@@ -68,7 +69,7 @@ impl Allocator {
|
|||||||
|
|
||||||
pub unsafe fn set_stack_bottom(bottom: *const u8) {
|
pub unsafe fn set_stack_bottom(bottom: *const u8) {
|
||||||
GC_set_stackbottom(
|
GC_set_stackbottom(
|
||||||
std::ptr::null(),
|
null(),
|
||||||
&GcStackBase {
|
&GcStackBase {
|
||||||
mem_base: bottom as *const libc::c_void,
|
mem_base: bottom as *const libc::c_void,
|
||||||
},
|
},
|
||||||
@@ -88,13 +89,7 @@ impl Allocator {
|
|||||||
finalizer: extern "C" fn(*mut c_void, *mut c_void),
|
finalizer: extern "C" fn(*mut c_void, *mut c_void),
|
||||||
client_data: *const c_void,
|
client_data: *const c_void,
|
||||||
) {
|
) {
|
||||||
GC_register_finalizer(
|
GC_register_finalizer(ptr, finalizer, client_data, null(), null());
|
||||||
ptr,
|
|
||||||
finalizer,
|
|
||||||
client_data,
|
|
||||||
std::ptr::null(),
|
|
||||||
std::ptr::null(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user