Include super basic memory arena
This commit is contained in:
parent
b2fe2b09ef
commit
f460860e10
3 changed files with 65 additions and 10 deletions
47
arena.h
Normal file
47
arena.h
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#ifndef ARENA_H
|
||||
#define ARENA_H
|
||||
|
||||
typedef enum {
|
||||
ARENA_ZERO = 1 << 0,
|
||||
ARENA_SOFTFAIL = 1 << 1,
|
||||
} arena_flags_t;
|
||||
|
||||
// More or less taken from https://nullprogram.com/blog/2023/09/27/
|
||||
typedef struct {
|
||||
uint8_t *begin;
|
||||
uint8_t *end;
|
||||
} arena_t;
|
||||
|
||||
arena_t arena_create(ptrdiff_t capacity)
|
||||
{
|
||||
arena_t a = {0};
|
||||
a.begin = malloc(capacity);
|
||||
a.end = a.begin ? a.begin + capacity : 0;
|
||||
return a;
|
||||
}
|
||||
|
||||
|
||||
void *arena_alloc(arena_t *a, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count, arena_flags_t flags)
|
||||
{
|
||||
ptrdiff_t padding = -(uintptr_t)a->begin & (align - 1);
|
||||
ptrdiff_t available = a->end - a->begin - padding;
|
||||
if (available < 0 || count > available/size)
|
||||
{
|
||||
// TODO: Decide what to do on oom
|
||||
if (flags & ARENA_SOFTFAIL)
|
||||
return 0;
|
||||
else
|
||||
abort();
|
||||
}
|
||||
void *p = a->begin + padding;
|
||||
a->begin += padding + count*size;
|
||||
return flags & ARENA_ZERO ? p : memset(p, 0, count*size);
|
||||
}
|
||||
|
||||
#define arena_reserve(arena, type, count, flags) (type *)arena_alloc(arena, sizeof(type), _Alignof(type), count, flags)
|
||||
|
||||
#define KB 1024
|
||||
#define MB 1024*KB
|
||||
#define GB 1024*MB
|
||||
|
||||
#endif
|
||||
2
input.h
2
input.h
|
|
@ -45,7 +45,7 @@ typedef enum {
|
|||
} mouse_keysym_t;
|
||||
|
||||
#define IME_STR_MAX 200 // TODO: check if this limit is sensible
|
||||
#define KEYBOARD_KEY_TOTAL
|
||||
#define KEYBOARD_KEY_TOTAL 256
|
||||
|
||||
typedef struct {
|
||||
uint8_t keyboard_state; // mostly used to check if we are using the IME
|
||||
|
|
|
|||
12
wayland.c
12
wayland.c
|
|
@ -20,6 +20,7 @@
|
|||
#include "zwp-text-input-unstable-v3-protocol.h"
|
||||
// common headers for platform layer and application layer
|
||||
#include "input.h"
|
||||
#include "arena.h"
|
||||
|
||||
struct wl_compositor *compositor = NULL;
|
||||
struct wl_registry *registry = NULL;
|
||||
|
|
@ -48,6 +49,7 @@ struct xkb_keymap *xkb_keymap = NULL;
|
|||
EGLBoolean errcode = 0;
|
||||
bool running = true;
|
||||
input_t user_input = {};
|
||||
arena_t main_arena = {};
|
||||
|
||||
char *egl_s(int code)
|
||||
{
|
||||
|
|
@ -205,7 +207,7 @@ void enter_callback(void *data, struct wl_keyboard* kb, uint serial, struct wl_s
|
|||
// NOTE: wl_array is a dynamic array as specified in wayland-util.h
|
||||
int32_t *pos = arr->data;
|
||||
// wl_keyboard is giving us all the currenly pressed keys anyways
|
||||
for (int i = 0; i < KEYBOARD_KEY_TOTAL)
|
||||
for (int i = 0; i < KEYBOARD_KEY_TOTAL; ++i)
|
||||
user_input.keys[i] = KEY_STATE_UNPRESSED;
|
||||
|
||||
wl_array_for_each(pos, arr) {
|
||||
|
|
@ -351,6 +353,11 @@ void pointer_axis_relative_direction_callback(void *data, struct wl_pointer *poi
|
|||
int main()
|
||||
{
|
||||
fprintf(stdout, "Starting platform layer...\n");
|
||||
main_arena = arena_create(1*GB);
|
||||
if (!main_arena.begin)
|
||||
fprintf(stderr, "Could not reserve memory for main arena!\n");
|
||||
|
||||
|
||||
struct wl_display *display = wl_display_connect(NULL);
|
||||
if (!display)
|
||||
{
|
||||
|
|
@ -478,7 +485,8 @@ int main()
|
|||
if (errcode != EGL_TRUE)
|
||||
fprintf(stderr, "ERR %s: Could not retrieve EGL config number\n", egl_s(errcode));
|
||||
|
||||
EGLConfig *configs = calloc(config_count, sizeof(EGLConfig));
|
||||
//EGLConfig *configs = calloc(config_count, sizeof(EGLConfig));
|
||||
EGLConfig *configs = arena_reserve(&main_arena, EGLConfig, config_count, ARENA_ZERO);
|
||||
|
||||
EGLint attr_list[] = {
|
||||
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue