1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

internal/struct.h rework

Replace macros with inline functions of equivalent contents, for much
improved readability.
This commit is contained in:
卜部昌平 2019-12-03 13:18:53 +09:00
parent e72b8592d9
commit 7d71d916a2
Notes: git 2019-12-26 20:46:04 +09:00

View file

@ -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)
{
return FIX_CONST_VALUE_PTR((RBASIC(st)->flags & RSTRUCT_EMBED_LEN_MASK) ?
RSTRUCT(st)->as.ary : RSTRUCT(st)->as.heap.ptr);
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 RSTRUCT_CONST_PTR(st)[k];
}
static inline const VALUE *