diff --git a/include/dmosi.h b/include/dmosi.h index 8c50ad7..2df38ef 100644 --- a/include/dmosi.h +++ b/include/dmosi.h @@ -372,6 +372,16 @@ typedef struct { uint64_t runtime_ms; /**< Thread runtime in milliseconds */ } dmosi_thread_info_t; +/** + * @brief Extra stack bytes reserved for thread and module startup overhead + * + * The thread entry wrapper and module loading infrastructure consume stack + * before the module's own main function is invoked. This constant accounts + * for that overhead so that the effective stack available to the module + * matches the size declared in its header. + */ +#define DMOSI_THREAD_STACK_OVERHEAD 512 + /** * @brief Create a thread * diff --git a/src/dmosi.c b/src/dmosi.c index 8d65cf1..4d83d3f 100644 --- a/src/dmosi.c +++ b/src/dmosi.c @@ -593,11 +593,12 @@ static Dmod_Pid_t dmod_spawn_module_internal(Dmod_Context_t* Context, int argc, spawn_args->argv = argv; spawn_args->process = new_process; - // Get stack size from Context header + // Get stack size from Context header and add overhead for thread/module startup uint64_t stack_size = Dmod_GetStackSize(Context); if (stack_size == 0) { stack_size = DMOSI_DEFAULT_STACK_SIZE; } + stack_size += DMOSI_THREAD_STACK_OVERHEAD; // Inherit priority from current thread int priority = dmosi_thread_get_priority(NULL);