refactor: optimize file watching and fix cross-platform reliability issues

Refactor memory allocation patterns:
- Replace realloc-in-loop with count-then-allocate pattern across all platforms
- Eliminate arbitrary buffer sizes (e.g., malloc(8 * ...)) in favor of exact counts
- Reduce allocation overhead by pre-counting items before malloc

Fix Windows file watching:
- Replace unreliable FindFirstChangeNotification with directory handle approach
- Add is_file_ready() to prevent events while compiler is still writing files
- Preserve timestamps when file is locked to retry on next poll
- Fix do-while loop in platform_directory_init_scan (was skipping first file)

Fix Linux inotify event handling:
- Consolidate DELETE+CREATE pairs into single RELOAD event
- Prevents duplicate events when compiler uses temp-file-and-rename pattern

Fix BSD/macOS kqueue implementation:
- Remove realloc loops from update_watches() and watch initialization
- Pre-count files before allocating file descriptor arrays

All platforms now correctly handle:
- Compiler overwrites (temp file operations)
- Manual copy/move operations
- Explicit file deletions

Tested on Linux, Windows 10, and FreeBSD.
This commit is contained in:
2026-01-23 23:39:24 +01:00
parent 93657d71e3
commit f331970ae2
2 changed files with 294 additions and 145 deletions
+1 -4
View File
@@ -1,13 +1,10 @@
include config.mk
ifeq ($(OS),Windows_NT)
# Force the shell to cmd.exe to avoid bash/sh interference
SHELL := cmd.exe
FULL_LIB := $(LIB_NAME).dll
LDFLAGS_PLAT :=
CFLAGS_PLAT :=
# Windows-safe directory creation: check existence, then create
# Use 2>NUL to silence "directory already exists" warnings if any
MKDIR = if not exist $(subst /,\,$(1)) mkdir $(subst /,\,$(1))
RMDIR = if exist $(subst /,\,$(1)) rd /s /q $(subst /,\,$(1))
else
@@ -50,4 +47,4 @@ obj/release/%.o: src/%.c
clean:
@$(call RMDIR,$(OBJ_DIR))
@$(call RMDIR,$(BIN_DIR))
@$(call RMDIR,$(BIN_DIR))