Commit: f10808abee641e1117c6d3ec977e275edf7a24bc
Parent: ac38711fefcc6d7f5804dd5c324a7ba175863644
Author: Randy Palamar
Date: Sat, 3 May 2025 22:52:45 -0600
os: use dynamic arrays for file watches (dirs and files)
Diffstat:
6 files changed, 32 insertions(+), 37 deletions(-)
diff --git a/main_linux.c b/main_linux.c
@@ -33,15 +33,15 @@ dispatch_file_watch_events(OS *os, Arena arena)
while ((rlen = read(fwctx->handle, mem, 4096)) > 0) {
for (u8 *data = mem; data < mem + rlen; data += sizeof(*event) + event->len) {
event = (struct inotify_event *)data;
- for (u32 i = 0; i < fwctx->directory_watch_count; i++) {
- FileWatchDirectory *dir = fwctx->directory_watches + i;
+ for (u32 i = 0; i < fwctx->count; i++) {
+ FileWatchDirectory *dir = fwctx->data + i;
if (event->wd != dir->handle)
continue;
s8 file = c_str_to_s8(event->name);
u64 hash = s8_hash(file);
- for (u32 i = 0; i < dir->file_watch_count; i++) {
- FileWatch *fw = dir->file_watches + i;
+ for (u32 i = 0; i < dir->count; i++) {
+ FileWatch *fw = dir->data + i;
if (fw->hash == hash) {
stream_append_s8s(&path, dir->name, s8("/"), file);
stream_append_byte(&path, 0);
diff --git a/main_w32.c b/main_w32.c
@@ -51,8 +51,8 @@ dispatch_file_watch(OS *os, FileWatchDirectory *fw_dir, u8 *buf, Arena arena)
stream_commit(&path, -1);
u64 hash = s8_hash(file_name);
- for (u32 i = 0; i < fw_dir->file_watch_count; i++) {
- FileWatch *fw = fw_dir->file_watches + i;
+ for (u32 i = 0; i < fw_dir->count; i++) {
+ FileWatch *fw = fw_dir->data + i;
if (fw->hash == hash) {
fw->callback(os, stream_to_s8(&path), fw->user_data, arena);
break;
diff --git a/os_linux.c b/os_linux.c
@@ -234,19 +234,21 @@ static OS_ADD_FILE_WATCH_FN(os_add_file_watch)
FileWatchDirectory *dir = lookup_file_watch_directory(fwctx, hash);
if (!dir) {
ASSERT(path.data[directory.len] == '/');
-
- dir = fwctx->directory_watches + fwctx->directory_watch_count++;
+ dir = da_push(a, fwctx);
dir->hash = hash;
dir->name = push_s8_zero(a, directory);
i32 mask = IN_MOVED_TO|IN_CLOSE_WRITE;
dir->handle = inotify_add_watch(fwctx->handle, (c8 *)dir->name.data, mask);
}
- insert_file_watch(dir, s8_cut_head(path, dir->name.len + 1), user_data, callback);
+ FileWatch *fw = da_push(a, dir);
+ fw->user_data = user_data;
+ fw->callback = callback;
+ fw->hash = s8_hash(s8_cut_head(path, dir->name.len + 1));
}
i32 pthread_setname_np(pthread_t, char *);
-static iptr
+function iptr
os_create_thread(Arena arena, iptr user_context, s8 name, os_thread_entry_point_fn *fn)
{
pthread_t result;
diff --git a/os_win32.c b/os_win32.c
@@ -313,7 +313,7 @@ static OS_ADD_FILE_WATCH_FN(os_add_file_watch)
if (!dir) {
ASSERT(path.data[directory.len] == '\\');
- dir = fwctx->directory_watches + fwctx->directory_watch_count++;
+ dir = da_push(a, fwctx);
dir->hash = hash;
dir->name = push_s8_zero(a, directory);
dir->handle = CreateFileA((c8 *)dir->name.data, GENERIC_READ, FILE_SHARE_READ, 0,
@@ -334,10 +334,13 @@ static OS_ADD_FILE_WATCH_FN(os_add_file_watch)
FILE_NOTIFY_CHANGE_LAST_WRITE, 0, overlapped, 0);
}
- insert_file_watch(dir, s8_cut_head(path, dir->name.len + 1), user_data, callback);
+ FileWatch *fw = da_push(a, dir);
+ fw->user_data = user_data;
+ fw->callback = callback;
+ fw->hash = s8_hash(s8_cut_head(path, dir->name.len + 1));
}
-static iptr
+function iptr
os_create_thread(Arena arena, iptr user_context, s8 name, os_thread_entry_point_fn *fn)
{
iptr result = CreateThread(0, 0, (iptr)fn, user_context, 0, 0);
diff --git a/util.c b/util.c
@@ -82,7 +82,7 @@ arena_capacity_(Arena *a, iz size, iz alignment)
return result;
}
-enum { DA_INITIAL_CAP = 8 };
+enum { DA_INITIAL_CAP = 4 };
#define da_reserve(a, s, n) \
(s)->data = da_reserve_((a), (s)->data, &(s)->capacity, (s)->count + n, \
_Alignof(typeof(*(s)->data)), sizeof(*(s)->data))
@@ -757,33 +757,21 @@ parse_f64(s8 s)
return result;
}
-static FileWatchDirectory *
+function FileWatchDirectory *
lookup_file_watch_directory(FileWatchContext *ctx, u64 hash)
{
FileWatchDirectory *result = 0;
-
- for (u32 i = 0; i < ctx->directory_watch_count; i++) {
- FileWatchDirectory *test = ctx->directory_watches + i;
+ for (u32 i = 0; i < ctx->count; i++) {
+ FileWatchDirectory *test = ctx->data + i;
if (test->hash == hash) {
result = test;
break;
}
}
-
return result;
}
-static void
-insert_file_watch(FileWatchDirectory *dir, s8 name, iptr user_data, file_watch_callback *callback)
-{
- ASSERT(dir->file_watch_count < ARRAY_COUNT(dir->file_watches));
- FileWatch *fw = dir->file_watches + dir->file_watch_count++;
- fw->hash = s8_hash(name);
- fw->user_data = user_data;
- fw->callback = callback;
-}
-
-static void
+function void
fill_kronecker_sub_matrix(i32 *out, i32 out_stride, i32 scale, i32 *b, uv2 b_dim)
{
f32x4 vscale = dup_f32x4(scale);
diff --git a/util.h b/util.h
@@ -230,16 +230,18 @@ typedef struct {
u64 hash;
iptr handle;
s8 name;
- /* TODO(rnp): just push these as a linked list */
- FileWatch file_watches[16];
- u32 file_watch_count;
- Arena buffer;
+
+ FileWatch *data;
+ iz count;
+ iz capacity;
+ Arena buffer;
} FileWatchDirectory;
typedef struct {
- FileWatchDirectory directory_watches[4];
- iptr handle;
- u32 directory_watch_count;
+ FileWatchDirectory *data;
+ iz count;
+ iz capacity;
+ iptr handle;
} FileWatchContext;
#define OS_ALLOC_ARENA_FN(name) Arena name(Arena old, iz capacity)