Commit Graph

124 Commits

Author SHA1 Message Date
anth64 baa75e897f refactor(module): centralize path parsing and add OS-specific separator
- Define STK_PATH_SEP macro to handle Windows and Unix path separators.
- Refactor extract_module_id to use STK_PATH_SEP.
- Simplify stk_module_load by delegating module ID extraction to extract_module_id.
2026-01-29 22:16:48 +01:00
anth64 92e33ff265 refactor(stk): replace if-else chains with switch statements in stk_poll
- Update event processing to use switch statements.
- Clean up redundant goto jumps after memory reallocation.
2026-01-29 21:25:44 +01:00
anth64 64435fd8cd fix(platform): implement atomic hot-reload synchronization
* Resolve STATUS_INVALID_IMAGE_FORMAT on Windows by preventing race conditions during file I/O.
* is_file_ready: Uses GENERIC_WRITE to block if any process (compiler, copy, etc.) is writing to the source. Added GetFileSize check to ensure headers are flushed.
* platform_copy_file: Copies to .tmp and uses MoveFileExA for an atomic swap, hiding the file from the loader until completion.
* Refactor: Unified Win32 and POSIX logic with a single exit point.
2026-01-29 08:00:15 +01:00
anth64 fb80c3a6c5 feat: implement hot-reload polling with dynamic array management
Implement complete hot-reload logic in stk_poll() supporting reload,
load, and unload operations. Module arrays dynamically grow/shrink
with automatic defragmentation to maintain tight memory layout.

- Grow arrays when loads exceed unloads
- Fill holes left by unloads with new modules
- Defragment and trim arrays when unloads exceed loads
- Unload before realloc to minimize peak memory usage
- Replace sprintf with safe C89 build_path() helper
2026-01-28 00:48:01 +01:00
anth64 c65b2eff6d fix: handle realloc to zero capacity
* Call stk_module_free_memory() when new_capacity is 0.
2026-01-28 00:45:39 +01:00
anth64 91ec40d5b1 fix: nullify module slots after unload
* Set all module slot fields (handle, init, shutdown, id) to NULL/empty
after unloading to prevent use-after-free and enable proper hole
detection during array defragmentation.
2026-01-28 00:18:29 +01:00
anth64 2823c8ab59 feat: add dynamic module array reallocation
* Implement stk_module_realloc_memory() to grow or shrink module arrays during hot-reload operations.
* All-or-nothing strategy with fallback to original pointers on partial realloc failure.
2026-01-28 00:04:34 +01:00
anth64 9491b070d2 refactor(WIP): replace event stubs with module identification logic
Replace the placeholder TODO logs in the polling loop with logic to
resolve filesystem events into specific module indices.

- Centralizing State: Refactored is_module_loaded to is_mod_loaded to
  check against the global stk_module_ids array, removing the need to
  pass local buffers and preparing for unified lifetime management.
- Standardizing Identity: Updated extract_module_id to use a
  consistent output buffer.
- Categorized Event Processing: Implemented a two-pass approach in
  stk_poll to count event types (LOAD, UNLOAD, RELOAD) and allocate
  tracking arrays, replacing the previous stubbed switch statement.
- Mapping Events to Indices: The poll loop now resolves filenames
  back to their specific loaded indices via is_mod_loaded to
  identify exactly which mod_id and index require action.
- Improved Flow Control: Introduced a finish_stk_poll label to
  ensure consistent cleanup and return values when no events are
  detected or processing is complete.
2026-01-27 07:54:51 +01:00
anth64 c558032ea0 feat: prevent configuration changes after initialization
* Introduced stk_initialized flag.
* Updated all configuration setters (stk_set_mod_dir, stk_set_tmp_dir_name, stk_set_module_init_fn, and stk_set_module_shutdown_fn) to silently return if the library is already initialized.
* Ensures internal state consistency by locking paths and function entry points once stk_init has been called.
2026-01-25 17:02:44 +01:00
anth64 472cb3b163 feat: make module paths and entry points configurable
* Added functions to set mod dir, temp dir name, and module init/shutdown function names.
* Replaced hardcoded string literals with configurable static buffers in module.c.
* Improved string safety by replacing sprintf with strncpy/strncat.
* Updated stk_init local path buffers to handle maximum combined path lengths.
2026-01-25 16:52:52 +01:00
anth64 ac0125274d set default values for mod and tmp dir, removed arguments from stk init 2026-01-25 16:01:51 +01:00
anth64 f66a3bc00d fix: legacy printf compatibility and add polling logs
- Change %zu to %lu in stk_init to support older msvcrt.dll (Windows 7/XP)
- Update pluralization logic to correctly handle "0 mods" vs "1 mod"
- Add temporary stk_log calls to stk_poll for monitoring module events
2026-01-25 13:36:53 +01:00
anth64 d1e71faa6d fix(platform): harden linux watch check and clean up whitespace 2026-01-24 09:58:52 +01:00
anth64 f331970ae2 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.
2026-01-23 23:39:24 +01:00
anth64 93657d71e3 gmake file should now use native os commands for file management now 2026-01-23 07:37:52 +01:00
anth64 d1972f7893 refactor(platform): Unify watch logic and fix manual snapshot leaks
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.
2026-01-22 00:24:04 +01:00
anth64 b4fbfebc85 build: remove -ldl from Windows target 2026-01-20 22:05:53 +01:00
anth64 c863da08dd fix(platform): address C89 warnings and Windows pointer casting
- 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.
2026-01-20 21:53:33 +01:00
anth64 404bf9503c fix: init file count to 0, just in case... 2026-01-19 23:16:32 +01:00
anth64 42cf581125 fix: use union for ISO C compliant function pointer conversion 2026-01-19 07:57:40 +01:00
anth64 c64f9aa946 fix: I forgot to change it from uint8_t to int... 2026-01-18 22:24:24 +01:00
anth64 3e2762ac15 remove accidental import, thanks clangd 2026-01-18 22:04:05 +01:00
anth64 a3978644af refactor: centralize module identity logic and upgrade lookup
- 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
2026-01-18 22:01:54 +01:00
anth64 a290be5dcc feat: implement .tmp directory isolation for safe hot-reload
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).
2026-01-18 21:26:17 +01:00
anth64 38469a358f Replace dynamic allocations with fixed-size buffers and add module extension filtering
- 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
2026-01-17 19:52:42 +01:00
anth64 e2e49e605c Fix bmake builds: use absolute include paths, output .dylib on macOS 2026-01-17 19:50:38 +01:00
anth64 2820552db8 Add STK_MOD_RELOAD to module event enum 2026-01-17 19:03:22 +01:00
anth64 86f08bec19 build: fix bmake clean by using absolute paths for directory removal 2026-01-11 18:06:22 +01:00
anth64 bf5863fcb1 build: implement platform-agnostic build system
- 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.
2026-01-11 17:55:47 +01:00
anth64 888e6d5f72 Added kqueue directory behavior and huge refactor
- 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.
2026-01-11 15:31:14 +01:00
anth64 503bd21fae Oops, fix merge conflict. 2026-01-11 12:54:46 +01:00
anth64 fe650743b9 'simplify' make file to work across gmake and bmake 2026-01-11 12:51:28 +01:00
anth64 4ae6e533fa Switch gcc to use available c compiler 2026-01-10 16:13:57 +01:00
anth64 0649e287be extract_module_id is now not static 2025-12-20 08:22:41 -05:00
anth64 a7d40929bf Change shutdown return type and hot reload WIP
- stk_shutdown now is a void return type
- detect if the module being loaded/unloaded is an existing module.
2025-11-07 07:55:44 +01:00
anth64 7169dd37b7 hot loading skeleton 2025-11-06 22:34:55 +01:00
anth64 a30eeb7b1c extract_module_id non static -> static 2025-11-06 22:29:15 +01:00
anth64 8d4a506a91 extract_module_id(const char* path) static -> not static 2025-11-06 21:25:47 +01:00
anth64 c95daad754 Simplify Windows event handling
- 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
2025-11-06 20:42:41 +01:00
anth64 13604e3f3e Fix Linux inotify event handling and simplify module events
- Simplify to STK_MOD_LOAD/STK_MOD_UNLOAD event types
- Replace IN_CREATE/IN_MODIFY with IN_CLOSE_WRITE to wait for complete writes
- Update watch flags to IN_CLOSE_WRITE | IN_DELETE | IN_MOVED_TO | IN_MOVED_FROM
2025-11-06 20:06:15 +01:00
anth64 0cf819fc73 stk module id static -> not static 2025-11-06 19:29:20 +01:00
anth64 b5bb6bc707 Fix (*file_list) pointer syntax in Windows code, remove repeated variable, windows code now returns event types 2025-11-05 07:49:57 +01:00
anth64 3b993458d1 Add module ID tracking for hot-reloading
- 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.
2025-11-04 07:39:20 +01:00
anth64 da4dcee490 Enhance file watching to return event types
- `platform_directory_watch_check` now returns event types along side
  file names/counts
2025-11-03 23:16:32 +01:00
anth64 ec2aa09596 add stk file event enum and definition for stk poll 2025-11-03 21:44:33 +01:00
anth64 35cc5afa9d Module discovery working for real this time
- Tested on Linux
- Allocate module memory before loading anything (seg fault fixed)
2025-11-02 19:41:56 +01:00
anth64 e878819b02 remove diff output 2025-11-02 19:28:16 +01:00
anth64 3c7608ef51 create mod directory if it does not exist. 2025-11-02 19:01:08 +01:00
anth64 5c621c8367 Init/shutdown working
- stk_init/shutdown do what they are supposed to.
- buffer sizes moved to headers
2025-11-02 18:40:33 +01:00
anth64 a0c9cdb577 Windows implementation for platform_directory_init_scan 2025-11-02 13:55:09 +01:00