Skip to content

Commit 4f5d2c1

Browse files
committed
Eliminate memory allocations in the system call setup_queue
Because the emulator lacks an address translation mechanism and is unable to allocate variables within the process's memory space, This commit grants the user the ability to create queues by passing an additional parameter to the system call that specifies the base address of queues.
1 parent e69cc1a commit 4f5d2c1

File tree

2 files changed

+8
-21
lines changed

2 files changed

+8
-21
lines changed

docs/syscall.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ If a window does not already exist, one will be created with the specified `widt
3131

3232
**system call number**: `0xC0DE`
3333

34-
**synopsis**: `void *setup_queue(int capacity, unsigned int* event_count)`
34+
**synopsis**: `void *setup_queue(void* base, int capacity, unsigned int* event_count)`
3535

36-
Allocate a continuous memory chunk that has the requested capacity and two closely packed queues, the event queue and the submission queue. The base address of the event queue is located in the returned address, and the submission queue is immediately after the event queue's last element, which is the event queue's base address plus the size of each event element times the given capacity. The capacity must be a power of 2; if it is not, it will be rounded up to the next highest power of 2. It is crucial to initialize the event counter variable before supplying its address to this system call because it serves as an interaction to the user that an event has been added to the event queue.
36+
The user must pass a continuous memory chunk that contains two tightly packed queues, the event queue and the submission queue. And the submission queue is immediately following the last element of the event queue, which is the event queue's base address plus the size of each event element multiplied by the given capacity. If the capacity is not a power of two, it will be treated as the rounded value of the next highest power of two. Additionally, because the event counter variable serves as a notifier to the user that an event has been added to the event queue, it is critical to initialize it before passing its address to this system call.
3737

3838
#### Events
3939

@@ -55,4 +55,4 @@ To inform the emulator that a batch of submissions should be processed, the appl
5555

5656
The submission entry is structured similarly to an event entry, with a 32-bit type field and an associated dynamic-sized value buffer whose width depends on the type of submission.
5757

58-
* `RELATIVE_MODE_SUBMISSION`: Enable or disable the mouse relative mode. If the mouse relative mode is enabled, the mouse cursor is wrapped within the window border, it's associated with an 8-bit wide boolean value that indicates whether the relative mouse mode should be enbled.
58+
* `RELATIVE_MODE_SUBMISSION`: Enable or disable the mouse relative mode. If the mouse relative mode is enabled, the mouse cursor is wrapped within the window border, it's associated with an 8-bit wide boolean value that indicates whether the relative mouse mode should be enbled.

src/syscall_sdl.c

+5-18
Original file line numberDiff line numberDiff line change
@@ -234,27 +234,14 @@ void syscall_draw_frame(struct riscv_t *rv)
234234

235235
void syscall_setup_queue(struct riscv_t *rv)
236236
{
237-
/* setup_queue(capacity, event_count) */
238-
queues_capacity = rv_get_reg(rv, rv_reg_a0);
239-
event_count = rv_get_reg(rv, rv_reg_a1);
237+
/* setup_queue(base, capacity, event_count) */
238+
void *base = (void *) (uintptr_t) rv_get_reg(rv, rv_reg_a0);
239+
queues_capacity = rv_get_reg(rv, rv_reg_a1);
240+
event_count = rv_get_reg(rv, rv_reg_a2);
240241

241-
queues_capacity = round_pow2(queues_capacity);
242-
if (queues_capacity == 0) {
243-
rv_set_reg(rv, rv_reg_a0, 0);
244-
return;
245-
}
246-
247-
/* FIXME: Allocate a memory chunk in the emulator's address space so that
248-
* the user can access it */
249-
void *base = malloc(sizeof(event_t) * queues_capacity +
250-
sizeof(submission_t) * queues_capacity);
251242
event_queue.base = base;
252243
submission_queue.base = base + sizeof(event_t) * queues_capacity;
253-
254-
rv_set_reg(
255-
rv, rv_reg_a0,
256-
(uint32_t) (uintptr_t) base); /* eliminate the "cast from pointer to
257-
integer of different size" warning*/
244+
queues_capacity = round_pow2(queues_capacity);
258245
}
259246

260247
void syscall_submit_queue(struct riscv_t *rv)

0 commit comments

Comments
 (0)