diff options
Diffstat (limited to 'internal.h')
-rw-r--r-- | internal.h | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/internal.h b/internal.h index d92c0fc374..b013f84616 100644 --- a/internal.h +++ b/internal.h @@ -576,6 +576,57 @@ struct RHash { extern void ruby_init_setproctitle(int argc, char *argv[]); #endif +#define RSTRUCT_EMBED_LEN_MAX RSTRUCT_EMBED_LEN_MAX +#define RSTRUCT_EMBED_LEN_MASK RSTRUCT_EMBED_LEN_MASK +#define RSTRUCT_EMBED_LEN_SHIFT RSTRUCT_EMBED_LEN_SHIFT +enum { + RSTRUCT_EMBED_LEN_MAX = 3, + RSTRUCT_EMBED_LEN_MASK = (RUBY_FL_USER2|RUBY_FL_USER1), + RSTRUCT_EMBED_LEN_SHIFT = (RUBY_FL_USHIFT+1), + + RSTRUCT_ENUM_END +}; + +struct RStruct { + struct RBasic basic; + union { + struct { + long len; + const VALUE *ptr; + } heap; + const VALUE ary[RSTRUCT_EMBED_LEN_MAX]; + } as; +}; + +#undef RSTRUCT_LEN +#undef RSTRUCT_PTR +#undef RSTRUCT_SET +#undef RSTRUCT_GET +#define RSTRUCT_EMBED_LEN(st) \ + (long)((RBASIC(st)->flags >> RSTRUCT_EMBED_LEN_SHIFT) & \ + (RSTRUCT_EMBED_LEN_MASK >> RSTRUCT_EMBED_LEN_SHIFT)) +#define RSTRUCT_LEN(st) rb_struct_len(st) +#define RSTRUCT_LENINT(st) rb_long2int(RSTRUCT_LEN(st)) +#define RSTRUCT_CONST_PTR(st) rb_struct_const_ptr(st) +#define RSTRUCT_PTR(st) ((VALUE *)RSTRUCT_CONST_PTR(RB_OBJ_WB_UNPROTECT_FOR(STRUCT, st))) +#define RSTRUCT_SET(st, idx, v) RB_OBJ_WRITE(st, &RSTRUCT_CONST_PTR(st)[idx], (v)) +#define RSTRUCT_GET(st, idx) (RSTRUCT_CONST_PTR(st)[idx]) +#define RSTRUCT(obj) (R_CAST(RStruct)(obj)) + +static inline long +rb_struct_len(VALUE st) +{ + return (RBASIC(st)->flags & RSTRUCT_EMBED_LEN_MASK) ? + RSTRUCT_EMBED_LEN(st) : RSTRUCT(st)->as.heap.len; +} + +static inline const VALUE * +rb_struct_const_ptr(VALUE st) +{ + return FIX_CONST_VALUE_PTR((RBASIC(st)->flags & RSTRUCT_EMBED_LEN_MASK) ? + RSTRUCT(st)->as.ary : RSTRUCT(st)->as.heap.ptr); +} + /* class.c */ struct rb_deprecated_classext_struct { |