Compare commits
21 Commits
v1.0.0-pre.2
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 5509c18b17 | |||
| cc39200917 | |||
| 95d7191fdd | |||
| eb4b4c1f80 | |||
| 7b7916911c | |||
| e46ec52c72 | |||
| 09528d60fd | |||
| d1004d8840 | |||
| 6fbd03f863 | |||
| 0cd50a5781 | |||
| 0ebeafd4bb | |||
| 03cce766cd | |||
| 5b252d2b4e | |||
| 08c846d641 | |||
| 6ca46fe79f | |||
| ba3a9dd163 | |||
| a28c4818ab | |||
| 49ecd8fde5 | |||
| 06d04cf92b | |||
| 4250f91969 | |||
| 42a96f2bc0 |
+84
-1
@@ -7,6 +7,79 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [1.0.0-pre.12] - 2026-03-29
|
||||
|
||||
### Fixed
|
||||
- `gmake.mk`: changed static library extension on Windows from `.lib` to `.a` for MinGW compatibility
|
||||
- `module_loader`: use `RTLD_GLOBAL` when loading modules to correctly expose host symbols to dynamic libraries
|
||||
|
||||
## [1.0.0-pre.11] - 2026-03-15
|
||||
|
||||
### Fixed
|
||||
- `gmake.mk`: `PREFIX` now defaults to `/usr` on Linux instead of `/usr/local`
|
||||
|
||||
## [1.0.0-pre.10] - 2026-03-14
|
||||
|
||||
### Fixed
|
||||
- `bmake.mk`: all target and object paths now anchored with `${.CURDIR}` to fix incorrect path nesting when building from outside the project root; substitution delimiters switched to `|` to avoid conflicts with path separators; compile rules now emit `-MT`/`-MF` for correct dependency file targeting; removed stale `.-include` directives for `.d` files; release lib rules now use `${.TARGET}` consistently
|
||||
- `gmake.mk`: removed `AR`/`ARFLAGS_STATIC` variables and the `ifeq`-guarded static lib rules; both debug and release static lib rules now use `ar rcs` directly, as MinGW ships `ar` and `lib.exe` was incorrect
|
||||
|
||||
## [1.0.0-pre.9] - 2026-03-12
|
||||
|
||||
### Changed
|
||||
- `debug` and `release` targets now produce both a shared and static library alongside each other
|
||||
- obj directories split into `shared/` and `static/` subdirs to keep `-fPIC` objects isolated
|
||||
- `install` no longer depends on `release`; guards with an existence check and exits with a clear message if artifacts are missing
|
||||
- `uninstall` removes static lib alongside shared lib
|
||||
- `build.sh`: `install` and `uninstall` now handle privilege escalation automatically via `doas` or `sudo`; skips escalation if already root
|
||||
|
||||
## [1.0.0-pre.8] - 2026-03-08
|
||||
|
||||
### Fixed
|
||||
- `stk_poll()`: stale index corruption when simultaneous load and unload events occurred in the same poll cycle. New modules were previously loaded into pre-compaction slot indices; they now always append to the compacted array via `module_count + successful_appends`, matching the `append_modules` path. The two load loops have been unified into one.
|
||||
- `stk_poll()`: `cascade_indices` was a fixed-size stack array of `STK_PATH_MAX` (256) elements with no bounds check. It is now heap-allocated to `module_count` entries per iteration, eliminating the silent overflow risk.
|
||||
- `stk_pending_retry()`: module array was pre-allocated to `module_count + stk_pending_count` but never shrunk when fewer entries loaded than were pending. Now calls `stk_module_realloc_memory(module_count)` after the retry loop completes.
|
||||
- `stk_collect_dependents()`: missing bounds guard on the `indices` write: `(*count)++` had no check against the buffer capacity before writing. Added `capacity` parameter (passed as `module_count` from the call site) and a guard that skips the write if the capacity is reached, preventing a silent overflow.
|
||||
- `stk_log_modules()`: `%lu` format specifier used with `size_t` is undefined behaviour on platforms where `unsigned long` is narrower than `size_t` (e.g. MSVC 64-bit). Fixed with an explicit `(unsigned long)` cast, preserving C89 compatibility.
|
||||
|
||||
## [1.0.0-pre.7] - 2026-03-07
|
||||
|
||||
### Fixed
|
||||
- `stk_module_load()`: added missing dependency failure logging when a module is deferred to the pending queue.
|
||||
|
||||
## [1.0.0-pre.6] - 2026-03-07
|
||||
|
||||
### Changed
|
||||
- `stk_poll()`: replaced per-module `stk_pending_add()` calls inside the load holes and `append_modules` loops with batch collection followed by a single `stk_pending_add_batch()` call after both loops complete.
|
||||
|
||||
## [1.0.0-pre.5] - 2026-03-07
|
||||
|
||||
### Changed
|
||||
- `stk_init()`: replaced per-module `stk_pending_add()` calls inside the deferred module loop with batch collection followed by a single `stk_pending_add_batch()` call after the loop.
|
||||
|
||||
## [1.0.0-pre.4] - 2026-03-07
|
||||
|
||||
### Changed
|
||||
- `stk_pending_retry()`: replaced per-module `realloc` at `attempt_load` with a single bulk allocation upfront (`module_count + stk_pending_count`) before the retry loop.
|
||||
- `stk_poll()`: replaced per-module `stk_pending_add()` calls inside the unload and cascade loops with batch collection followed by a single `stk_pending_add_batch()` call after each loop.
|
||||
|
||||
## [1.0.0-pre.3] - 2026-03-07
|
||||
|
||||
### Added
|
||||
- `STK_MOD_DEP_LOG_BUFFER` (2048) added to `stk.h`
|
||||
|
||||
### Changed
|
||||
- Dependency failure logging now emits a single line per module listing all unmet deps with their reason: `not found` or `requires <constraint>, have <version>`
|
||||
- `Deferring 'test_mod_dep': unmet deps: test_mod (not found)`
|
||||
- `Unloading 'test_mod_dep': unmet deps: test_mod (not found), renderer (requires ^2.0.0, have 1.3.0)`
|
||||
- Silent defer at `stk_init` and vague `"unmet dependencies"` cascade log in `stk_poll` replaced with `stk_log_dependency_failures()`
|
||||
- Kahn topological sort refactored into generic `stk_kahn_sort()` accepting a `has_dep` callback and an `on_cycle` callback, decoupling it from `stk_modules`. `stk_topo_sort()` is now a thin wrapper. `stk_sort_load_order()` uses the same core via `stk_batch_has_dep()`, inspecting tmp files so simultaneous load events are processed dependency-first without a retry cycle
|
||||
- `stk_module_load()` split into `stk_module_preload()`, `stk_module_activate()`, `stk_module_discard()`, and `stk_validate_dependencies_single()`. Init is not called if deps are unmet
|
||||
- On UNLOAD events in `stk_poll()`, the unload set is expanded to include all transitively dependent modules via `stk_collect_dependents()`, sorted dependents-first via `stk_sort_unload_order()`. Modules unloaded due to expansion are queued to pending
|
||||
- On LOAD events with unmet dependencies, the tmp path is added to the pending queue instead of being dropped
|
||||
- `stk_pending_retry()` now skips already-loaded entries and prunes entries whose file no longer exists
|
||||
- `free_poll` label moved above `stk_pending_retry()` so retry always runs regardless of exit path
|
||||
|
||||
## [1.0.0-pre.2] - 2026-03-06
|
||||
|
||||
### Fixed
|
||||
@@ -145,7 +218,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Dependency management and versioning not yet implemented
|
||||
- API is unstable and subject to change in future releases
|
||||
|
||||
[Unreleased]: https://github.com/anth64/stk/compare/v1.0.0-pre.2...HEAD
|
||||
[Unreleased]: https://github.com/anth64/stk/compare/v1.0.0-pre.12...HEAD
|
||||
[1.0.0-pre.12]: https://github.com/anth64/stk/compare/v1.0.0-pre.11...v1.0.0-pre.12
|
||||
[1.0.0-pre.11]: https://github.com/anth64/stk/compare/v1.0.0-pre.10...v1.0.0-pre.11
|
||||
[1.0.0-pre.10]: https://github.com/anth64/stk/compare/v1.0.0-pre.9...v1.0.0-pre.10
|
||||
[1.0.0-pre.9]: https://github.com/anth64/stk/compare/v1.0.0-pre.8...v1.0.0-pre.9
|
||||
[1.0.0-pre.8]: https://github.com/anth64/stk/compare/v1.0.0-pre.7...v1.0.0-pre.8
|
||||
[1.0.0-pre.7]: https://github.com/anth64/stk/compare/v1.0.0-pre.6...v1.0.0-pre.7
|
||||
[1.0.0-pre.6]: https://github.com/anth64/stk/compare/v1.0.0-pre.5...v1.0.0-pre.6
|
||||
[1.0.0-pre.5]: https://github.com/anth64/stk/compare/v1.0.0-pre.4...v1.0.0-pre.5
|
||||
[1.0.0-pre.4]: https://github.com/anth64/stk/compare/v1.0.0-pre.3...v1.0.0-pre.4
|
||||
[1.0.0-pre.3]: https://github.com/anth64/stk/compare/v1.0.0-pre.2...v1.0.0-pre.3
|
||||
[1.0.0-pre.2]: https://github.com/anth64/stk/compare/v1.0.0-pre.1...v1.0.0-pre.2
|
||||
[1.0.0-pre.1]: https://github.com/anth64/stk/compare/v0.1.3...v1.0.0-pre.1
|
||||
[0.1.3]: https://github.com/anth64/stk/releases/tag/v0.1.3
|
||||
|
||||
@@ -14,7 +14,7 @@ It is designed to run on modern systems running POSIX and Windows using C89.
|
||||
- **Hot-swapping** of modules at runtime
|
||||
- **Cross-platform** (Linux, BSD, Windows, macOS)
|
||||
- **Optional WASM support** for multi-language modules (planned)
|
||||
- **Developer tools**: lightweight metadata, logging/tracing, and dependency management (in progress)
|
||||
- **Developer tools**: lightweight metadata, logging/tracing, and dependency management
|
||||
- **Minimal, portable API**
|
||||
|
||||
---
|
||||
@@ -43,19 +43,19 @@ build.bat debug release
|
||||
|
||||
#### Unix (Linux/BSD/macOS)
|
||||
```bash
|
||||
sudo ./build.sh install
|
||||
./build.sh install
|
||||
```
|
||||
|
||||
Installs to `/usr/local` by default. Use `PREFIX` to customize:
|
||||
Installs to `/usr` on Linux, `/usr/local` on BSD/macOS by default. Use `PREFIX` to customize:
|
||||
```bash
|
||||
sudo ./build.sh PREFIX=$HOME/.local install
|
||||
./build.sh PREFIX=$HOME/.local install
|
||||
```
|
||||
|
||||
#### Windows
|
||||
```
|
||||
build.bat release
|
||||
```
|
||||
* Once finished building, copy the headers from `include/` to `your_project/include/stk/` and `bin/release/stk.dll` to your project's lib directory.
|
||||
* Once finished building, copy the headers from `include/` to `your_project/include/stk/` and copy `bin/release/stk.dll` or `bin/release/stk.lib` to your project's `lib` directory, depending on whether you are linking dynamically or statically.
|
||||
|
||||
---
|
||||
|
||||
@@ -234,7 +234,7 @@ stk_init();
|
||||
|
||||
## Project Status
|
||||
|
||||
**Current Version:** 1.0.0-pre.1
|
||||
**Current Version:** 1.0.0-pre.12
|
||||
|
||||
### What Works
|
||||
- Cross-platform module loading and hot-reloading
|
||||
@@ -244,6 +244,7 @@ stk_init();
|
||||
- Runtime-configurable logging behavior
|
||||
- Optional module metadata (name, version, description)
|
||||
- Dependency declaration, validation, and versioning
|
||||
- Detailed dependency failure logging (missing ids, version mismatches)
|
||||
- Cascade unload when dependencies are removed
|
||||
- Pending queue with automatic retry when deps become available
|
||||
- Topological sort with cycle detection
|
||||
|
||||
@@ -12,44 +12,59 @@ FULL_LIB = lib${LIB_NAME}.dylib
|
||||
FULL_LIB = lib${LIB_NAME}.so
|
||||
.endif
|
||||
|
||||
STATIC_LIB = lib${LIB_NAME}.a
|
||||
|
||||
LDFLAGS_PLAT = -ldl
|
||||
CFLAGS_PLAT = -fPIC
|
||||
CFLAGS_BASE = -Wall -Wpedantic -I${.CURDIR}/${INC_DIR} -std=c89 ${CFLAGS_PLAT}
|
||||
CFLAGS_STATIC =
|
||||
|
||||
.PHONY: all debug release clean test install uninstall
|
||||
|
||||
all: debug
|
||||
|
||||
OBJS_DEBUG = ${SRCS:S/^src\//obj\/debug\//:S/.c$/.o/}
|
||||
OBJS_RELEASE = ${SRCS:S/^src\//obj\/release\//:S/.c$/.o/}
|
||||
OBJS_DEBUG_SHARED = ${SRCS:S|^src/|${.CURDIR}/obj/debug/shared/|:S/.c$/.o/}
|
||||
OBJS_DEBUG_STATIC = ${SRCS:S|^src/|${.CURDIR}/obj/debug/static/|:S/.c$/.o/}
|
||||
OBJS_RELEASE_SHARED = ${SRCS:S|^src/|${.CURDIR}/obj/release/shared/|:S/.c$/.o/}
|
||||
OBJS_RELEASE_STATIC = ${SRCS:S|^src/|${.CURDIR}/obj/release/static/|:S/.c$/.o/}
|
||||
|
||||
debug: ${BIN_DIR}/debug/${FULL_LIB}
|
||||
release: ${BIN_DIR}/release/${FULL_LIB}
|
||||
debug: ${.CURDIR}/${BIN_DIR}/debug/${FULL_LIB} ${.CURDIR}/${BIN_DIR}/debug/${STATIC_LIB}
|
||||
release: ${.CURDIR}/${BIN_DIR}/release/${FULL_LIB} ${.CURDIR}/${BIN_DIR}/release/${STATIC_LIB}
|
||||
|
||||
${BIN_DIR}/debug/${FULL_LIB}: ${OBJS_DEBUG}
|
||||
${.CURDIR}/${BIN_DIR}/debug/${FULL_LIB}: ${OBJS_DEBUG_SHARED}
|
||||
@mkdir -p ${.TARGET:H}
|
||||
${CC} -shared -o ${.TARGET} ${.ALLSRC} ${LDFLAGS_PLAT}
|
||||
|
||||
${BIN_DIR}/release/${FULL_LIB}: ${OBJS_RELEASE}
|
||||
@mkdir -p ${.CURDIR}/${BIN_DIR}/release
|
||||
${CC} -shared -s -o ${.CURDIR}/${BIN_DIR}/release/${FULL_LIB} ${.ALLSRC} ${LDFLAGS_PLAT}
|
||||
${.CURDIR}/${BIN_DIR}/debug/${STATIC_LIB}: ${OBJS_DEBUG_STATIC}
|
||||
@mkdir -p ${.TARGET:H}
|
||||
ar rcs ${.TARGET} ${.ALLSRC}
|
||||
|
||||
${.CURDIR}/${BIN_DIR}/release/${FULL_LIB}: ${OBJS_RELEASE_SHARED}
|
||||
@mkdir -p ${.TARGET:H}
|
||||
${CC} -shared -s -o ${.TARGET} ${.ALLSRC} ${LDFLAGS_PLAT}
|
||||
|
||||
${.CURDIR}/${BIN_DIR}/release/${STATIC_LIB}: ${OBJS_RELEASE_STATIC}
|
||||
@mkdir -p ${.TARGET:H}
|
||||
ar rcs ${.TARGET} ${.ALLSRC}
|
||||
|
||||
.for _src in ${SRCS}
|
||||
_obj_base = ${_src:S/^src\///:S/.c$/.o/}
|
||||
|
||||
obj/debug/${_obj_base}: ${_src}
|
||||
${.CURDIR}/obj/debug/shared/${_src:S|^src/||:S/.c$/.o/}: ${.CURDIR}/${_src}
|
||||
@mkdir -p ${.TARGET:H}
|
||||
${CC} ${CFLAGS_BASE} -g -O0 -MMD -MP -c ${.ALLSRC} -o ${.TARGET}
|
||||
${CC} ${CFLAGS_BASE} -g -O0 -MMD -MP -MT ${.TARGET} -MF ${.TARGET:S/.o$/.d/} -c ${.ALLSRC} -o ${.TARGET}
|
||||
|
||||
obj/release/${_obj_base}: ${_src}
|
||||
${.CURDIR}/obj/debug/static/${_src:S|^src/||:S/.c$/.o/}: ${.CURDIR}/${_src}
|
||||
@mkdir -p ${.TARGET:H}
|
||||
${CC} ${CFLAGS_BASE} -O2 -MMD -MP -c ${.ALLSRC} -o ${.TARGET}
|
||||
${CC} ${CFLAGS_BASE} ${CFLAGS_STATIC} -g -O0 -MMD -MP -MT ${.TARGET} -MF ${.TARGET:S/.o$/.d/} -c ${.ALLSRC} -o ${.TARGET}
|
||||
|
||||
${.CURDIR}/obj/release/shared/${_src:S|^src/||:S/.c$/.o/}: ${.CURDIR}/${_src}
|
||||
@mkdir -p ${.TARGET:H}
|
||||
${CC} ${CFLAGS_BASE} -O2 -MMD -MP -MT ${.TARGET} -MF ${.TARGET:S/.o$/.d/} -c ${.ALLSRC} -o ${.TARGET}
|
||||
|
||||
${.CURDIR}/obj/release/static/${_src:S|^src/||:S/.c$/.o/}: ${.CURDIR}/${_src}
|
||||
@mkdir -p ${.TARGET:H}
|
||||
${CC} ${CFLAGS_BASE} ${CFLAGS_STATIC} -O2 -MMD -MP -MT ${.TARGET} -MF ${.TARGET:S/.o$/.d/} -c ${.ALLSRC} -o ${.TARGET}
|
||||
.endfor
|
||||
|
||||
.-include "obj/debug/*.d"
|
||||
.-include "obj/release/*.d"
|
||||
|
||||
clean:
|
||||
rm -rf ${.CURDIR}/${OBJ_DIR} ${.CURDIR}/${BIN_DIR}
|
||||
|
||||
@@ -57,13 +72,16 @@ test: debug
|
||||
@echo "=== Building and running stk tests ==="
|
||||
cd ${.CURDIR}/test && ${MAKE} -f bmake.mk
|
||||
|
||||
install: release
|
||||
install:
|
||||
@test -f ${.CURDIR}/${BIN_DIR}/release/${FULL_LIB} || { echo "Run 'make -f bmake.mk release' before installing."; exit 1; }
|
||||
install -d ${LIBDIR} ${INCDIR}/stk
|
||||
install -m 755 ${.CURDIR}/${BIN_DIR}/release/${FULL_LIB} ${LIBDIR}/
|
||||
install -m 644 ${.CURDIR}/${BIN_DIR}/release/${STATIC_LIB} ${LIBDIR}/
|
||||
install -m 644 ${.CURDIR}/${INC_DIR}/stk.h ${INCDIR}/stk/
|
||||
install -m 644 ${.CURDIR}/${INC_DIR}/stk_version.h ${INCDIR}/stk/
|
||||
install -m 644 ${.CURDIR}/${INC_DIR}/stk_log.h ${INCDIR}/stk/
|
||||
|
||||
uninstall:
|
||||
rm -f ${LIBDIR}/${FULL_LIB}
|
||||
rm -f ${LIBDIR}/${STATIC_LIB}
|
||||
rm -rf ${INCDIR}/stk
|
||||
|
||||
@@ -8,4 +8,32 @@ else
|
||||
MK_FILE="bmake.mk"
|
||||
fi
|
||||
|
||||
make -f "$MK_FILE" "$@"
|
||||
HAS_INSTALL=0
|
||||
HAS_UNINSTALL=0
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
install) HAS_INSTALL=1 ;;
|
||||
uninstall) HAS_UNINSTALL=1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ "$HAS_INSTALL" = "1" ] || [ "$HAS_UNINSTALL" = "1" ]; then
|
||||
if [ "$HAS_INSTALL" = "1" ]; then
|
||||
make -f "$MK_FILE" release
|
||||
fi
|
||||
|
||||
if [ "$(id -u)" = "0" ]; then
|
||||
PRIV=""
|
||||
elif command -v doas > /dev/null 2>&1; then
|
||||
PRIV="doas"
|
||||
elif command -v sudo > /dev/null 2>&1; then
|
||||
PRIV="sudo"
|
||||
else
|
||||
echo "error: install requires root. neither doas nor sudo found." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
$PRIV make -f "$MK_FILE" "$@"
|
||||
else
|
||||
make -f "$MK_FILE" "$@"
|
||||
fi
|
||||
|
||||
@@ -4,6 +4,7 @@ OBJ_DIR = obj
|
||||
BIN_DIR = bin
|
||||
|
||||
LIB_NAME = stk
|
||||
STATIC_LIB = lib$(LIB_NAME).a
|
||||
|
||||
SRCS = src/module.c \
|
||||
src/platform.c \
|
||||
|
||||
@@ -3,14 +3,18 @@ include config.mk
|
||||
ifeq ($(OS),Windows_NT)
|
||||
SHELL := cmd.exe
|
||||
FULL_LIB := $(LIB_NAME).dll
|
||||
STATIC_LIB := lib$(LIB_NAME).a
|
||||
LDFLAGS_PLAT :=
|
||||
CFLAGS_PLAT :=
|
||||
CFLAGS_STATIC :=
|
||||
MKDIR = if not exist $(subst /,\,$(1)) mkdir $(subst /,\,$(1))
|
||||
RMDIR = if exist $(subst /,\,$(1)) rd /s /q $(subst /,\,$(1))
|
||||
else
|
||||
FULL_LIB := lib$(LIB_NAME).so
|
||||
STATIC_LIB := lib$(LIB_NAME).a
|
||||
LDFLAGS_PLAT := -ldl
|
||||
CFLAGS_PLAT := -fPIC
|
||||
CFLAGS_STATIC :=
|
||||
MKDIR = mkdir -p $(1)
|
||||
RMDIR = rm -rf $(1)
|
||||
endif
|
||||
@@ -18,7 +22,7 @@ endif
|
||||
RELEASE_LDFLAGS := -s
|
||||
CFLAGS_BASE := -Wall -Wpedantic -I$(INC_DIR) -std=c89 $(CFLAGS_PLAT)
|
||||
|
||||
PREFIX ?= /usr/local
|
||||
PREFIX ?= /usr
|
||||
LIBDIR ?= $(PREFIX)/lib
|
||||
INCDIR ?= $(PREFIX)/include
|
||||
|
||||
@@ -26,29 +30,47 @@ INCDIR ?= $(PREFIX)/include
|
||||
|
||||
all: debug
|
||||
|
||||
debug: $(BIN_DIR)/debug/$(FULL_LIB)
|
||||
release: $(BIN_DIR)/release/$(FULL_LIB)
|
||||
debug: $(BIN_DIR)/debug/$(FULL_LIB) $(BIN_DIR)/debug/$(STATIC_LIB)
|
||||
release: $(BIN_DIR)/release/$(FULL_LIB) $(BIN_DIR)/release/$(STATIC_LIB)
|
||||
|
||||
# Debug Rules
|
||||
$(BIN_DIR)/debug/$(FULL_LIB): $(SRCS:src/%.c=obj/debug/%.o)
|
||||
$(BIN_DIR)/debug/$(FULL_LIB): $(SRCS:src/%.c=obj/debug/shared/%.o)
|
||||
@$(call MKDIR,$(@D))
|
||||
$(CC) -shared -o $@ $^ $(LDFLAGS_PLAT)
|
||||
|
||||
obj/debug/%.o: src/%.c
|
||||
$(BIN_DIR)/debug/$(STATIC_LIB): $(SRCS:src/%.c=obj/debug/static/%.o)
|
||||
@$(call MKDIR,$(@D))
|
||||
ar rcs $@ $^
|
||||
|
||||
obj/debug/shared/%.o: src/%.c
|
||||
@$(call MKDIR,$(@D))
|
||||
$(CC) $(CFLAGS_BASE) -g -O0 -MMD -MP -c $< -o $@
|
||||
|
||||
obj/debug/static/%.o: src/%.c
|
||||
@$(call MKDIR,$(@D))
|
||||
$(CC) $(CFLAGS_BASE) $(CFLAGS_STATIC) -g -O0 -MMD -MP -c $< -o $@
|
||||
|
||||
# Release Rules
|
||||
$(BIN_DIR)/release/$(FULL_LIB): $(SRCS:src/%.c=obj/release/%.o)
|
||||
$(BIN_DIR)/release/$(FULL_LIB): $(SRCS:src/%.c=obj/release/shared/%.o)
|
||||
@$(call MKDIR,$(@D))
|
||||
$(CC) -shared $(RELEASE_LDFLAGS) -o $@ $^ $(LDFLAGS_PLAT)
|
||||
|
||||
obj/release/%.o: src/%.c
|
||||
$(BIN_DIR)/release/$(STATIC_LIB): $(SRCS:src/%.c=obj/release/static/%.o)
|
||||
@$(call MKDIR,$(@D))
|
||||
ar rcs $@ $^
|
||||
|
||||
obj/release/shared/%.o: src/%.c
|
||||
@$(call MKDIR,$(@D))
|
||||
$(CC) $(CFLAGS_BASE) -O2 -MMD -MP -c $< -o $@
|
||||
|
||||
-include $(wildcard obj/debug/*.d)
|
||||
-include $(wildcard obj/release/*.d)
|
||||
obj/release/static/%.o: src/%.c
|
||||
@$(call MKDIR,$(@D))
|
||||
$(CC) $(CFLAGS_BASE) $(CFLAGS_STATIC) -O2 -MMD -MP -c $< -o $@
|
||||
|
||||
-include $(wildcard obj/debug/shared/*.d)
|
||||
-include $(wildcard obj/debug/static/*.d)
|
||||
-include $(wildcard obj/release/shared/*.d)
|
||||
-include $(wildcard obj/release/static/*.d)
|
||||
|
||||
clean:
|
||||
@$(call RMDIR,$(OBJ_DIR))
|
||||
@@ -58,23 +80,26 @@ test: debug
|
||||
@echo "=== Building and running stk tests ==="
|
||||
@$(MAKE) -C test -f gmake.mk
|
||||
|
||||
# Installation (Unix only)
|
||||
ifneq ($(OS),Windows_NT)
|
||||
install: release
|
||||
install:
|
||||
@test -f $(BIN_DIR)/release/$(FULL_LIB) || { echo "Run 'make -f gmake.mk release' before installing."; exit 1; }
|
||||
install -d $(LIBDIR) $(INCDIR)/stk
|
||||
install -m 755 $(BIN_DIR)/release/$(FULL_LIB) $(LIBDIR)/
|
||||
install -m 644 $(BIN_DIR)/release/$(STATIC_LIB) $(LIBDIR)/
|
||||
install -m 644 $(INC_DIR)/stk.h $(INCDIR)/stk/
|
||||
install -m 644 $(INC_DIR)/stk_version.h $(INCDIR)/stk/
|
||||
install -m 644 $(INC_DIR)/stk_log.h $(INCDIR)/stk/
|
||||
|
||||
uninstall:
|
||||
rm -f $(LIBDIR)/$(FULL_LIB)
|
||||
rm -f $(LIBDIR)/$(STATIC_LIB)
|
||||
rm -rf $(INCDIR)/stk
|
||||
else
|
||||
install:
|
||||
@echo "make install is not supported on Windows."
|
||||
@echo "Copy include/ directory contents to your_project/include/stk/"
|
||||
@echo "Copy bin/release/stk.dll to your project's lib directory."
|
||||
@echo "Copy bin/release/stk.lib to your project's lib directory."
|
||||
|
||||
uninstall:
|
||||
@echo "make uninstall is not supported on Windows."
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
/* Buffers */
|
||||
#define STK_LOG_PREFIX_BUFFER 64
|
||||
#define STK_MOD_DEP_OPERATOR_BUFFER 3
|
||||
#define STK_MOD_DEP_LOG_BUFFER 2048
|
||||
#define STK_MOD_DESC_BUFFER 256
|
||||
#define STK_MOD_DIR_BUFFER 256
|
||||
#define STK_MOD_ID_BUFFER 64
|
||||
|
||||
+500
-62
@@ -206,12 +206,15 @@ unsigned char stk_validate_dependencies(size_t count)
|
||||
return result;
|
||||
}
|
||||
|
||||
unsigned char stk_topo_sort(size_t count, size_t *order)
|
||||
typedef int (*stk_dep_query_fn)(size_t i, size_t j, void *ctx);
|
||||
|
||||
static unsigned char stk_kahn_sort(size_t count, size_t *order,
|
||||
stk_dep_query_fn has_dep, void *ctx,
|
||||
void (*on_cycle)(size_t i))
|
||||
{
|
||||
size_t *in_degree = NULL;
|
||||
size_t *queue = NULL;
|
||||
size_t head, tail, sorted, i, d;
|
||||
int dep_index;
|
||||
size_t head, tail, sorted, i, j;
|
||||
unsigned char result = STK_MOD_INIT_SUCCESS;
|
||||
|
||||
if (count == 0)
|
||||
@@ -229,11 +232,9 @@ unsigned char stk_topo_sort(size_t count, size_t *order)
|
||||
in_degree[i] = 0;
|
||||
|
||||
for (i = 0; i < count; i++)
|
||||
for (d = 0; d < stk_modules[i].dep_count; d++) {
|
||||
dep_index = is_mod_loaded(stk_modules[i].deps[d].id);
|
||||
if (dep_index >= 0)
|
||||
for (j = 0; j < count; j++)
|
||||
if (i != j && has_dep(i, j, ctx))
|
||||
in_degree[i]++;
|
||||
}
|
||||
|
||||
head = tail = sorted = 0;
|
||||
|
||||
@@ -246,48 +247,59 @@ unsigned char stk_topo_sort(size_t count, size_t *order)
|
||||
order[sorted++] = mod;
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
for (d = 0; d < stk_modules[i].dep_count; d++) {
|
||||
dep_index =
|
||||
is_mod_loaded(stk_modules[i].deps[d].id);
|
||||
if (dep_index != (int)mod)
|
||||
if (i == mod || !has_dep(i, mod, ctx))
|
||||
continue;
|
||||
if (--in_degree[i] == 0)
|
||||
queue[tail++] = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sorted != count) {
|
||||
size_t j;
|
||||
size_t k;
|
||||
int in_order;
|
||||
for (i = 0; i < count; i++) {
|
||||
in_order = 0;
|
||||
for (j = 0; j < sorted; j++) {
|
||||
if (order[j] == i) {
|
||||
for (k = 0; k < sorted; k++) {
|
||||
if (order[k] == i) {
|
||||
in_order = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!in_order)
|
||||
stk_log(STK_LOG_ERROR,
|
||||
"Circular dependency detected with %s",
|
||||
stk_modules[i].id);
|
||||
if (!in_order && on_cycle)
|
||||
on_cycle(i);
|
||||
}
|
||||
|
||||
result = STK_MOD_DEP_CIRCULAR_ERROR;
|
||||
}
|
||||
done:
|
||||
if (in_degree)
|
||||
free(in_degree);
|
||||
if (queue)
|
||||
free(queue);
|
||||
|
||||
done:
|
||||
free(in_degree);
|
||||
free(queue);
|
||||
return result;
|
||||
}
|
||||
|
||||
unsigned char stk_module_load(const char *path, int index)
|
||||
static int stk_loaded_has_dep(size_t i, size_t j, void *ctx)
|
||||
{
|
||||
size_t d;
|
||||
(void)ctx;
|
||||
for (d = 0; d < stk_modules[i].dep_count; d++)
|
||||
if (is_mod_loaded(stk_modules[i].deps[d].id) == (int)j)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void stk_log_cycle(size_t i)
|
||||
{
|
||||
stk_log(STK_LOG_ERROR, "Circular dependency detected with %s",
|
||||
stk_modules[i].id);
|
||||
}
|
||||
|
||||
unsigned char stk_topo_sort(size_t count, size_t *order)
|
||||
{
|
||||
return stk_kahn_sort(count, order, stk_loaded_has_dep, NULL,
|
||||
stk_log_cycle);
|
||||
}
|
||||
|
||||
unsigned char stk_module_preload(const char *path, int index)
|
||||
{
|
||||
void *handle;
|
||||
char module_id[STK_MOD_ID_BUFFER];
|
||||
@@ -323,37 +335,6 @@ unsigned char stk_module_load(const char *path, int index)
|
||||
|
||||
extract_module_id(path, module_id);
|
||||
|
||||
u.obj = platform_get_symbol(handle, stk_mod_deps_sym);
|
||||
if (u.obj) {
|
||||
const stk_dep_t *early_deps = (const stk_dep_t *)u.obj;
|
||||
size_t early_dep_count = 0;
|
||||
size_t di;
|
||||
int found;
|
||||
|
||||
while (early_deps[early_dep_count].id[0] != '\0')
|
||||
early_dep_count++;
|
||||
|
||||
for (di = 0; di < early_dep_count; di++) {
|
||||
found = is_mod_loaded(early_deps[di].id);
|
||||
if (found < 0) {
|
||||
platform_unload_library(handle);
|
||||
return STK_MOD_DEP_NOT_FOUND_ERROR;
|
||||
}
|
||||
if (early_deps[di].version[0] &&
|
||||
!stk_validate_constraint(
|
||||
early_deps[di].version,
|
||||
stk_modules[found].version)) {
|
||||
platform_unload_library(handle);
|
||||
return STK_MOD_DEP_VERSION_MISMATCH_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (stk_modules[index].init() != STK_MOD_INIT_SUCCESS) {
|
||||
platform_unload_library(handle);
|
||||
return STK_MOD_INIT_FAILURE;
|
||||
}
|
||||
|
||||
stk_modules[index].handle = handle;
|
||||
|
||||
len = strlen(module_id);
|
||||
@@ -444,6 +425,152 @@ skip_deps:
|
||||
return STK_MOD_INIT_SUCCESS;
|
||||
}
|
||||
|
||||
void stk_module_discard(size_t index)
|
||||
{
|
||||
platform_unload_library(stk_modules[index].handle);
|
||||
stk_modules[index].handle = NULL;
|
||||
stk_modules[index].init = NULL;
|
||||
stk_modules[index].shutdown = NULL;
|
||||
stk_modules[index].id[0] = '\0';
|
||||
stk_modules[index].name[0] = '\0';
|
||||
stk_modules[index].version[0] = '\0';
|
||||
stk_modules[index].desc[0] = '\0';
|
||||
if (stk_modules[index].deps) {
|
||||
free(stk_modules[index].deps);
|
||||
stk_modules[index].deps = NULL;
|
||||
}
|
||||
stk_modules[index].dep_count = 0;
|
||||
}
|
||||
|
||||
unsigned char stk_module_activate(size_t index)
|
||||
{
|
||||
if (stk_modules[index].init() != STK_MOD_INIT_SUCCESS) {
|
||||
stk_module_discard(index);
|
||||
return STK_MOD_INIT_FAILURE;
|
||||
}
|
||||
return STK_MOD_INIT_SUCCESS;
|
||||
}
|
||||
|
||||
unsigned char stk_validate_dependencies_single(size_t index)
|
||||
{
|
||||
size_t d;
|
||||
int found;
|
||||
|
||||
if (stk_modules[index].dep_count == 0)
|
||||
return STK_MOD_INIT_SUCCESS;
|
||||
|
||||
for (d = 0; d < stk_modules[index].dep_count; d++) {
|
||||
found = is_mod_loaded(stk_modules[index].deps[d].id);
|
||||
if (found < 0)
|
||||
return STK_MOD_DEP_NOT_FOUND_ERROR;
|
||||
if (stk_modules[index].deps[d].version[0] &&
|
||||
!stk_validate_constraint(stk_modules[index].deps[d].version,
|
||||
stk_modules[found].version))
|
||||
return STK_MOD_DEP_VERSION_MISMATCH_ERROR;
|
||||
}
|
||||
|
||||
return STK_MOD_INIT_SUCCESS;
|
||||
}
|
||||
|
||||
void stk_log_dependency_failures(size_t index, const char *action)
|
||||
{
|
||||
char buf[STK_MOD_DEP_LOG_BUFFER];
|
||||
size_t d, pos, len;
|
||||
int found;
|
||||
int first = 1;
|
||||
|
||||
if (stk_modules[index].dep_count == 0)
|
||||
return;
|
||||
|
||||
pos = 0;
|
||||
for (d = 0; d < stk_modules[index].dep_count; d++) {
|
||||
found = is_mod_loaded(stk_modules[index].deps[d].id);
|
||||
|
||||
if (found >= 0 &&
|
||||
(!stk_modules[index].deps[d].version[0] ||
|
||||
stk_validate_constraint(stk_modules[index].deps[d].version,
|
||||
stk_modules[found].version)))
|
||||
continue;
|
||||
|
||||
if (!first && pos < sizeof(buf) - 2) {
|
||||
buf[pos++] = ',';
|
||||
buf[pos++] = ' ';
|
||||
}
|
||||
first = 0;
|
||||
|
||||
if (found < 0) {
|
||||
len = strlen(stk_modules[index].deps[d].id);
|
||||
if (pos + len + 12 < sizeof(buf)) {
|
||||
memcpy(buf + pos, stk_modules[index].deps[d].id,
|
||||
len);
|
||||
pos += len;
|
||||
memcpy(buf + pos, " (not found)", 12);
|
||||
pos += 12;
|
||||
}
|
||||
} else {
|
||||
len = strlen(stk_modules[index].deps[d].id);
|
||||
if (pos + len < sizeof(buf)) {
|
||||
memcpy(buf + pos, stk_modules[index].deps[d].id,
|
||||
len);
|
||||
pos += len;
|
||||
}
|
||||
if (pos < sizeof(buf) - 1) {
|
||||
buf[pos++] = ' ';
|
||||
}
|
||||
buf[pos++] = '(';
|
||||
len = strlen("requires ");
|
||||
if (pos + len < sizeof(buf)) {
|
||||
memcpy(buf + pos, "requires ", len);
|
||||
pos += len;
|
||||
}
|
||||
len = strlen(stk_modules[index].deps[d].version);
|
||||
if (pos + len < sizeof(buf)) {
|
||||
memcpy(buf + pos,
|
||||
stk_modules[index].deps[d].version, len);
|
||||
pos += len;
|
||||
}
|
||||
len = strlen(", have ");
|
||||
if (pos + len < sizeof(buf)) {
|
||||
memcpy(buf + pos, ", have ", len);
|
||||
pos += len;
|
||||
}
|
||||
len = strlen(stk_modules[found].version);
|
||||
if (pos + len < sizeof(buf)) {
|
||||
memcpy(buf + pos, stk_modules[found].version,
|
||||
len);
|
||||
pos += len;
|
||||
}
|
||||
if (pos < sizeof(buf) - 1)
|
||||
buf[pos++] = ')';
|
||||
}
|
||||
}
|
||||
|
||||
if (first)
|
||||
return;
|
||||
|
||||
buf[pos] = '\0';
|
||||
stk_log(STK_LOG_WARN, "%s '%s': unmet deps: %s", action,
|
||||
stk_modules[index].id, buf);
|
||||
}
|
||||
|
||||
unsigned char stk_module_load(const char *path, int index)
|
||||
{
|
||||
unsigned char result;
|
||||
|
||||
result = stk_module_preload(path, index);
|
||||
if (result != STK_MOD_INIT_SUCCESS)
|
||||
return result;
|
||||
|
||||
result = stk_validate_dependencies_single(index);
|
||||
if (result != STK_MOD_INIT_SUCCESS) {
|
||||
stk_log_dependency_failures(index, "Deferring");
|
||||
stk_module_discard(index);
|
||||
return result;
|
||||
}
|
||||
|
||||
return stk_module_activate(index);
|
||||
}
|
||||
|
||||
unsigned char stk_module_load_init(const char *path, int index)
|
||||
{
|
||||
int result;
|
||||
@@ -537,20 +664,229 @@ unsigned char stk_module_realloc_memory(size_t new_capacity)
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
int *file_indices;
|
||||
char (*file_names)[STK_PATH_MAX];
|
||||
const char *tmp_dir;
|
||||
} stk_batch_dep_ctx_t;
|
||||
|
||||
static int stk_batch_has_dep(size_t i, size_t j, void *ctx)
|
||||
{
|
||||
stk_batch_dep_ctx_t *c = (stk_batch_dep_ctx_t *)ctx;
|
||||
char id_j[STK_MOD_ID_BUFFER];
|
||||
char tmp_i[STK_PATH_MAX_OS];
|
||||
void *h;
|
||||
const stk_dep_t *deps;
|
||||
size_t d, dep_count;
|
||||
union {
|
||||
void *obj;
|
||||
} u;
|
||||
int result = 0;
|
||||
|
||||
extract_module_id(c->file_names[c->file_indices[j]], id_j);
|
||||
|
||||
tmp_i[0] = '\0';
|
||||
strncat(tmp_i, c->tmp_dir, STK_PATH_MAX_OS - 1);
|
||||
strncat(tmp_i, STK_PATH_SEP_STR, STK_PATH_MAX_OS - strlen(tmp_i) - 1);
|
||||
strncat(tmp_i, c->file_names[c->file_indices[i]],
|
||||
STK_PATH_MAX_OS - strlen(tmp_i) - 1);
|
||||
|
||||
h = platform_load_library(tmp_i);
|
||||
if (!h)
|
||||
return 0;
|
||||
|
||||
u.obj = platform_get_symbol(h, stk_mod_deps_sym);
|
||||
if (u.obj) {
|
||||
deps = (const stk_dep_t *)u.obj;
|
||||
dep_count = 0;
|
||||
while (deps[dep_count].id[0] != '\0')
|
||||
dep_count++;
|
||||
for (d = 0; d < dep_count; d++) {
|
||||
if (strncmp(deps[d].id, id_j, STK_MOD_ID_BUFFER) == 0) {
|
||||
result = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
platform_unload_library(h);
|
||||
return result;
|
||||
}
|
||||
|
||||
void stk_sort_load_order(int *file_indices, size_t n,
|
||||
char (*file_names)[STK_PATH_MAX], const char *tmp_dir)
|
||||
{
|
||||
size_t *order = NULL;
|
||||
int *result = NULL;
|
||||
stk_batch_dep_ctx_t ctx;
|
||||
size_t i;
|
||||
|
||||
if (n <= 1)
|
||||
return;
|
||||
|
||||
order = malloc(n * sizeof(size_t));
|
||||
result = malloc(n * sizeof(int));
|
||||
if (!order || !result)
|
||||
goto cleanup;
|
||||
|
||||
ctx.file_indices = file_indices;
|
||||
ctx.file_names = file_names;
|
||||
ctx.tmp_dir = tmp_dir;
|
||||
|
||||
if (stk_kahn_sort(n, order, stk_batch_has_dep, &ctx, NULL) !=
|
||||
STK_MOD_INIT_SUCCESS)
|
||||
goto cleanup;
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
result[i] = file_indices[order[i]];
|
||||
for (i = 0; i < n; i++)
|
||||
file_indices[i] = result[i];
|
||||
|
||||
cleanup:
|
||||
free(order);
|
||||
free(result);
|
||||
}
|
||||
|
||||
void stk_collect_dependents(size_t *indices, size_t *count, size_t capacity)
|
||||
{
|
||||
size_t i, d;
|
||||
int in_set, changed;
|
||||
|
||||
do {
|
||||
changed = 0;
|
||||
for (i = 0; i < module_count; i++) {
|
||||
in_set = 0;
|
||||
{
|
||||
size_t k;
|
||||
for (k = 0; k < *count; k++) {
|
||||
if (indices[k] == i) {
|
||||
in_set = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (in_set)
|
||||
continue;
|
||||
|
||||
for (d = 0; d < stk_modules[i].dep_count; d++) {
|
||||
int dep_index =
|
||||
is_mod_loaded(stk_modules[i].deps[d].id);
|
||||
if (dep_index < 0)
|
||||
continue;
|
||||
{
|
||||
size_t k;
|
||||
for (k = 0; k < *count; k++) {
|
||||
if (indices[k] ==
|
||||
(size_t)dep_index) {
|
||||
if (*count >= capacity)
|
||||
goto next_module;
|
||||
indices[(*count)++] = i;
|
||||
changed = 1;
|
||||
goto next_module;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
next_module:;
|
||||
}
|
||||
} while (changed);
|
||||
}
|
||||
|
||||
void stk_sort_unload_order(size_t *indices, size_t n)
|
||||
{
|
||||
size_t *topo = NULL;
|
||||
size_t *result = NULL;
|
||||
size_t i, j, k;
|
||||
int in_set;
|
||||
|
||||
if (n <= 1)
|
||||
return;
|
||||
|
||||
topo = malloc(module_count * sizeof(size_t));
|
||||
result = malloc(n * sizeof(size_t));
|
||||
|
||||
if (!topo || !result)
|
||||
goto fallback;
|
||||
|
||||
if (stk_topo_sort(module_count, topo) != STK_MOD_INIT_SUCCESS)
|
||||
goto fallback;
|
||||
|
||||
k = 0;
|
||||
for (i = module_count; i > 0; --i) {
|
||||
size_t mod = topo[i - 1];
|
||||
in_set = 0;
|
||||
for (j = 0; j < n; j++) {
|
||||
if (indices[j] == mod) {
|
||||
in_set = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (in_set)
|
||||
result[k++] = mod;
|
||||
}
|
||||
|
||||
if (k == n) {
|
||||
for (i = 0; i < n; i++)
|
||||
indices[i] = result[i];
|
||||
}
|
||||
|
||||
free(topo);
|
||||
free(result);
|
||||
return;
|
||||
|
||||
fallback:
|
||||
free(topo);
|
||||
free(result);
|
||||
for (i = 0; i < n / 2; i++) {
|
||||
size_t tmp = indices[i];
|
||||
indices[i] = indices[n - 1 - i];
|
||||
indices[n - 1 - i] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
void stk_module_unload_all(void)
|
||||
{
|
||||
size_t i;
|
||||
size_t *order = NULL;
|
||||
|
||||
if (module_count == 0)
|
||||
goto free_mem;
|
||||
|
||||
order = malloc(module_count * sizeof(size_t));
|
||||
if (order) {
|
||||
for (i = 0; i < module_count; i++)
|
||||
order[i] = i;
|
||||
stk_sort_unload_order(order, module_count);
|
||||
for (i = 0; i < module_count; i++)
|
||||
stk_module_unload(order[i]);
|
||||
free(order);
|
||||
} else {
|
||||
for (i = module_count; i > 0; --i)
|
||||
stk_module_unload(i - 1);
|
||||
}
|
||||
|
||||
free_mem:
|
||||
stk_module_free_memory();
|
||||
}
|
||||
|
||||
void stk_pending_add(const char *path)
|
||||
{
|
||||
char (*new_pending)[STK_PATH_MAX_OS];
|
||||
char incoming_id[STK_MOD_ID_BUFFER];
|
||||
char existing_id[STK_MOD_ID_BUFFER];
|
||||
size_t i;
|
||||
|
||||
extract_module_id(path, incoming_id);
|
||||
|
||||
for (i = 0; i < stk_pending_count; i++) {
|
||||
extract_module_id(stk_pending[i], existing_id);
|
||||
if (strncmp(existing_id, incoming_id, STK_MOD_ID_BUFFER) == 0) {
|
||||
strncpy(stk_pending[i], path, STK_PATH_MAX_OS - 1);
|
||||
stk_pending[i][STK_PATH_MAX_OS - 1] = '\0';
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
new_pending = malloc((stk_pending_count + 1) * sizeof(*stk_pending));
|
||||
if (!new_pending)
|
||||
return;
|
||||
@@ -566,6 +902,74 @@ void stk_pending_add(const char *path)
|
||||
stk_pending_count++;
|
||||
}
|
||||
|
||||
void stk_pending_add_batch(const char (*paths)[STK_PATH_MAX_OS], size_t count)
|
||||
{
|
||||
char (*new_pending)[STK_PATH_MAX_OS];
|
||||
char incoming_id[STK_MOD_ID_BUFFER];
|
||||
char existing_id[STK_MOD_ID_BUFFER];
|
||||
size_t i, j, new_count;
|
||||
|
||||
if (!paths || count == 0)
|
||||
return;
|
||||
|
||||
new_count = 0;
|
||||
for (i = 0; i < count; i++) {
|
||||
int found = 0;
|
||||
extract_module_id(paths[i], incoming_id);
|
||||
|
||||
for (j = 0; j < stk_pending_count; j++) {
|
||||
extract_module_id(stk_pending[j], existing_id);
|
||||
if (strncmp(existing_id, incoming_id,
|
||||
STK_MOD_ID_BUFFER) == 0) {
|
||||
strncpy(stk_pending[j], paths[i],
|
||||
STK_PATH_MAX_OS - 1);
|
||||
stk_pending[j][STK_PATH_MAX_OS - 1] = '\0';
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
new_count++;
|
||||
}
|
||||
|
||||
if (new_count == 0)
|
||||
return;
|
||||
|
||||
new_pending =
|
||||
malloc((stk_pending_count + new_count) * sizeof(*stk_pending));
|
||||
if (!new_pending)
|
||||
return;
|
||||
|
||||
for (i = 0; i < stk_pending_count; i++)
|
||||
memcpy(new_pending[i], stk_pending[i], STK_PATH_MAX_OS);
|
||||
|
||||
free(stk_pending);
|
||||
stk_pending = new_pending;
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
int found = 0;
|
||||
extract_module_id(paths[i], incoming_id);
|
||||
|
||||
for (j = 0; j < stk_pending_count; j++) {
|
||||
extract_module_id(stk_pending[j], existing_id);
|
||||
if (strncmp(existing_id, incoming_id,
|
||||
STK_MOD_ID_BUFFER) == 0) {
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
strncpy(stk_pending[stk_pending_count], paths[i],
|
||||
STK_PATH_MAX_OS - 1);
|
||||
stk_pending[stk_pending_count][STK_PATH_MAX_OS - 1] =
|
||||
'\0';
|
||||
stk_pending_count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void stk_pending_remove(const char *id)
|
||||
{
|
||||
size_t i, write;
|
||||
@@ -593,6 +997,7 @@ size_t stk_pending_retry(void)
|
||||
unsigned char deps_satisfied;
|
||||
unsigned char result;
|
||||
void *handle;
|
||||
void *test;
|
||||
union {
|
||||
void *obj;
|
||||
const char *(*meta_func)(void);
|
||||
@@ -600,11 +1005,44 @@ size_t stk_pending_retry(void)
|
||||
const stk_dep_t *deps;
|
||||
size_t dep_count;
|
||||
int found;
|
||||
size_t write;
|
||||
char pending_id[STK_MOD_ID_BUFFER];
|
||||
|
||||
if (!stk_pending_count)
|
||||
return 0;
|
||||
|
||||
write = 0;
|
||||
for (i = 0; i < stk_pending_count; i++) {
|
||||
test = platform_load_library(stk_pending[i]);
|
||||
if (test) {
|
||||
platform_unload_library(test);
|
||||
if (write != i)
|
||||
memcpy(stk_pending[write], stk_pending[i],
|
||||
STK_PATH_MAX_OS);
|
||||
write++;
|
||||
}
|
||||
}
|
||||
stk_pending_count = write;
|
||||
|
||||
if (!stk_pending_count) {
|
||||
stk_pending_free();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (stk_module_realloc_memory(module_count + stk_pending_count) != 0)
|
||||
return 0;
|
||||
|
||||
for (i = 0; i < stk_pending_count; i++) {
|
||||
extract_module_id(stk_pending[i], pending_id);
|
||||
if (is_mod_loaded(pending_id) >= 0) {
|
||||
memcpy(stk_pending[i],
|
||||
stk_pending[stk_pending_count - 1],
|
||||
STK_PATH_MAX_OS);
|
||||
stk_pending_count--;
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
|
||||
handle = platform_load_library(stk_pending[i]);
|
||||
if (!handle)
|
||||
continue;
|
||||
@@ -641,9 +1079,6 @@ size_t stk_pending_retry(void)
|
||||
continue;
|
||||
|
||||
attempt_load:
|
||||
if (stk_module_realloc_memory(module_count + 1) != 0)
|
||||
continue;
|
||||
|
||||
result = stk_module_load(stk_pending[i], module_count);
|
||||
if (result != STK_MOD_INIT_SUCCESS)
|
||||
continue;
|
||||
@@ -660,6 +1095,9 @@ size_t stk_pending_retry(void)
|
||||
if (stk_pending_count == 0)
|
||||
stk_pending_free();
|
||||
|
||||
if (loaded > 0)
|
||||
stk_module_realloc_memory(module_count);
|
||||
|
||||
return loaded;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -278,7 +278,7 @@ void *platform_load_library(const char *path)
|
||||
#ifdef _WIN32
|
||||
return (void *)LoadLibraryA(path);
|
||||
#else
|
||||
return dlopen(path, RTLD_NOW);
|
||||
return dlopen(path, RTLD_NOW | RTLD_GLOBAL);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,11 @@ void extract_module_id(const char *path, char *out_id);
|
||||
int is_mod_loaded(const char *module_id);
|
||||
|
||||
size_t stk_module_count(void);
|
||||
unsigned char stk_module_preload(const char *path, int index);
|
||||
unsigned char stk_module_activate(size_t index);
|
||||
unsigned char stk_validate_dependencies_single(size_t index);
|
||||
void stk_log_dependency_failures(size_t index, const char *action);
|
||||
void stk_module_discard(size_t index);
|
||||
unsigned char stk_module_load(const char *path, int index);
|
||||
unsigned char stk_module_load_init(const char *path, int index);
|
||||
unsigned char stk_module_init_memory(size_t capacity);
|
||||
@@ -54,8 +59,13 @@ void stk_module_unload_all(void);
|
||||
unsigned char stk_validate_dependencies(size_t count);
|
||||
unsigned char stk_topo_sort(size_t count, size_t *order);
|
||||
void stk_pending_add(const char *path);
|
||||
void stk_pending_add_batch(const char (*paths)[STK_PATH_MAX_OS], size_t count);
|
||||
void stk_pending_remove(const char *id);
|
||||
size_t stk_pending_retry(void);
|
||||
void stk_sort_unload_order(size_t *indices, size_t n);
|
||||
void stk_collect_dependents(size_t *indices, size_t *count, size_t capacity);
|
||||
void stk_sort_load_order(int *file_indices, size_t n,
|
||||
char (*file_names)[STK_PATH_MAX], const char *tmp_dir);
|
||||
|
||||
static void build_path(char *dest, size_t dest_size, const char *dir,
|
||||
const char *file)
|
||||
@@ -113,7 +123,8 @@ static void stk_log_module(size_t index)
|
||||
static void stk_log_modules(void)
|
||||
{
|
||||
size_t i;
|
||||
stk_log(STK_LOG_INFO, "Loaded modules (%lu):", module_count);
|
||||
stk_log(STK_LOG_INFO,
|
||||
"Loaded modules (%lu):", (unsigned long)module_count);
|
||||
for (i = 0; i < module_count; i++)
|
||||
stk_log_module(i);
|
||||
}
|
||||
@@ -121,19 +132,20 @@ static void stk_log_modules(void)
|
||||
unsigned char stk_init(void)
|
||||
{
|
||||
char (*files)[STK_PATH_MAX] = NULL;
|
||||
size_t file_count, i, successful_loads = 0;
|
||||
char (*test_scan)[STK_PATH_MAX];
|
||||
size_t file_count, i, j, write, successful_loads = 0;
|
||||
size_t index, test_count;
|
||||
char full_path[STK_PATH_MAX_OS];
|
||||
char tmp_path[STK_PATH_MAX_OS];
|
||||
int load_result;
|
||||
unsigned char dep_result;
|
||||
size_t *order = NULL;
|
||||
char (*init_batch)[STK_PATH_MAX_OS] = NULL;
|
||||
size_t init_batch_count = 0;
|
||||
|
||||
platform_mkdir(stk_mod_dir);
|
||||
build_path(stk_tmp_dir, sizeof(stk_tmp_dir), stk_mod_dir, stk_tmp_name);
|
||||
if (platform_mkdir(stk_tmp_dir) != STK_PLATFORM_OPERATION_SUCCESS) {
|
||||
char (*test_scan)[STK_PATH_MAX];
|
||||
size_t test_count;
|
||||
|
||||
test_scan =
|
||||
platform_directory_init_scan(stk_tmp_dir, &test_count);
|
||||
if (test_scan)
|
||||
@@ -168,13 +180,15 @@ unsigned char stk_init(void)
|
||||
continue;
|
||||
}
|
||||
|
||||
load_result = stk_module_load_init(tmp_path, successful_loads);
|
||||
load_result = stk_module_preload(tmp_path, successful_loads);
|
||||
|
||||
if (load_result != STK_MOD_INIT_SUCCESS) {
|
||||
stk_log(STK_LOG_ERROR, "Failed to load module %s: %s",
|
||||
files[i], stk_error_string(load_result));
|
||||
stk_log(STK_LOG_ERROR,
|
||||
"Failed to preload module %s: %s", files[i],
|
||||
stk_error_string(load_result));
|
||||
} else {
|
||||
successful_loads++;
|
||||
module_count++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,34 +200,66 @@ unsigned char stk_init(void)
|
||||
if (module_count == 0)
|
||||
goto scanned;
|
||||
|
||||
dep_result = stk_validate_dependencies(module_count);
|
||||
if (dep_result != STK_MOD_INIT_SUCCESS) {
|
||||
size_t j;
|
||||
char mod_tmp_path[STK_PATH_MAX_OS];
|
||||
stk_log(STK_LOG_WARN,
|
||||
"Some modules have unmet dependencies, deferring");
|
||||
for (j = 0; j < module_count; j++) {
|
||||
if (stk_modules[j].dep_count > 0) {
|
||||
build_path(mod_tmp_path, sizeof(mod_tmp_path),
|
||||
stk_tmp_dir, stk_modules[j].id);
|
||||
strncat(mod_tmp_path, STK_MODULE_EXT,
|
||||
sizeof(mod_tmp_path) -
|
||||
strlen(mod_tmp_path) - 1);
|
||||
stk_pending_add(mod_tmp_path);
|
||||
stk_module_unload(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
order = malloc(module_count * sizeof(size_t));
|
||||
if (order) {
|
||||
dep_result = stk_topo_sort(module_count, order);
|
||||
if (dep_result != STK_MOD_INIT_SUCCESS)
|
||||
stk_log(STK_LOG_ERROR, "Dependency sort failed: %s",
|
||||
stk_error_string(dep_result));
|
||||
free(order);
|
||||
}
|
||||
|
||||
init_batch = malloc(module_count * sizeof(*init_batch));
|
||||
|
||||
for (j = 0; j < module_count; j++) {
|
||||
index = order ? order[j] : j;
|
||||
dep_result = stk_validate_dependencies_single(index);
|
||||
if (dep_result != STK_MOD_INIT_SUCCESS) {
|
||||
stk_log_dependency_failures(index, "Deferring");
|
||||
if (init_batch) {
|
||||
build_path(init_batch[init_batch_count],
|
||||
sizeof(init_batch[init_batch_count]),
|
||||
stk_tmp_dir, stk_modules[index].id);
|
||||
strncat(
|
||||
init_batch[init_batch_count],
|
||||
STK_MODULE_EXT,
|
||||
sizeof(init_batch[init_batch_count]) -
|
||||
strlen(init_batch[init_batch_count]) -
|
||||
1);
|
||||
init_batch_count++;
|
||||
}
|
||||
stk_module_discard(index);
|
||||
continue;
|
||||
}
|
||||
if (stk_module_activate(index) != STK_MOD_INIT_SUCCESS) {
|
||||
stk_log(STK_LOG_ERROR, "Failed to init module %s",
|
||||
stk_modules[index].id);
|
||||
stk_module_discard(index);
|
||||
}
|
||||
}
|
||||
|
||||
if (init_batch_count > 0)
|
||||
stk_pending_add_batch(
|
||||
(const char (*)[STK_PATH_MAX_OS])init_batch,
|
||||
init_batch_count);
|
||||
|
||||
free(init_batch);
|
||||
init_batch = NULL;
|
||||
|
||||
if (order) {
|
||||
free(order);
|
||||
order = NULL;
|
||||
}
|
||||
|
||||
write = 0;
|
||||
for (j = 0; j < module_count; j++) {
|
||||
if (stk_modules[j].handle != NULL) {
|
||||
if (write != j)
|
||||
stk_modules[write] = stk_modules[j];
|
||||
write++;
|
||||
}
|
||||
}
|
||||
module_count = write;
|
||||
|
||||
scanned:
|
||||
watch_handle = platform_directory_watch_start(stk_mod_dir);
|
||||
if (!watch_handle) {
|
||||
@@ -224,6 +270,8 @@ scanned:
|
||||
return STK_INIT_WATCH_ERROR;
|
||||
}
|
||||
|
||||
stk_pending_retry();
|
||||
|
||||
stk_log(STK_LOG_INFO, "stk v%s initialized, watching %s/",
|
||||
STK_VERSION_STRING, stk_mod_dir);
|
||||
if (module_count > 0)
|
||||
@@ -261,8 +309,7 @@ size_t stk_poll(void)
|
||||
unload_count = 0;
|
||||
int *reloaded_mod_indices = NULL, *reloaded_mod_file_indices = NULL,
|
||||
*unloaded_mod_indices = NULL, *loaded_mod_indices = NULL;
|
||||
size_t remaining_loads, new_capacity, holes_to_fill;
|
||||
size_t write_pos, read_pos;
|
||||
size_t new_capacity;
|
||||
char full_path[STK_PATH_MAX_OS], tmp_path[STK_PATH_MAX_OS];
|
||||
char mod_id[STK_MOD_ID_BUFFER];
|
||||
int load_result;
|
||||
@@ -270,6 +317,21 @@ size_t stk_poll(void)
|
||||
char (*module_ids)[STK_MOD_ID_BUFFER] = NULL;
|
||||
unsigned char dep_result;
|
||||
size_t *order = NULL;
|
||||
size_t *unload_order = NULL;
|
||||
size_t expanded_count;
|
||||
size_t index, oi;
|
||||
int is_orig;
|
||||
size_t write;
|
||||
int file_index, mod_index;
|
||||
size_t *cascade_indices = NULL;
|
||||
size_t cascade_count;
|
||||
size_t j, k, cascade_write;
|
||||
char (*dep_batch)[STK_PATH_MAX_OS] = NULL;
|
||||
size_t dep_batch_count = 0;
|
||||
char (*cascade_batch)[STK_PATH_MAX_OS] = NULL;
|
||||
size_t cascade_batch_count = 0;
|
||||
char (*load_batch)[STK_PATH_MAX_OS] = NULL;
|
||||
size_t load_batch_count = 0;
|
||||
|
||||
if (module_count > 0) {
|
||||
module_ids = malloc(module_count * sizeof(*module_ids));
|
||||
@@ -345,22 +407,89 @@ size_t stk_poll(void)
|
||||
goto begin_operations;
|
||||
|
||||
handle_grow:
|
||||
remaining_loads = load_count - unload_count;
|
||||
new_capacity = module_count + remaining_loads;
|
||||
new_capacity = module_count + load_count;
|
||||
if (stk_module_realloc_memory(new_capacity) != STK_MOD_INIT_SUCCESS)
|
||||
goto free_poll;
|
||||
|
||||
begin_operations:
|
||||
unload_order = malloc(module_count * sizeof(size_t));
|
||||
if (unload_order) {
|
||||
expanded_count = unload_count;
|
||||
|
||||
for (i = 0; i < unload_count; i++)
|
||||
unload_order[i] = (size_t)unloaded_mod_indices[i];
|
||||
|
||||
stk_collect_dependents(unload_order, &expanded_count,
|
||||
module_count);
|
||||
stk_sort_unload_order(unload_order, expanded_count);
|
||||
|
||||
dep_batch = malloc(expanded_count * sizeof(*dep_batch));
|
||||
dep_batch_count = 0;
|
||||
|
||||
for (i = 0; i < expanded_count; i++) {
|
||||
index = unload_order[i];
|
||||
|
||||
stk_log(STK_LOG_INFO, "Unloaded module: %s",
|
||||
stk_modules[index].id);
|
||||
stk_pending_remove(stk_modules[index].id);
|
||||
|
||||
is_orig = 0;
|
||||
for (oi = 0; oi < unload_count; oi++) {
|
||||
if ((size_t)unloaded_mod_indices[oi] == index) {
|
||||
is_orig = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!is_orig && dep_batch) {
|
||||
build_path(dep_batch[dep_batch_count],
|
||||
sizeof(dep_batch[dep_batch_count]),
|
||||
stk_tmp_dir, stk_modules[index].id);
|
||||
strncat(
|
||||
dep_batch[dep_batch_count], STK_MODULE_EXT,
|
||||
sizeof(dep_batch[dep_batch_count]) -
|
||||
strlen(dep_batch[dep_batch_count]) - 1);
|
||||
dep_batch_count++;
|
||||
}
|
||||
|
||||
stk_module_unload(index);
|
||||
}
|
||||
|
||||
if (dep_batch_count > 0)
|
||||
stk_pending_add_batch(
|
||||
(const char (*)[STK_PATH_MAX_OS])dep_batch,
|
||||
dep_batch_count);
|
||||
|
||||
free(dep_batch);
|
||||
dep_batch = NULL;
|
||||
free(unload_order);
|
||||
unload_order = NULL;
|
||||
} else {
|
||||
for (i = 0; i < unload_count; ++i) {
|
||||
stk_log(STK_LOG_INFO, "Unloaded module: %s",
|
||||
stk_modules[unloaded_mod_indices[i]].id);
|
||||
stk_pending_remove(stk_modules[unloaded_mod_indices[i]].id);
|
||||
stk_pending_remove(
|
||||
stk_modules[unloaded_mod_indices[i]].id);
|
||||
stk_module_unload(unloaded_mod_indices[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (unload_count > 0) {
|
||||
write = 0;
|
||||
for (i = 0; i < module_count; i++) {
|
||||
if (stk_modules[i].handle != NULL) {
|
||||
if (write != i)
|
||||
stk_modules[write] = stk_modules[i];
|
||||
write++;
|
||||
}
|
||||
}
|
||||
module_count = write;
|
||||
if (module_count > 0)
|
||||
stk_module_realloc_memory(module_count);
|
||||
}
|
||||
|
||||
for (i = 0; i < reload_count; ++i) {
|
||||
int file_index = reloaded_mod_file_indices[i];
|
||||
int mod_index = reloaded_mod_indices[i];
|
||||
file_index = reloaded_mod_file_indices[i];
|
||||
mod_index = reloaded_mod_indices[i];
|
||||
|
||||
build_path(full_path, sizeof(full_path), stk_mod_dir,
|
||||
file_list[file_index]);
|
||||
@@ -383,57 +512,36 @@ begin_operations:
|
||||
stk_error_string(load_result));
|
||||
}
|
||||
|
||||
holes_to_fill = (load_count < unload_count) ? load_count : unload_count;
|
||||
for (i = 0; i < holes_to_fill; ++i) {
|
||||
int target_index = unloaded_mod_indices[i];
|
||||
int file_index = loaded_mod_indices[i];
|
||||
|
||||
for (i = 0; i < load_count; i++) {
|
||||
file_index = loaded_mod_indices[i];
|
||||
build_path(full_path, sizeof(full_path), stk_mod_dir,
|
||||
file_list[file_index]);
|
||||
build_path(tmp_path, sizeof(tmp_path), stk_tmp_dir,
|
||||
file_list[file_index]);
|
||||
|
||||
if (platform_copy_file(full_path, tmp_path) !=
|
||||
STK_PLATFORM_OPERATION_SUCCESS) {
|
||||
stk_log(STK_LOG_ERROR, "Failed to copy %s for loading",
|
||||
file_list[file_index]);
|
||||
continue;
|
||||
platform_copy_file(full_path, tmp_path);
|
||||
}
|
||||
|
||||
load_result = stk_module_load(tmp_path, target_index);
|
||||
if (load_result != STK_MOD_INIT_SUCCESS)
|
||||
stk_log(STK_LOG_ERROR, "Failed to load module %s: %s",
|
||||
file_list[file_index],
|
||||
stk_error_string(load_result));
|
||||
}
|
||||
if (load_count > 1)
|
||||
stk_sort_load_order(loaded_mod_indices, load_count, file_list,
|
||||
stk_tmp_dir);
|
||||
|
||||
if (load_count > unload_count)
|
||||
goto append_modules;
|
||||
load_batch = malloc(load_count * sizeof(*load_batch));
|
||||
load_batch_count = 0;
|
||||
|
||||
if (unload_count > load_count)
|
||||
goto trim_arrays;
|
||||
for (i = 0; i < load_count; ++i) {
|
||||
file_index = loaded_mod_indices[i];
|
||||
|
||||
goto validate_deps;
|
||||
|
||||
append_modules:
|
||||
for (; i < load_count; ++i) {
|
||||
int file_index = loaded_mod_indices[i];
|
||||
|
||||
build_path(full_path, sizeof(full_path), stk_mod_dir,
|
||||
file_list[file_index]);
|
||||
build_path(tmp_path, sizeof(tmp_path), stk_tmp_dir,
|
||||
file_list[file_index]);
|
||||
|
||||
if (platform_copy_file(full_path, tmp_path) !=
|
||||
STK_PLATFORM_OPERATION_SUCCESS) {
|
||||
stk_log(STK_LOG_ERROR, "Failed to copy %s for loading",
|
||||
file_list[file_index]);
|
||||
continue;
|
||||
}
|
||||
|
||||
load_result = stk_module_load(tmp_path, module_count +
|
||||
successful_appends);
|
||||
if (load_result != STK_MOD_INIT_SUCCESS) {
|
||||
if (load_result == STK_MOD_DEP_NOT_FOUND_ERROR ||
|
||||
load_result == STK_MOD_DEP_VERSION_MISMATCH_ERROR) {
|
||||
if (load_batch)
|
||||
memcpy(load_batch[load_batch_count++], tmp_path,
|
||||
STK_PATH_MAX_OS);
|
||||
} else if (load_result != STK_MOD_INIT_SUCCESS) {
|
||||
stk_log(STK_LOG_ERROR, "Failed to load module %s: %s",
|
||||
file_list[file_index],
|
||||
stk_error_string(load_result));
|
||||
@@ -444,73 +552,79 @@ append_modules:
|
||||
|
||||
module_count += successful_appends;
|
||||
|
||||
if (successful_appends < (load_count - holes_to_fill))
|
||||
if (successful_appends < load_count)
|
||||
stk_module_realloc_memory(module_count);
|
||||
|
||||
if (load_batch_count > 0)
|
||||
stk_pending_add_batch(
|
||||
(const char (*)[STK_PATH_MAX_OS])load_batch,
|
||||
load_batch_count);
|
||||
|
||||
free(load_batch);
|
||||
load_batch = NULL;
|
||||
|
||||
goto validate_deps;
|
||||
|
||||
trim_arrays:
|
||||
write_pos = unloaded_mod_indices[holes_to_fill];
|
||||
for (i = holes_to_fill + 1; i < unload_count; ++i) {
|
||||
if (unloaded_mod_indices[i] < write_pos)
|
||||
write_pos = unloaded_mod_indices[i];
|
||||
}
|
||||
|
||||
for (read_pos = write_pos + 1; read_pos < module_count; ++read_pos) {
|
||||
if (stk_modules[read_pos].handle != NULL) {
|
||||
stk_modules[write_pos] = stk_modules[read_pos];
|
||||
++write_pos;
|
||||
}
|
||||
}
|
||||
|
||||
module_count = write_pos;
|
||||
stk_module_realloc_memory(module_count);
|
||||
|
||||
validate_deps:
|
||||
if (module_count == 0)
|
||||
goto free_poll;
|
||||
|
||||
{
|
||||
size_t cascade_indices[STK_PATH_MAX];
|
||||
size_t cascade_count;
|
||||
char cascade_tmp_path[STK_PATH_MAX_OS];
|
||||
size_t j, k, cascade_write;
|
||||
|
||||
do {
|
||||
cascade_count = 0;
|
||||
|
||||
cascade_indices = malloc(module_count * sizeof(size_t));
|
||||
if (!cascade_indices)
|
||||
break;
|
||||
|
||||
for (j = 0; j < module_count; j++) {
|
||||
if (stk_modules[j].dep_count == 0)
|
||||
continue;
|
||||
for (k = 0; k < stk_modules[j].dep_count; k++) {
|
||||
if (is_mod_loaded(
|
||||
stk_modules[j].deps[k].id) <
|
||||
if (is_mod_loaded(stk_modules[j].deps[k].id) <
|
||||
0) {
|
||||
cascade_indices
|
||||
[cascade_count++] = j;
|
||||
cascade_indices[cascade_count++] = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (cascade_count == 0)
|
||||
if (cascade_count == 0) {
|
||||
free(cascade_indices);
|
||||
cascade_indices = NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
cascade_batch = malloc(cascade_count * sizeof(*cascade_batch));
|
||||
cascade_batch_count = 0;
|
||||
|
||||
for (j = 0; j < cascade_count; j++) {
|
||||
size_t index = cascade_indices[j];
|
||||
stk_log(STK_LOG_WARN,
|
||||
"Unloading '%s': unmet dependencies",
|
||||
stk_modules[index].id);
|
||||
build_path(cascade_tmp_path,
|
||||
sizeof(cascade_tmp_path),
|
||||
index = cascade_indices[j];
|
||||
stk_log_dependency_failures(index, "Unloading");
|
||||
if (cascade_batch) {
|
||||
build_path(
|
||||
cascade_batch[cascade_batch_count],
|
||||
sizeof(cascade_batch[cascade_batch_count]),
|
||||
stk_tmp_dir, stk_modules[index].id);
|
||||
strncat(cascade_tmp_path, STK_MODULE_EXT,
|
||||
sizeof(cascade_tmp_path) -
|
||||
strlen(cascade_tmp_path) - 1);
|
||||
stk_pending_add(cascade_tmp_path);
|
||||
strncat(
|
||||
cascade_batch[cascade_batch_count],
|
||||
STK_MODULE_EXT,
|
||||
sizeof(cascade_batch[cascade_batch_count]) -
|
||||
strlen(cascade_batch
|
||||
[cascade_batch_count]) -
|
||||
1);
|
||||
cascade_batch_count++;
|
||||
}
|
||||
stk_module_unload(index);
|
||||
}
|
||||
|
||||
if (cascade_batch_count > 0)
|
||||
stk_pending_add_batch(
|
||||
(const char (*)[STK_PATH_MAX_OS])cascade_batch,
|
||||
cascade_batch_count);
|
||||
|
||||
free(cascade_batch);
|
||||
cascade_batch = NULL;
|
||||
|
||||
cascade_write = 0;
|
||||
for (j = 0; j < module_count; j++) {
|
||||
if (stk_modules[j].handle != NULL) {
|
||||
@@ -522,8 +636,10 @@ validate_deps:
|
||||
}
|
||||
module_count = cascade_write;
|
||||
|
||||
free(cascade_indices);
|
||||
cascade_indices = NULL;
|
||||
|
||||
} while (cascade_count > 0);
|
||||
}
|
||||
|
||||
if (module_count > 0)
|
||||
stk_module_realloc_memory(module_count);
|
||||
@@ -537,12 +653,12 @@ validate_deps:
|
||||
free(order);
|
||||
}
|
||||
|
||||
free_poll:
|
||||
stk_pending_retry();
|
||||
|
||||
if (module_count > 0)
|
||||
stk_log_modules();
|
||||
|
||||
free_poll:
|
||||
free(reloaded_mod_indices);
|
||||
free(reloaded_mod_file_indices);
|
||||
free(unloaded_mod_indices);
|
||||
|
||||
Reference in New Issue
Block a user