Skip to content

Commit 2f815e3

Browse files
committed
Merge branch 'frame'
2 parents 35ea345 + 9fe44c3 commit 2f815e3

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

src/alloc.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
use crate::*;
22
use std::alloc::{GlobalAlloc, Layout, System};
33
use std::cell::RefCell;
4+
use std::sync::atomic::{AtomicUsize, Ordering};
5+
6+
struct MyAllocator;
7+
8+
unsafe impl GlobalAlloc for MyAllocator {
9+
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
10+
MALLOC_AMOUNT.fetch_add(layout.size(), Ordering::SeqCst);
11+
System.alloc(layout)
12+
}
13+
14+
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
15+
MALLOC_AMOUNT.fetch_sub(layout.size(), Ordering::SeqCst);
16+
System.dealloc(ptr, layout)
17+
}
18+
}
19+
20+
#[global_allocator]
21+
static GLOBAL: MyAllocator = MyAllocator;
22+
23+
pub static MALLOC_AMOUNT: AtomicUsize = AtomicUsize::new(0);
424

525
thread_local!(
626
pub static ALLOC: RefCell<Allocator> = RefCell::new(Allocator::new());
@@ -17,14 +37,12 @@ pub trait GC {
1737
fn mark(&self, alloc: &mut Allocator);
1838
}
1939

20-
///-----------------------------------------------------------------------------------------------------------------
2140
///
2241
/// Heap page struct.
2342
///
2443
/// Single page occupies `ALLOC_SIZE` bytes in memory.
2544
/// This struct contains 64 * (`SIZE` - 1) `GCBox` cells, and bitmap (`SIZE` - 1 bytes each) for marking phase.
2645
///
27-
///-----------------------------------------------------------------------------------------------------------------
2846
struct Page {
2947
data: [GCBox<RValue>; DATA_LEN],
3048
mark_bits: [u64; SIZE - 1],

src/coroutine/stack.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use super::*;
22
use once_cell::sync::Lazy;
33
use region::{protect, Protection};
4-
use std::{
5-
alloc::{alloc, Layout, LayoutError},
6-
sync::Mutex,
7-
};
4+
use std::alloc::{GlobalAlloc, Layout, LayoutError, System};
5+
use std::sync::Mutex;
86

97
const DEFAULT_STACK_SIZE: usize = 1024 * 512;
108
const STACK_LAYOUT: Result<Layout, LayoutError> =
@@ -41,7 +39,7 @@ impl Stack {
4139
pub(crate) fn allocate() -> Self {
4240
match STACK_STORE.lock().unwrap().pop() {
4341
None => unsafe {
44-
let stack = alloc(STACK_LAYOUT.unwrap());
42+
let stack = System.alloc(STACK_LAYOUT.unwrap());
4543
protect(stack, DEFAULT_STACK_SIZE, Protection::READ_WRITE)
4644
.expect("Mprotect failed.");
4745
Stack(stack)

src/vm/executor.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -469,23 +469,23 @@ impl VM {
469469

470470
impl VM {
471471
fn gc(&mut self) {
472-
//let malloced = MALLOC_AMOUNT.with(|x| x.borrow().clone());
472+
let malloced = MALLOC_AMOUNT.load(std::sync::atomic::Ordering::SeqCst);
473473
let (object_trigger, malloc_trigger) = ALLOC.with(|m| {
474474
let m = m.borrow();
475-
(m.is_allocated(), false)
475+
(m.is_allocated(), m.malloc_threshold < malloced)
476476
});
477477
if !object_trigger && !malloc_trigger {
478478
return;
479479
}
480480
#[cfg(feature = "perf")]
481481
self.globals.perf.get_perf(Perf::GC);
482482
self.globals.gc();
483-
/*if malloc_trigger {
484-
let malloced = MALLOC_AMOUNT.with(|x| x.borrow().clone());
483+
if malloc_trigger {
484+
let malloced = MALLOC_AMOUNT.load(std::sync::atomic::Ordering::SeqCst);
485485
if malloced > 0 {
486486
ALLOC.with(|m| m.borrow_mut().malloc_threshold = (malloced * 2) as usize);
487487
}
488-
}*/
488+
}
489489
}
490490

491491
fn jmp_cond(&mut self, cond: bool, inst_offset: usize, dest_offset: usize) {

0 commit comments

Comments
 (0)