Skip to content

Commit 35ea345

Browse files
committed
Add descriptions.
1 parent ed6220f commit 35ea345

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

src/coroutine/stack.rs

+25
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ const STACK_LAYOUT: Result<Layout, LayoutError> =
1212

1313
static STACK_STORE: Lazy<Mutex<Vec<Stack>>> = Lazy::new(|| Mutex::new(Vec::new()));
1414

15+
///
16+
/// Machine stack handle for Fiber.
17+
///
18+
/// `Stack` is a wrapper of a raw pointer which points to the top of machine stack area
19+
/// of a Fiber object.
20+
/// The stack area is newly allocated when a Fiber object is 'resume'd at the first time
21+
/// after created.
22+
///
23+
/// Default size of the stack area is DEFAULT_SIZE bytes(currently, 512KiB).
24+
///
1525
#[derive(Clone, Copy, Debug, PartialEq)]
1626
#[repr(transparent)]
1727
pub struct Stack(*mut u8);
@@ -24,6 +34,10 @@ impl Stack {
2434
Self(std::ptr::null_mut())
2535
}
2636

37+
/// Allocate new stack area.
38+
///
39+
/// If some `Stack` were saved in `STACK_STORE`, the newest one is returned.
40+
/// Otherwise, allocate new `Stack` and return it.
2741
pub(crate) fn allocate() -> Self {
2842
match STACK_STORE.lock().unwrap().pop() {
2943
None => unsafe {
@@ -36,6 +50,10 @@ impl Stack {
3650
}
3751
}
3852

53+
/// Deallocate `Stack`.
54+
///
55+
/// Currently, when a Fiber object was disposed by GC, associated `Stack` is returned
56+
/// to `STACK_STORE`.
3957
pub(crate) fn deallocate(&mut self) {
4058
if self.0.is_null() {
4159
return;
@@ -44,6 +62,13 @@ impl Stack {
4462
self.0 = std::ptr::null_mut();
4563
}
4664

65+
/// Initialize `Stack`.
66+
///
67+
/// Addresses of some functions are stored at the bottom of the stack area.
68+
///
69+
/// - `new_context` is to be called when the Fiber coroutine is 'resume'd at the first time.
70+
/// - `guard` is to be called when 'resume'd **after** the Fiber coroutine execution was finished.
71+
/// - a pointer which points FiberContext is placed at the very bottom of the stack.
4772
pub(crate) fn init(&mut self, fiber: *const FiberContext) -> u64 {
4873
unsafe {
4974
let s_ptr = self.0.offset(DEFAULT_STACK_SIZE as isize);

src/vm/executor/frame.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ impl Frame {
6262
}
6363
}
6464

65+
///
66+
/// Control frame
67+
///
68+
/// This is a wrapped raw pointer which points to a certain point within `RubyStack`.
69+
/// You can obtain or alter various information like cfp, lfp, and the number of local variables
70+
/// in the frame through `ControlFrame`.
71+
///
6572
#[derive(Debug, Clone, Copy, PartialEq)]
6673
pub struct ControlFrame(*mut Value);
6774

@@ -683,17 +690,18 @@ impl VM {
683690
///
684691
/// ### After
685692
///~~~~text
686-
/// lfp cfp sp
687-
/// v v v
688-
/// +------+------+--+------+------+------+------+-----
689-
/// | a0 | a1 |..| an | self | flg | cfp* |
690-
/// +------+------+--+------+------+------+------+-----
693+
/// lfp cfp sp
694+
/// v v v
695+
/// +------+------+--+------+------+------+------+-----+---
696+
/// | a0 | a1 |..| an | self | cfp* | lfp | flg |
697+
/// +------+------+--+------+------+------+------+-----+---
691698
/// <-------- local frame --------> <---------->
692699
/// native control frame
693700
///~~~~
694701
///
695-
/// - flg: flags
696702
/// - cfp*: prev cfp
703+
/// - lfp: lfp
704+
/// - flg: flags
697705
///
698706
pub(crate) fn prepare_native_frame(&mut self, args_len: usize) {
699707
self.save_next_pc();

src/vm/executor/ruby_stack.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ use std::pin::Pin;
55

66
pub(super) const VM_STACK_SIZE: usize = 8192;
77

8+
///
9+
/// Ruruby exection stack.
10+
///
11+
/// Ruruby stack is implemented as a fixed and pinned heap array of `Value`s.
12+
/// You can do operations like push, pop, or extend, as if it was a Vec.
13+
///
14+
/// Stack size is VM_STACK_SIZE(currently, 8192 `Value`s).
815
#[derive(Clone)]
916
pub(super) struct RubyStack {
10-
//len: usize,
17+
/// Stack pointer.
1118
pub sp: StackPtr,
19+
/// Pinned Buffer.
1220
buf: Pin<Box<[Value]>>,
1321
}
1422

@@ -49,6 +57,7 @@ impl IndexMut<Range<usize>> for RubyStack {
4957
}
5058

5159
impl RubyStack {
60+
/// Allocate new `RubyStack`.
5261
pub(super) fn new() -> Self {
5362
let mut inner = unsafe { Box::new_uninit_slice(VM_STACK_SIZE).assume_init() };
5463
let sp = StackPtr::from(inner.as_mut_ptr());
@@ -58,33 +67,43 @@ impl RubyStack {
5867
}
5968
}
6069

70+
/// Set SP to `new_len`.
6171
unsafe fn set_len(&mut self, new_len: usize) {
6272
self.sp = StackPtr::from(self.buf.as_mut_ptr()) + new_len;
6373
}
6474

75+
/// Increment SP.
6576
unsafe fn inc_len(&mut self, offset: usize) {
6677
self.sp = self.sp + offset;
6778
}
6879

80+
/// Decrement SP.
6981
unsafe fn dec_len(&mut self, offset: usize) {
7082
self.sp = self.sp - offset;
7183
}
7284

85+
/// Get length of stack.
86+
/// This is as same as the index of SP in the stack.
7387
pub(super) fn len(&self) -> usize {
7488
let len = unsafe { self.sp.as_ptr().offset_from(self.buf.as_ptr()) };
7589
assert!(len >= 0);
7690
len as usize
7791
}
7892

93+
/// Shortens the stack.
94+
/// If len is greater than the current length, this has no effect.
7995
pub(super) fn truncate(&mut self, len: usize) {
8096
if len >= self.len() {
8197
return;
8298
}
8399
unsafe { self.set_len(len) };
84100
}
85101

102+
/// Resize the stack so that len is equal to new_len.
103+
/// If new_len is greater than len, the stack is extended by the difference, with each additional slot filled with Nil.
104+
/// If new_len is less than len, the stack is simply truncated.
86105
pub(super) fn resize(&mut self, new_len: usize) {
87-
debug_assert!(new_len <= VM_STACK_SIZE);
106+
assert!(new_len <= VM_STACK_SIZE);
88107
let len = self.len();
89108
if new_len > len {
90109
self.buf[len..new_len].fill(Value::nil());

0 commit comments

Comments
 (0)