Skip to content

Commit

Permalink
Change vm_resize_heap into vm_grow_heap
Browse files Browse the repository at this point in the history
Makes intention more explicit. Safer for concurrent malloc.
  • Loading branch information
maximecb committed Aug 31, 2024
1 parent d9e51a8 commit ffa7550
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 23 deletions.
4 changes: 2 additions & 2 deletions api/syscalls.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
"description": "Report the current heap size in bytes."
},
{
"name": "vm_resize_heap",
"name": "vm_grow_heap",
"args": [
[
"u64",
Expand All @@ -124,7 +124,7 @@
],
"permission": "default_allowed",
"const_idx": 17,
"description": "Resize the heap to a new size given in bytes. This is similar to the `brk()` system call on POSIX systems. Note that the heap may be resized to a size larger than requested. The heap size is guaranteed to be a multiple of 8 bytes. Returns the new heap size in bytes if successful, or `UINT64_MAX` on failure."
"description": "Grow the heap to a new size given in bytes. This is similar to the `brk()` system call on POSIX systems. Note that the heap may be resized to a size larger than requested. The heap size is guaranteed to be a multiple of 8 bytes. If the requested size is smaller than the current heap size, this is a no-op. Returns the new heap size in bytes."
}
],
"constants": []
Expand Down
6 changes: 3 additions & 3 deletions doc/syscalls.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ u64 vm_heap_size()

Report the current heap size in bytes.

## vm_resize_heap
## vm_grow_heap

```
u64 vm_resize_heap(u64 num_bytes)
u64 vm_grow_heap(u64 num_bytes)
```

**Returns:** `u64 new_size`

Resize the heap to a new size given in bytes. This is similar to the `brk()` system call on POSIX systems. Note that the heap may be resized to a size larger than requested. The heap size is guaranteed to be a multiple of 8 bytes. Returns the new heap size in bytes if successful, or `UINT64_MAX` on failure.
Grow the heap to a new size given in bytes. This is similar to the `brk()` system call on POSIX systems. Note that the heap may be resized to a size larger than requested. The heap size is guaranteed to be a multiple of 8 bytes. If the requested size is smaller than the current heap size, this is a no-op. Returns the new heap size in bytes.

# io

Expand Down
6 changes: 3 additions & 3 deletions ncc/include/uvm/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
// Report the current heap size in bytes.
#define vm_heap_size() asm () -> u64 { syscall vm_heap_size; }

// u64 vm_resize_heap(u64 num_bytes)
// Resize the heap to a new size given in bytes. This is similar to the `brk()` system call on POSIX systems. Note that the heap may be resized to a size larger than requested. The heap size is guaranteed to be a multiple of 8 bytes. Returns the new heap size in bytes if successful, or `UINT64_MAX` on failure.
#define vm_resize_heap(__num_bytes) asm (__num_bytes) -> u64 { syscall vm_resize_heap; }
// u64 vm_grow_heap(u64 num_bytes)
// Grow the heap to a new size given in bytes. This is similar to the `brk()` system call on POSIX systems. Note that the heap may be resized to a size larger than requested. The heap size is guaranteed to be a multiple of 8 bytes. If the requested size is smaller than the current heap size, this is a no-op. Returns the new heap size in bytes.
#define vm_grow_heap(__num_bytes) asm (__num_bytes) -> u64 { syscall vm_grow_heap; }

// void print_i64(i64 val)
// Print an i64 value to standard output.
Expand Down
4 changes: 2 additions & 2 deletions vm/src/sys/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub const WINDOW_ON_MOUSEUP: u16 = 13;
pub const VM_HEAP_SIZE: u16 = 14;
pub const WINDOW_ON_KEYUP: u16 = 15;
pub const MEMSET32: u16 = 16;
pub const VM_RESIZE_HEAP: u16 = 17;
pub const VM_GROW_HEAP: u16 = 17;
pub const AUDIO_OPEN_OUTPUT: u16 = 18;
pub const WINDOW_ON_TEXTINPUT: u16 = 19;
pub const PRINT_F32: u16 = 20;
Expand Down Expand Up @@ -61,7 +61,7 @@ pub const SYSCALL_DESCS: [Option<SysCallDesc>; SYSCALL_TBL_LEN] = [
Some(SysCallDesc { name: "vm_heap_size", const_idx: 14, argc: 0, has_ret: true }),
Some(SysCallDesc { name: "window_on_keyup", const_idx: 15, argc: 2, has_ret: false }),
Some(SysCallDesc { name: "memset32", const_idx: 16, argc: 3, has_ret: false }),
Some(SysCallDesc { name: "vm_resize_heap", const_idx: 17, argc: 1, has_ret: true }),
Some(SysCallDesc { name: "vm_grow_heap", const_idx: 17, argc: 1, has_ret: true }),
Some(SysCallDesc { name: "audio_open_output", const_idx: 18, argc: 4, has_ret: true }),
Some(SysCallDesc { name: "window_on_textinput", const_idx: 19, argc: 2, has_ret: false }),
Some(SysCallDesc { name: "print_f32", const_idx: 20, argc: 1, has_ret: false }),
Expand Down
12 changes: 4 additions & 8 deletions vm/src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,12 @@ pub fn get_syscall(const_idx: u16) -> SysCallFn
match const_idx {
// Core VM syscalls
VM_HEAP_SIZE => SysCallFn::Fn0_1(vm_heap_size),
VM_RESIZE_HEAP => SysCallFn::Fn1_1(vm_resize_heap),
VM_GROW_HEAP => SysCallFn::Fn1_1(vm_grow_heap),
MEMSET => SysCallFn::Fn3_0(memset),
MEMSET32 => SysCallFn::Fn3_0(memset32),
MEMCPY => SysCallFn::Fn3_0(memcpy),
MEMCMP => SysCallFn::Fn3_1(memcmp),


//thread_id
//thread_sleep
//thread_spawn
Expand Down Expand Up @@ -140,15 +139,12 @@ fn vm_heap_size(thread: &mut Thread) -> Value
//Value::from(vm.heap_size())
}

fn vm_resize_heap(thread: &mut Thread, num_bytes: Value) -> Value
fn vm_grow_heap(thread: &mut Thread, num_bytes: Value) -> Value
{
todo!();

/*
let mut vm = thread.vm.lock().unwrap();
let num_bytes = num_bytes.as_usize();
let new_size = vm.resize_heap(num_bytes);
let new_size = vm.grow_heap(num_bytes);
Value::from(new_size)
*/
}

fn thread_id(thread: &mut Thread) -> Value
Expand Down
10 changes: 5 additions & 5 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1670,13 +1670,13 @@ impl VM
vm
}

/*
/// Resize the heap to a new size in bytes
pub fn resize_heap(&mut self, num_bytes: usize) -> usize
/// Grow the heap to a new size in bytes
pub fn grow_heap(&mut self, num_bytes: usize) -> usize
{
self.heap.resize(num_bytes)
todo!();

//self.heap.resize(num_bytes)
}
*/

// Create a new thread
pub fn new_thread(vm: &Arc<Mutex<VM>>, fun: Value, args: Vec<Value>) -> u64
Expand Down

0 comments on commit ffa7550

Please sign in to comment.