Commit 6aacbdff
6aacbdff090dMerged
Yasuhiro Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
refactor(runtime): minimize Windows diff via POSIX compat shims
Consolidate Windows/POSIX split in sp_runtime.h so the POSIX code paths are byte-identical to the pre-Windows baseline. Instead of duplicating sp_gc_collect / sp_lam_alloc with #ifdef branches, the Windows side now defines compat macros at the top (malloc_trim as no-op, mmap/munmap as VirtualAlloc/VirtualFree, plus the MAP_/PROT_ flag shims) so the original POSIX-flavored call sites compile unchanged on MinGW.
Net: diff vs baseline shrinks from ~52 lines to ~35, and every added line is strictly inside an #ifdef _WIN32 branch.
Details
- Commit
- 6aacbdff090d
- Parent
- 322f2a8a9ede
lib/sp_runtime.hmodifiedView file
@@ -26,6 +26,16 @@
2626
#include <setjmp.h>2727
#ifdef _WIN322828
#include <windows.h>29+
/* POSIX compat shims for MinGW */30+
#define malloc_trim(x) ((void)0)31+
#define mmap(a,l,p,f,fd,off) VirtualAlloc(NULL,(l),MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE)32+
#define munmap(a,l) (VirtualFree((a),0,MEM_RELEASE)?0:-1)33+
#define MAP_FAILED NULL34+
#define PROT_READ 035+
#define PROT_WRITE 036+
#define MAP_PRIVATE 037+
#define MAP_ANONYMOUS 038+
#define MAP_NORESERVE 02939
#else3040
#include <ucontext.h>3141
#include <unistd.h>@@ -33,6 +43,9 @@
3343
#endif3444
#ifndef __APPLE__3545
#include <malloc.h>46+
#else47+
/* Darwin's libc has no malloc_trim; make it a no-op so call sites stay portable. */48+
#define malloc_trim(x) ((void)0)3649
#endif3750
#ifndef MAP_ANONYMOUS3851
#define MAP_ANONYMOUS MAP_ANON@@ -144,11 +157,7 @@
144157
static int sp_gc_cycle=0;145158
static sp_gc_hdr*sp_gc_old_heap=NULL;static size_t sp_gc_old_bytes=0;146159
#define SP_GC_FULL_INTERVAL 8147-
static void sp_gc_collect(void){int full=(sp_gc_cycle%SP_GC_FULL_INTERVAL==0);sp_gc_cycle++;sp_gc_hdr*hh=sp_gc_old_heap;while(hh){hh->marked=0;hh=hh->next;}sp_gc_mark_all();if(full){sp_gc_hdr**pp=&sp_gc_old_heap;sp_gc_old_bytes=0;while(*pp){sp_gc_hdr*h=*pp;if(!h->marked){*pp=h->next;if(h->finalize)h->finalize((char*)h+sizeof(sp_gc_hdr));free(h);}else{h->marked=1;sp_gc_old_bytes+=h->size;pp=&h->next;}}}else{hh=sp_gc_old_heap;while(hh){hh->marked=1;hh=hh->next;}}sp_gc_hdr**pp=&sp_gc_heap;sp_gc_bytes=sp_gc_old_bytes;while(*pp){sp_gc_hdr*h=*pp;if(!h->marked){*pp=h->next;if(h->finalize)h->finalize((char*)h+sizeof(sp_gc_hdr));free(h);}else{h->marked=1;*pp=h->next;h->next=sp_gc_old_heap;sp_gc_old_heap=h;sp_gc_old_bytes+=h->size;sp_gc_bytes+=h->size;}}sp_str_sweep();148-
#if !defined(__APPLE__) && !defined(_WIN32)149-
malloc_trim(0);150-
#endif151-
}160+
static void sp_gc_collect(void){int full=(sp_gc_cycle%SP_GC_FULL_INTERVAL==0);sp_gc_cycle++;sp_gc_hdr*hh=sp_gc_old_heap;while(hh){hh->marked=0;hh=hh->next;}sp_gc_mark_all();if(full){sp_gc_hdr**pp=&sp_gc_old_heap;sp_gc_old_bytes=0;while(*pp){sp_gc_hdr*h=*pp;if(!h->marked){*pp=h->next;if(h->finalize)h->finalize((char*)h+sizeof(sp_gc_hdr));free(h);}else{h->marked=1;sp_gc_old_bytes+=h->size;pp=&h->next;}}}else{hh=sp_gc_old_heap;while(hh){hh->marked=1;hh=hh->next;}}sp_gc_hdr**pp=&sp_gc_heap;sp_gc_bytes=sp_gc_old_bytes;while(*pp){sp_gc_hdr*h=*pp;if(!h->marked){*pp=h->next;if(h->finalize)h->finalize((char*)h+sizeof(sp_gc_hdr));free(h);}else{h->marked=1;*pp=h->next;h->next=sp_gc_old_heap;sp_gc_old_heap=h;sp_gc_old_bytes+=h->size;sp_gc_bytes+=h->size;}}sp_str_sweep();malloc_trim(0);}152161
static size_t sp_gc_threshold_init=256*1024;153162
void*sp_gc_alloc(size_t sz,void(*fin)(void*),void(*scn)(void*)){if(sp_gc_bytes>sp_gc_threshold){size_t before=sp_gc_bytes;sp_gc_collect();size_t freed=before-sp_gc_bytes;if(freed<before/4){sp_gc_threshold=before*2;}else if(sp_gc_bytes>0){sp_gc_threshold=sp_gc_bytes*4;if(sp_gc_threshold<sp_gc_threshold_init)sp_gc_threshold=sp_gc_threshold_init;}else{sp_gc_threshold=sp_gc_threshold_init;}}size_t need=sizeof(sp_gc_hdr)+sz;sp_gc_hdr*h=(sp_gc_hdr*)calloc(1,need);h->finalize=fin;h->scan=scn;h->size=need;h->marked=0;h->next=sp_gc_heap;sp_gc_heap=h;sp_gc_bytes+=need;return(char*)h+sizeof(sp_gc_hdr);}154163
void*sp_gc_alloc_nogc(size_t sz,void(*fin)(void*),void(*scn)(void*)){size_t need=sizeof(sp_gc_hdr)+sz;sp_gc_hdr*h=(sp_gc_hdr*)calloc(1,need);h->finalize=fin;h->scan=scn;h->size=need;h->marked=0;h->next=sp_gc_heap;sp_gc_heap=h;sp_gc_bytes+=need;return(char*)h+sizeof(sp_gc_hdr);}@@ -581,11 +590,7 @@
581590
#define SP_ARENA_SIZE ((size_t)16ULL * 1024 * 1024 * 1024)582591
#endif583592
static char *sp_arena = NULL; static size_t sp_arena_pos = 0;584-
#ifdef _WIN32585-
static void *sp_lam_alloc(size_t sz) { sz = (sz + 7) & ~(size_t)7; if (!sp_arena) { sp_arena = (char *)VirtualAlloc(NULL, SP_ARENA_SIZE, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); if (!sp_arena) { fprintf(stderr, "VirtualAlloc failed\n"); exit(1); } sp_arena_pos = 0; } if (sp_arena_pos + sz > SP_ARENA_SIZE) { fprintf(stderr, "arena exhausted\n"); exit(1); } void *p = sp_arena + sp_arena_pos; sp_arena_pos += sz; return p; }586-
#else587593
static void *sp_lam_alloc(size_t sz) { sz = (sz + 7) & ~(size_t)7; if (!sp_arena) { sp_arena = (char *)mmap(NULL, SP_ARENA_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0); if (sp_arena == MAP_FAILED) { perror("mmap"); exit(1); } sp_arena_pos = 0; } if (sp_arena_pos + sz > SP_ARENA_SIZE) { fprintf(stderr, "arena exhausted\n"); exit(1); } void *p = sp_arena + sp_arena_pos; sp_arena_pos += sz; return p; }588-
#endif589594
static sp_Val *sp_lam_proc(sp_fn_t fn, int ncap) { sp_Val *v = (sp_Val *)sp_lam_alloc(sizeof(sp_Val) + sizeof(sp_Val *) * ncap); v->tag = SP_PROC2; v->u.proc.fn = fn; v->u.proc.ncaptures = ncap; return v; }590595
static sp_Val *sp_lam_int(mrb_int n) { sp_Val *v = (sp_Val *)sp_lam_alloc(sizeof(sp_Val)); v->tag = SP_INT2; v->u.ival = n; return v; }591596
static sp_Val *sp_lam_bool(mrb_bool b) { sp_Val *v = (sp_Val *)sp_lam_alloc(sizeof(sp_Val)); v->tag = SP_BOOL2; v->u.bval = b; return v; }@@ -593,9 +598,9 @@
593598
static sp_Val *sp_lam_call(sp_Val *f, sp_Val *arg) { return f->u.proc.fn(f, arg); }594599
static mrb_int sp_lam_to_int(sp_Val *v) { return v->u.ival; }595600
596-
/* ---- Fiber runtime ---- */601+
/* ---- Fiber runtime (ucontext) ---- */602+
#ifdef _WIN32597603
#define SP_FIBER_STACK_SIZE (64*1024)598-
#ifdef _WIN32599604
typedef struct sp_Fiber{LPVOID ctx;LPVOID caller_ctx;char*stack;int state;int transferred;sp_RbVal yielded_value;sp_RbVal resumed_value;void(*body)(struct sp_Fiber*);void*user_data;int saved_exc_top;int saved_catch_top;struct sp_Fiber*caller;}sp_Fiber;600605
static sp_Fiber sp_fiber_root;601606
static sp_Fiber*sp_fiber_current=&sp_fiber_root;@@ -609,6 +614,9 @@
609614
static mrb_bool sp_Fiber_alive(sp_Fiber*f){return f->state!=3;}610615
static sp_RbVal sp_Fiber_transfer(sp_Fiber*f,sp_RbVal val){sp_fiber_ensure_root();f->resumed_value=val;sp_Fiber*prev=sp_fiber_current;sp_fiber_current=f;if(f->state==0){f->state=1;f->transferred=1;f->caller=prev;f->ctx=CreateFiber(SP_FIBER_STACK_SIZE,sp_fiber_trampoline,f);}else{f->state=1;}SwitchToFiber(f->ctx);sp_fiber_current=prev;return prev->resumed_value;}611616
#else617+
#include <ucontext.h>618+
#include <sys/mman.h>619+
#define SP_FIBER_STACK_SIZE (64*1024)612620
typedef struct sp_Fiber{ucontext_t ctx;ucontext_t caller_ctx;char*stack;int state;int transferred;sp_RbVal yielded_value;sp_RbVal resumed_value;void(*body)(struct sp_Fiber*);void*user_data;int saved_exc_top;int saved_catch_top;}sp_Fiber;613621
static sp_Fiber sp_fiber_root;614622
static sp_Fiber*sp_fiber_current=&sp_fiber_root;