Commit 776dd259

776dd25921f7Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · +1 added · ~1 modified

Description

runtime: extract core value types into sp_types.h

Move the primitive typedefs, the leaf value structs (Range/Class/ Complex/Rational/Encoding), the GC headers, the typed arrays, and the non-poly hashes out of sp_runtime.h into a new lib/sp_types.h that sp_runtime.h includes near the top. Pure type/macro definitions, no function bodies or global state.

This is a no-op reorganization (per-TU content is unchanged since sp_runtime.h includes sp_types.h) that lets libspinel_rt.a sources share the layouts without the header's static/inline function bodies. Groundwork for moving struct-coupled cold helpers into sp_core.c; the allocator-externalization decision that move requires is deferred. sp_RbVal, the poly containers, and the conditional Proc/Fiber/etc. types stay in sp_runtime.h for a later pass.

Bootstrap converges; 640 tests pass; optcarrot checksum 59662.

Details

Commit
776dd25921f7
Parent
4ce9f66a7290

lib/sp_runtime.hmodifiedView file

@@ -60,29 +60,11 @@
6060 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
6161 #endif
6262
63-typedef int64_t mrb_int;
64-typedef double mrb_float;
65-typedef bool mrb_bool;
66-
67-/* Sentinel value reserved by the int? (scalar-nullable int) type. An
68- int? slot is bit-compatible with mrb_int; SP_INT_NIL marks the
69- "nil" inhabitant. The chosen pattern is INT64_MIN, which Ruby's
70- Integer would auto-promote to Bignum (#597 limitation), so the
71- reservation lines up with spinel's existing fast-path-only spec.
72- `sp_int_is_nil(v)` is the canonical predicate; treat any int? value
73- produced by runtime helpers as opaque outside this macro. */
74-#define SP_INT_NIL ((mrb_int)INT64_MIN)
75-#define sp_int_is_nil(v) ((v) == SP_INT_NIL)
76-/* sp_sym is defined per-program in emit_sym_runtime, but poly helpers
77- below need to reference it by forward declaration. */
78-typedef mrb_int sp_sym;
63+/* Core value-type definitions (primitives, leaf structs, GC headers,
64+ typed arrays, non-poly hashes) live in sp_types.h so libspinel_rt.a
65+ sources can share them without the function bodies below. */
66+#include "sp_types.h"
7967 static const char *sp_sym_to_s(sp_sym id);
80-#ifndef TRUE
81-#define TRUE true
82-#endif
83-#ifndef FALSE
84-#define FALSE false
85-#endif
8668
8769 /* sp_raise_cls forward decl — defined later in this header (line ~1017).
8870 Used by the integer-division helpers below to match CRuby semantics:
@@ -226,7 +208,6 @@
226208 /* String -> number parsers now live in libspinel_rt.a (lib/sp_core.c). */
227209 #include "sp_core.h"
228210
229-typedef struct{mrb_int first;mrb_int last;}sp_Range;
230211 static sp_Range sp_range_new(mrb_int f,mrb_int l){sp_Range r;r.first=f;r.last=l;return r;}
231212 /* Inclusive-form `Range#include?`/`#cover?` on the boxed (SP_TAG_OBJ
232213 cls_id SP_BUILTIN_RANGE) Range value. The direct sp_Range typed
@@ -245,14 +226,12 @@
245226 Other Class methods (`.name`, `.inspect`, `.==`, `.!=`,
246227 `.superclass`, `.ancestors`, dynamic `is_a?(c)` against a
247228 variable, etc.) are not yet supported. */
248-typedef struct{mrb_int cls_id;}sp_Class;
249229
250230 /* ---- Complex runtime ---- */
251231 /* Value-type Cartesian Complex: 16 bytes, passed by value. Used by
252232 optcarrot's nestopia palette generator; the palette is precomputed
253233 in the default code path so this is exercised only with
254234 `--nestopia-palette`. */
255-typedef struct{mrb_float re;mrb_float im;}sp_Complex;
256235 static inline sp_Complex sp_complex_polar(mrb_float m,mrb_float a){sp_Complex c;c.re=m*cos(a);c.im=m*sin(a);return c;}
257236 static inline sp_Complex sp_complex_add(sp_Complex a,sp_Complex b){sp_Complex c;c.re=a.re+b.re;c.im=a.im+b.im;return c;}
258237 static inline sp_Complex sp_complex_mul(sp_Complex a,sp_Complex b){sp_Complex c;c.re=a.re*b.re-a.im*b.im;c.im=a.re*b.im+a.im*b.re;return c;}
@@ -286,7 +265,6 @@
286265 Stored in reduced form -- the parser hands us the already-reduced
287266 numerator/denominator from the literal; Integer#quo / arithmetic
288267 normalizes via sp_rational_reduce. Issue #841. */
289-typedef struct{mrb_int num;mrb_int den;}sp_Rational;
290268 static inline mrb_int sp_rational_gcd_i(mrb_int a, mrb_int b) {
291269 if (a < 0) a = -a;
292270 if (b < 0) b = -b;
@@ -328,11 +306,9 @@
328306 recycle(h) on the unmarked object instead of finalize+free. The
329307 hook is responsible for deciding whether to keep the storage
330308 (pool push) or free it. Used by class-instance free-list pools. */
331-typedef struct sp_gc_hdr { struct sp_gc_hdr *next; void (*finalize)(void *); void (*scan)(void *); size_t size; unsigned marked : 1; unsigned frozen : 1; void (*recycle)(struct sp_gc_hdr *); } sp_gc_hdr;
332309 static sp_gc_hdr *sp_gc_heap = NULL; static size_t sp_gc_bytes = 0; static size_t sp_gc_threshold = 256*1024;
333310
334311 /* ---- String GC ---- */
335-typedef struct sp_str_hdr { struct sp_str_hdr *next; size_t size; size_t len; } sp_str_hdr;
336312 static sp_str_hdr *sp_str_heap = NULL;
337313 #define SPL(s) (&("\xff" s)[1])
338314 static const char sp_str_empty_data[] = "\xff";
@@ -382,7 +358,6 @@
382358 as a tiny value type so `__ENCODING__` and `String#encoding` can
383359 answer Ruby's Encoding-shaped protocol without carrying full
384360 transcoding state. */
385-typedef struct{const char *name;}sp_Encoding;
386361 static inline sp_Encoding sp_encoding_utf8(void){return(sp_Encoding){"UTF-8"};}
387362 static inline const char*sp_encoding_name(sp_Encoding e){return e.name?e.name:sp_str_empty;}
388363 static inline const char*sp_encoding_inspect(sp_Encoding e){return sp_sprintf("#<Encoding:%s>",sp_encoding_name(e));}
@@ -841,7 +816,6 @@
841816 []= paths read it from the same cache line as len/cap — no extra
842817 cache miss vs. the GC-header bit. calloc in sp_gc_alloc zero-inits
843818 it, so constructors need no change. Issue #918. */
844-typedef struct{mrb_int*data;mrb_int start;mrb_int len;mrb_int cap;mrb_int frozen;}sp_IntArray;
845819 static void sp_IntArray_fin(void*p){free(((sp_IntArray*)p)->data);}
846820 static sp_IntArray*sp_IntArray_new(void){sp_IntArray*a=(sp_IntArray*)sp_gc_alloc(sizeof(sp_IntArray),sp_IntArray_fin,NULL);a->cap=16;a->data=(mrb_int*)malloc(sizeof(mrb_int)*a->cap);if(!a->data)sp_oom_die();a->start=0;a->len=0;{sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));h->size+=sizeof(mrb_int)*a->cap;sp_gc_bytes+=sizeof(mrb_int)*a->cap;}return a;}
847821 /* Issue #799: clamp e-s+1 against size_t overflow + an arbitrary
@@ -937,7 +911,6 @@
937911 pattern other compare helpers use). */
938912 static mrb_int sp_IntArray_cmp(sp_IntArray*a,sp_IntArray*b){if(!a||!b)return a==b?0:(a?1:-1);mrb_int n=a->len<b->len?a->len:b->len;for(mrb_int i=0;i<n;i++){mrb_int av=a->data[a->start+i],bv=b->data[b->start+i];if(av<bv)return -1;if(av>bv)return 1;}if(a->len<b->len)return -1;if(a->len>b->len)return 1;return 0;}
939913
940-typedef struct{mrb_float*data;mrb_int len;mrb_int cap;mrb_int frozen;}sp_FloatArray;
941914 static void sp_FloatArray_fin(void*p){sp_FloatArray*a=(sp_FloatArray*)p;sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_float)*a->cap;h->size-=sizeof(mrb_float)*a->cap;free(a->data);}
942915 static sp_FloatArray*sp_FloatArray_new(void){sp_FloatArray*a=(sp_FloatArray*)sp_gc_alloc(sizeof(sp_FloatArray),sp_FloatArray_fin,NULL);a->cap=16;a->data=(mrb_float*)malloc(sizeof(mrb_float)*a->cap);if(!a->data)sp_oom_die();a->len=0;{sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));h->size+=sizeof(mrb_float)*a->cap;sp_gc_bytes+=sizeof(mrb_float)*a->cap;}return a;}
943916 static inline void sp_FloatArray_push(sp_FloatArray*a,mrb_float v){if(a->frozen){sp_raise_frozen_array();return;}if(a->len>=a->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_float)*a->cap;h->size-=sizeof(mrb_float)*a->cap;a->cap=a->cap*2+1;a->data=(mrb_float*)realloc(a->data,sizeof(mrb_float)*a->cap);h->size+=sizeof(mrb_float)*a->cap;sp_gc_bytes+=sizeof(mrb_float)*a->cap;}a->data[a->len++]=v;}
@@ -974,7 +947,6 @@
974947 static sp_FloatArray*sp_FloatArray_difference(sp_FloatArray*a,sp_FloatArray*b){sp_FloatArray*r=sp_FloatArray_new();for(mrb_int i=0;i<a->len;i++){mrb_float v=a->data[i];if(!sp_FloatArray_include(b,v))sp_FloatArray_push(r,v);}return r;}
975948
976949 /* ---- PtrArray: array of void* pointers ---- */
977-typedef struct{void**data;mrb_int len;mrb_int cap;void(*scan_elem)(void*);mrb_int frozen;}sp_PtrArray;
978950 static void sp_PtrArray_fin(void*p){sp_PtrArray*a=(sp_PtrArray*)p;sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(void*)*a->cap;h->size-=sizeof(void*)*a->cap;free(a->data);}
979951 static void sp_PtrArray_gc_scan(void*p){sp_PtrArray*a=(sp_PtrArray*)p;if(!a->scan_elem)return;for(mrb_int i=0;i<a->len;i++){if(a->data[i])a->scan_elem(a->data[i]);}}
980952 static sp_PtrArray*sp_PtrArray_new_scan(void(*scan_elem)(void*)){sp_PtrArray*a=(sp_PtrArray*)sp_gc_alloc(sizeof(sp_PtrArray),sp_PtrArray_fin,scan_elem?sp_PtrArray_gc_scan:NULL);a->cap=16;a->data=(void**)malloc(sizeof(void*)*a->cap);if(!a->data)sp_oom_die();a->len=0;a->scan_elem=scan_elem;{sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));h->size+=sizeof(void*)*a->cap;sp_gc_bytes+=sizeof(void*)*a->cap;}return a;}
@@ -1018,8 +990,6 @@
1018990 * inside the struct so empty/short StrArrays skip the data malloc.
1019991 * Common idiom "".split(",") and most class-metadata lists stay small.
1020992 * data == inline_data is the discriminator for "still on inline storage". */
1021-#define SP_STRARR_INLINE 4
1022-typedef struct{const char**data;mrb_int len;mrb_int cap;mrb_int frozen;const char*inline_data[SP_STRARR_INLINE];}sp_StrArray;
1023993 static void sp_StrArray_fin(void*p){sp_StrArray*a=(sp_StrArray*)p;if(a->data!=a->inline_data){sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(const char*)*a->cap;h->size-=sizeof(const char*)*a->cap;free(a->data);}}
1024994 static void sp_StrArray_scan(void*p){sp_StrArray*a=(sp_StrArray*)p;for(mrb_int i=0;i<a->len;i++)sp_mark_string(a->data[i]);}
1025995 static sp_StrArray*sp_StrArray_new(void){sp_StrArray*a=(sp_StrArray*)sp_gc_alloc(sizeof(sp_StrArray),sp_StrArray_fin,sp_StrArray_scan);a->cap=SP_STRARR_INLINE;a->data=a->inline_data;a->len=0;return a;}
@@ -1089,7 +1059,6 @@
10891059 static const char *sp_StrArray_sample(sp_StrArray*a){if(a->len<=0)return sp_str_empty;return a->data[(mrb_int)(rand()%a->len)];}
10901060
10911061 static inline uint64_t sp_str_hash(const char*s){uint64_t h=14695981039346656037ULL;while(*s){h^=(unsigned char)*s++;h*=1099511628211ULL;}return h;}
1092-typedef struct{const char**keys;mrb_int*vals;const char**order;mrb_int len;mrb_int cap;mrb_int mask;mrb_int default_v;}sp_StrIntHash;
10931062 static void sp_StrIntHash_fin(void*p){sp_StrIntHash*h=(sp_StrIntHash*)p;free(h->keys);free(h->vals);free(h->order);}
10941063 static void sp_StrIntHash_scan(void*p){sp_StrIntHash*h=(sp_StrIntHash*)p;for(mrb_int i=0;i<h->cap;i++){if(h->keys[i])sp_mark_string(h->keys[i]);}}
10951064 static sp_StrIntHash*sp_StrIntHash_new(void){sp_StrIntHash*h=(sp_StrIntHash*)sp_gc_alloc(sizeof(sp_StrIntHash),sp_StrIntHash_fin,sp_StrIntHash_scan);h->cap=16;h->mask=15;h->keys=(const char**)calloc(h->cap,sizeof(const char*));h->vals=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->order=(const char**)malloc(sizeof(const char*)*h->cap);h->len=0;h->default_v=0;return h;}
@@ -1110,7 +1079,6 @@
11101079 static sp_StrIntHash*sp_StrIntHash_dup(sp_StrIntHash*h){sp_StrIntHash*r=sp_StrIntHash_new();r->default_v=h->default_v;for(mrb_int i=0;i<h->len;i++)sp_StrIntHash_set(r,h->order[i],sp_StrIntHash_get(h,h->order[i]));return r;}
11111080 static mrb_bool sp_StrIntHash_eq(sp_StrIntHash*a,sp_StrIntHash*b){if(!a||!b)return a==b;if(a->len!=b->len)return FALSE;for(mrb_int i=0;i<a->len;i++){const char*k=a->order[i];if(!sp_StrIntHash_has_key(b,k))return FALSE;if(sp_StrIntHash_get(a,k)!=sp_StrIntHash_get(b,k))return FALSE;}return TRUE;}
11121081
1113-typedef struct{const char**keys;const char**vals;const char**order;mrb_int len;mrb_int cap;mrb_int mask;const char*default_v;}sp_StrStrHash;
11141082 static void sp_StrStrHash_fin(void*p){sp_StrStrHash*h=(sp_StrStrHash*)p;free(h->keys);free(h->vals);free(h->order);}
11151083 static void sp_StrStrHash_scan(void*p){sp_StrStrHash*h=(sp_StrStrHash*)p;for(mrb_int i=0;i<h->cap;i++){if(h->keys[i]){sp_mark_string(h->keys[i]);sp_mark_string(h->vals[i]);}}if(h->default_v)sp_mark_string(h->default_v);}
11161084 static sp_StrStrHash*sp_StrStrHash_new(void){sp_StrStrHash*h=(sp_StrStrHash*)sp_gc_alloc(sizeof(sp_StrStrHash),sp_StrStrHash_fin,sp_StrStrHash_scan);h->cap=16;h->mask=15;h->keys=(const char**)calloc(h->cap,sizeof(const char*));h->vals=(const char**)calloc(h->cap,sizeof(const char*));h->order=(const char**)malloc(sizeof(const char*)*h->cap);h->len=0;h->default_v=NULL;return h;}
@@ -1129,7 +1097,6 @@
11291097 static sp_StrStrHash*sp_StrStrHash_dup(sp_StrStrHash*h){sp_StrStrHash*r=sp_StrStrHash_new();r->default_v=h->default_v;for(mrb_int i=0;i<h->len;i++)sp_StrStrHash_set(r,h->order[i],sp_StrStrHash_get(h,h->order[i]));return r;}
11301098 static mrb_bool sp_StrStrHash_eq(sp_StrStrHash*a,sp_StrStrHash*b){if(!a||!b)return a==b;if(a->len!=b->len)return FALSE;for(mrb_int i=0;i<a->len;i++){const char*k=a->order[i];if(!sp_StrStrHash_has_key(b,k))return FALSE;if(!sp_str_eq(sp_StrStrHash_get(a,k),sp_StrStrHash_get(b,k)))return FALSE;}return TRUE;}
11311099
1132-typedef struct{mrb_int*keys;const char**vals;mrb_int*order;mrb_bool*used;mrb_int len;mrb_int cap;mrb_int mask;const char*default_v;}sp_IntStrHash;
11331100 static void sp_IntStrHash_fin(void*p){sp_IntStrHash*h=(sp_IntStrHash*)p;free(h->keys);free(h->vals);free(h->order);free(h->used);}
11341101 static void sp_IntStrHash_scan(void*p){sp_IntStrHash*h=(sp_IntStrHash*)p;for(mrb_int i=0;i<h->cap;i++)if(h->used[i])sp_mark_string(h->vals[i]);if(h->default_v)sp_mark_string(h->default_v);}
11351102 static sp_IntStrHash*sp_IntStrHash_new(void){sp_IntStrHash*h=(sp_IntStrHash*)sp_gc_alloc(sizeof(sp_IntStrHash),sp_IntStrHash_fin,sp_IntStrHash_scan);h->cap=16;h->mask=15;h->keys=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->vals=(const char**)calloc(h->cap,sizeof(const char*));h->order=(mrb_int*)malloc(sizeof(mrb_int)*h->cap);h->used=(mrb_bool*)calloc(h->cap,sizeof(mrb_bool));h->len=0;h->default_v=NULL;return h;}
@@ -1150,7 +1117,6 @@
11501117 layout (used[] bitmap so 0/-1 keys are distinguishable from
11511118 empty), with int-valued slots. Used by Array#tally on int
11521119 arrays — see #865. */
1153-typedef struct{mrb_int*keys;mrb_int*vals;mrb_int*order;mrb_bool*used;mrb_int len;mrb_int cap;mrb_int mask;mrb_int default_v;}sp_IntIntHash;
11541120 static void sp_IntIntHash_fin(void*p){sp_IntIntHash*h=(sp_IntIntHash*)p;free(h->keys);free(h->vals);free(h->order);free(h->used);}
11551121 static sp_IntIntHash*sp_IntIntHash_new(void){sp_IntIntHash*h=(sp_IntIntHash*)sp_gc_alloc(sizeof(sp_IntIntHash),sp_IntIntHash_fin,NULL);h->cap=16;h->mask=15;h->keys=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->vals=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->order=(mrb_int*)malloc(sizeof(mrb_int)*h->cap);h->used=(mrb_bool*)calloc(h->cap,sizeof(mrb_bool));h->len=0;h->default_v=0;return h;}
11561122 static sp_IntIntHash*sp_IntIntHash_new_with_default(mrb_int d){sp_IntIntHash*h=sp_IntIntHash_new();h->default_v=d;return h;}