Skip to content

Commit

Permalink
src: allocate memory from stack instead of mmap
Browse files Browse the repository at this point in the history
Default stack limit on linux is 8M. That is plenty enough for this app
that uses 1K of memory.

$ ulimit -s
8192
  • Loading branch information
e2dk4r committed Oct 24, 2024
1 parent 60f1845 commit e19dd73
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,6 @@ main(void)

// TODO: make maxSupportedControllers configurable
u32 maxSupportedControllers = 4;
struct gamepad *gamepads = alloca(maxSupportedControllers * sizeof(*gamepads));
bzero(gamepads, maxSupportedControllers * sizeof(*gamepads));

/* wayland */
context.wl_display = wl_display_connect(0);
Expand Down Expand Up @@ -312,13 +310,17 @@ main(void)
/* memory */
struct memory_block memory_block = {};
memory_block.total = 1 * KILOBYTES;
memory_block.block = mmap(0, (size_t)memory_block.total, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
memory_block.block = alloca(memory_block.total);
bzero(memory_block.block, memory_block.total);
// mmap(0, (size_t)memory_block.total, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (!memory_block.block) {
fatal("you do not have 1k memory available.\n");
error_code = GAMEPAD_ERROR_MEMORY;
goto wayland_exit;
}

struct gamepad *gamepads = mem_push(&memory_block, maxSupportedControllers * sizeof(*gamepads));

struct memory_chunk *MemoryForDeviceOpenEvents =
mem_push_chunk(&memory_block, sizeof(struct op), maxSupportedControllers);
struct memory_chunk *MemoryForJoystickPollEvents =
Expand Down

0 comments on commit e19dd73

Please sign in to comment.