diff options
author | 卜部昌平 <shyouhei@ruby-lang.org> | 2019-12-03 13:18:53 +0900 |
---|---|---|
committer | 卜部昌平 <shyouhei@ruby-lang.org> | 2019-12-26 20:45:12 +0900 |
commit | 7d71d916a225c87ab15d62512f0e9cebad16832f (patch) | |
tree | 5e0862f40af94a644d5c6991c895b79acf53f010 /internal | |
parent | e72b8592d9b4daa079d85d0ceb60f466edaec94d (diff) | |
download | ruby-7d71d916a225c87ab15d62512f0e9cebad16832f.tar.gz |
internal/struct.h rework
Replace macros with inline functions of equivalent contents, for much
improved readability.
Diffstat (limited to 'internal')
-rw-r--r-- | internal/struct.h | 114 |
1 files changed, 82 insertions, 32 deletions
diff --git a/internal/struct.h b/internal/struct.h index f63b45007e..b0f7f2eddd 100644 --- a/internal/struct.h +++ b/internal/struct.h @@ -9,30 +9,17 @@ * modify this file, provided that the conditions mentioned in the * file COPYING are met. Consult the file for details. */ - -#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 +#include "internal/gc.h" /* for RB_OBJ_WRITE */ +#include "internal/stdbool.h" /* for bool */ +#include "ruby/ruby.h" /* for struct RBasic */ enum { RSTRUCT_EMBED_LEN_MAX = RVALUE_EMBED_LEN_MAX, RSTRUCT_EMBED_LEN_MASK = (RUBY_FL_USER2|RUBY_FL_USER1), RSTRUCT_EMBED_LEN_SHIFT = (RUBY_FL_USHIFT+1), RSTRUCT_TRANSIENT_FLAG = FL_USER3, - - RSTRUCT_ENUM_END }; -#if USE_TRANSIENT_HEAP -#define RSTRUCT_TRANSIENT_P(st) FL_TEST_RAW((obj), RSTRUCT_TRANSIENT_FLAG) -#define RSTRUCT_TRANSIENT_SET(st) FL_SET_RAW((st), RSTRUCT_TRANSIENT_FLAG) -#define RSTRUCT_TRANSIENT_UNSET(st) FL_UNSET_RAW((st), RSTRUCT_TRANSIENT_FLAG) -#else -#define RSTRUCT_TRANSIENT_P(st) 0 -#define RSTRUCT_TRANSIENT_SET(st) ((void)0) -#define RSTRUCT_TRANSIENT_UNSET(st) ((void)0) -#endif - struct RStruct { struct RBasic basic; union { @@ -44,38 +31,101 @@ struct RStruct { } as; }; +#define RSTRUCT(obj) (R_CAST(RStruct)(obj)) #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)) /* struct.c */ VALUE rb_struct_init_copy(VALUE copy, VALUE s); VALUE rb_struct_lookup(VALUE s, VALUE idx); VALUE rb_struct_s_keyword_init(VALUE klass); +static inline const VALUE *rb_struct_const_heap_ptr(VALUE st); +static inline bool RSTRUCT_TRANSIENT_P(VALUE st); +static inline void RSTRUCT_TRANSIENT_SET(VALUE st); +static inline void RSTRUCT_TRANSIENT_UNSET(VALUE st); +static inline long RSTRUCT_EMBED_LEN(VALUE st); +static inline long RSTRUCT_LEN(VALUE st); +static inline int RSTRUCT_LENINT(VALUE st); +static inline const VALUE *RSTRUCT_CONST_PTR(VALUE st); +static inline void RSTRUCT_SET(VALUE st, long k, VALUE v); +static inline VALUE RSTRUCT_GET(VALUE st, long k); + +static inline bool +RSTRUCT_TRANSIENT_P(VALUE st) +{ +#if USE_TRANSIENT_HEAP + return FL_TEST_RAW(st, RSTRUCT_TRANSIENT_FLAG); +#else + return false; +#endif +} + +static inline void +RSTRUCT_TRANSIENT_SET(VALUE st) +{ +#if USE_TRANSIENT_HEAP + FL_SET_RAW(st, RSTRUCT_TRANSIENT_FLAG); +#endif +} + +static inline void +RSTRUCT_TRANSIENT_UNSET(VALUE st) +{ +#if USE_TRANSIENT_HEAP + FL_UNSET_RAW(st, RSTRUCT_TRANSIENT_FLAG); +#endif +} static inline long -rb_struct_len(VALUE st) +RSTRUCT_EMBED_LEN(VALUE st) { - return (RBASIC(st)->flags & RSTRUCT_EMBED_LEN_MASK) ? - RSTRUCT_EMBED_LEN(st) : RSTRUCT(st)->as.heap.len; + long ret = FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK); + ret >>= RSTRUCT_EMBED_LEN_SHIFT; + return ret; +} + +static inline long +RSTRUCT_LEN(VALUE st) +{ + if (FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK)) { + return RSTRUCT_EMBED_LEN(st); + } + else { + return RSTRUCT(st)->as.heap.len; + } +} + +static inline int +RSTRUCT_LENINT(VALUE st) +{ + return rb_long2int(RSTRUCT_LEN(st)); } static inline const VALUE * -rb_struct_const_ptr(VALUE st) +RSTRUCT_CONST_PTR(VALUE st) +{ + const struct RStruct *p = RSTRUCT(st); + + if (FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK)) { + return p->as.ary; + } + else { + return p->as.heap.ptr; + } +} + +static inline void +RSTRUCT_SET(VALUE st, long k, VALUE v) +{ + RB_OBJ_WRITE(st, &RSTRUCT_CONST_PTR(st)[k], v); +} + +static inline VALUE +RSTRUCT_GET(VALUE st, long k) { - return FIX_CONST_VALUE_PTR((RBASIC(st)->flags & RSTRUCT_EMBED_LEN_MASK) ? - RSTRUCT(st)->as.ary : RSTRUCT(st)->as.heap.ptr); + return RSTRUCT_CONST_PTR(st)[k]; } static inline const VALUE * |