Skip to content

Commit

Permalink
Add VM user data
Browse files Browse the repository at this point in the history
  • Loading branch information
foonathan committed Dec 8, 2023
1 parent fe4a48d commit 227ec99
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/lauf/runtime/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ typedef enum lauf_runtime_fiber_status
//=== queries ===//
/// The VM that is executing the program.
lauf_vm* lauf_runtime_get_vm(lauf_runtime_process* process);
/// The user data of the VM that is executing the program.
void* lauf_runtime_get_vm_user_data(lauf_runtime_process* process);

/// The program that is running.
const lauf_asm_program* lauf_runtime_get_program(lauf_runtime_process* process);
Expand Down
8 changes: 8 additions & 0 deletions include/lauf/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ typedef struct lauf_vm_options
lauf_vm_panic_handler panic_handler;
/// The allocator used when the program wants to allocate heap memory.
lauf_vm_allocator allocator;

// Arbitrary user data.
void* user_data;
} lauf_vm_options;

extern const lauf_vm_options lauf_default_vm_options;
Expand All @@ -68,6 +71,11 @@ lauf_vm_allocator lauf_vm_set_allocator(lauf_vm* vm, lauf_vm_allocator a);
/// Returns the allocator.
lauf_vm_allocator lauf_vm_get_allocator(lauf_vm* vm);

/// Exchanges the user data.
void* lauf_vm_set_user_data(lauf_vm* vm, void* user_data);
/// Returns the user data.
void* lauf_vm_get_user_data(lauf_vm* vm);

/// Starts a new process for the program.
///
/// It creates a fiber for the entry function and turns it into the current fiber,
Expand Down
5 changes: 5 additions & 0 deletions src/lauf/runtime/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ lauf_vm* lauf_runtime_get_vm(lauf_runtime_process* process)
return process->vm;
}

void* lauf_runtime_get_vm_user_data(lauf_runtime_process* process)
{
return process->vm->user_data;
}

const lauf_asm_program* lauf_runtime_get_program(lauf_runtime_process* process)
{
return &process->program;
Expand Down
16 changes: 15 additions & 1 deletion src/lauf/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const lauf_vm_allocator lauf_vm_malloc_allocator
[](void*, void* memory, size_t) { std::free(memory); }};

const lauf_vm_options lauf_default_vm_options = [] {
lauf_vm_options result;
lauf_vm_options result{};

result.initial_vstack_size_in_elements = 1024ull;
result.max_vstack_size_in_elements = 16 * 1024ull;
Expand All @@ -38,6 +38,8 @@ const lauf_vm_options lauf_default_vm_options = [] {

result.allocator = lauf_vm_malloc_allocator;

result.user_data = nullptr;

return result;
}();

Expand Down Expand Up @@ -70,6 +72,18 @@ lauf_vm_allocator lauf_vm_get_allocator(lauf_vm* vm)
return vm->heap_allocator;
}

void* lauf_vm_set_user_data(lauf_vm* vm, void* user_data)
{
auto old = vm->user_data;
vm->user_data = user_data;
return old;
}

void* lauf_vm_get_user_data(lauf_vm* vm)
{
return vm->user_data;
}

lauf_runtime_process* lauf_vm_start_process(lauf_vm* vm, const lauf_asm_program* program)
{
auto fn = program->_entry;
Expand Down
4 changes: 3 additions & 1 deletion src/lauf/vm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ struct lauf_vm : lauf::intrinsic_arena<lauf_vm>
std::size_t step_limit;

lauf_runtime_process process;
void* user_data;

explicit lauf_vm(lauf::arena_key key, lauf_vm_options options)
: lauf::intrinsic_arena<lauf_vm>(key), panic_handler(options.panic_handler),
heap_allocator(options.allocator),
initial_vstack_size(options.initial_vstack_size_in_elements),
max_vstack_size(options.max_vstack_size_in_elements),
initial_cstack_size(options.initial_cstack_size_in_bytes),
max_cstack_size(options.max_cstack_size_in_bytes), step_limit(options.step_limit)
max_cstack_size(options.max_cstack_size_in_bytes), step_limit(options.step_limit),
user_data(options.user_data)
{}

~lauf_vm()
Expand Down

0 comments on commit 227ec99

Please sign in to comment.