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.
Unlike inotify, Windows and BSD require manual state snapshots to detect
specific file changes. This refactor standardizes that manual handling
to ensure it is resource-safe and easy to follow.
- Fix memory leaks in platform_directory_watch_check where temporary
buffers (new_snaps, file_list) were not reliably freed on 'phantom'
triggers or error paths.
- Unify control flow using a 'goto' cleanup pattern to ensure
deterministic resource deallocation.
- Synchronizes the manual snapshot comparison logic between Windows
and BSD to ensure identical LOAD/RELOAD/UNLOAD event behavior.
- Simplifies the platform_directory_init_scan logic by removing
redundant directory rewinds and nested checks.
- Add WIN32_LEAN_AND_MEAN to optimize Windows header inclusion.
- Align internal API signatures (is_module_loaded) for consistency.
- Include <stdint.h> so Windows does not complain about int types.
- Cast GetProcAddress return through intptr_t to satisfy -Wpedantic.
- Moved work_path for *nix only macros.
- Move extract_module_id and is_valid_module_file to module.c
- Update is_module_loaded to return module index instead of uint8_t
- Fix platform.c event checks to handle index-based return (>= 0)
- Centralize STK_MODULE_EXT definitions in stk.h
Add foundation for cross-platform hot-reload system by isolating
loaded modules from source files using a temporary directory.
Changes:
- Add configurable tmp directory parameter to stk_init()
(defaults to mods/.tmp/ if not specified)
- Copy all modules from mods/ to .tmp/ on initialization
- Load modules exclusively from .tmp/ directory
- Clean up .tmp/ directory on shutdown
- Add cross-platform file operations:
* platform_mkdir() - create directories
* platform_copy_file() - copy files
* platform_remove_file() - delete files
* platform_remove_dir() - delete directory and contents
- Improve BSD kqueue implementation to detect file overwrites
(adds individual file watches with NOTE_WRITE)
This isolates the loaded shared libraries from source files,
preventing segfaults when users overwrite mods using cp/copy
operations. The actual reload logic remains unimplemented
(marked as TODO in stk_poll switch cases).
- Use fixed STK_PATH_MAX and STK_MOD_ID_BUFFER throughout for predictable memory
- Filter by platform-specific extensions (.so/.dll/.dylib) with compile-time length
- Add RELOAD event detection and is_module_loaded() helper
- Maintain feature parity across all platforms
- Split Makefile into gmake.mk (Linux/Windows) and bmake.mk (BSD/macOS)
- Added config.mk for shared variables.
- Added build.sh and build.bat dispatchers.
- Retains old build behavior with dependency tracking.
- Added kqueue directory watching code for other unix like OSes
(FreeBSD, OpenBSD, MacOS, etc)
- Since much of the code for Linux and other *nix OSes was the same,
some refactoring was done to not have duplicate code.
- Use STK_MOD_LOAD/STK_MOD_UNLOAD for Windows events
- Map FILE_ACTION_ADDED/MODIFIED/RENAMED_NEW_NAME to load
- Map FILE_ACTION_REMOVED/RENAMED_OLD_NAME to unload
- Add stk_module_ids array to track module IDs by filename.
- extract_module_id() extracts module ID from it's filename.
- Enables tracking of file changes of already loaded modules.