mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Refactor macros of array.c
Move some macros in array.c to internal/array.h so that other files can also access these macros.
This commit is contained in:
parent
4798a4fec2
commit
1c9acb6bb1
Notes:
git
2022-07-21 22:03:08 +09:00
5 changed files with 73 additions and 56 deletions
42
array.c
42
array.c
|
@ -53,23 +53,6 @@ should_be_T_ARRAY(VALUE ary)
|
|||
return RB_TYPE_P(ary, T_ARRAY);
|
||||
}
|
||||
|
||||
RBIMPL_ATTR_MAYBE_UNUSED()
|
||||
static int
|
||||
should_not_be_shared_and_embedded(VALUE ary)
|
||||
{
|
||||
return !FL_TEST((ary), ELTS_SHARED) || !FL_TEST((ary), RARRAY_EMBED_FLAG);
|
||||
}
|
||||
|
||||
#define ARY_SHARED_P(ary) \
|
||||
(assert(should_be_T_ARRAY((VALUE)(ary))), \
|
||||
assert(should_not_be_shared_and_embedded((VALUE)ary)), \
|
||||
FL_TEST_RAW((ary),ELTS_SHARED)!=0)
|
||||
|
||||
#define ARY_EMBED_P(ary) \
|
||||
(assert(should_be_T_ARRAY((VALUE)(ary))), \
|
||||
assert(should_not_be_shared_and_embedded((VALUE)ary)), \
|
||||
FL_TEST_RAW((ary), RARRAY_EMBED_FLAG) != 0)
|
||||
|
||||
#define ARY_HEAP_PTR(a) (assert(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.ptr)
|
||||
#define ARY_HEAP_LEN(a) (assert(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.len)
|
||||
#define ARY_HEAP_CAPA(a) (assert(!ARY_EMBED_P(a)), assert(!ARY_SHARED_ROOT_P(a)), \
|
||||
|
@ -147,7 +130,6 @@ should_not_be_shared_and_embedded(VALUE ary)
|
|||
RARRAY(ary)->as.heap.aux.capa = (n); \
|
||||
} while (0)
|
||||
|
||||
#define ARY_SHARED_ROOT(ary) (assert(ARY_SHARED_P(ary)), RARRAY(ary)->as.heap.aux.shared_root)
|
||||
#define ARY_SET_SHARED(ary, value) do { \
|
||||
const VALUE _ary_ = (ary); \
|
||||
const VALUE _value_ = (value); \
|
||||
|
@ -158,11 +140,6 @@ should_not_be_shared_and_embedded(VALUE ary)
|
|||
RB_OBJ_WRITE(_ary_, &RARRAY(_ary_)->as.heap.aux.shared_root, _value_); \
|
||||
} while (0)
|
||||
|
||||
#define RARRAY_SHARED_ROOT_FLAG FL_USER12
|
||||
#define ARY_SHARED_ROOT_P(ary) (assert(should_be_T_ARRAY((VALUE)(ary))), \
|
||||
FL_TEST_RAW((ary), RARRAY_SHARED_ROOT_FLAG))
|
||||
#define ARY_SHARED_ROOT_REFCNT(ary) \
|
||||
(assert(ARY_SHARED_ROOT_P(ary)), RARRAY(ary)->as.heap.aux.capa)
|
||||
#define ARY_SHARED_ROOT_OCCUPIED(ary) (!ARY_LITERAL_P(ary) && ARY_SHARED_ROOT_REFCNT(ary) == 1)
|
||||
#define ARY_SET_SHARED_ROOT_REFCNT(ary, value) do { \
|
||||
assert(ARY_SHARED_ROOT_P(ary)); \
|
||||
|
@ -175,11 +152,6 @@ should_not_be_shared_and_embedded(VALUE ary)
|
|||
FL_SET((ary), RARRAY_SHARED_ROOT_FLAG); \
|
||||
} while (0)
|
||||
|
||||
#define RARRAY_LITERAL_FLAG FL_USER15
|
||||
#define ARY_LITERAL_P(ary) \
|
||||
(assert(should_be_T_ARRAY((VALUE)(ary))), \
|
||||
FL_TEST_RAW((ary), RARRAY_LITERAL_FLAG))
|
||||
|
||||
static inline void
|
||||
ARY_SET(VALUE a, long i, VALUE v)
|
||||
{
|
||||
|
@ -251,8 +223,8 @@ ary_verify_(VALUE ary, const char *file, int line)
|
|||
{
|
||||
assert(RB_TYPE_P(ary, T_ARRAY));
|
||||
|
||||
if (FL_TEST(ary, ELTS_SHARED)) {
|
||||
VALUE root = RARRAY(ary)->as.heap.aux.shared_root;
|
||||
if (ARY_SHARED_P(ary)) {
|
||||
VALUE root = ARY_SHARED_ROOT(ary);
|
||||
const VALUE *ptr = ARY_HEAP_PTR(ary);
|
||||
const VALUE *root_ptr = RARRAY_CONST_PTR_TRANSIENT(root);
|
||||
long len = ARY_HEAP_LEN(ary), root_len = RARRAY_LEN(root);
|
||||
|
@ -597,7 +569,7 @@ rb_ary_decrement_share(VALUE shared_root)
|
|||
static void
|
||||
rb_ary_unshare(VALUE ary)
|
||||
{
|
||||
VALUE shared_root = RARRAY(ary)->as.heap.aux.shared_root;
|
||||
VALUE shared_root = ARY_SHARED_ROOT(ary);
|
||||
rb_ary_decrement_share(shared_root);
|
||||
FL_UNSET_SHARED(ary);
|
||||
}
|
||||
|
@ -770,10 +742,10 @@ VALUE
|
|||
rb_ary_shared_with_p(VALUE ary1, VALUE ary2)
|
||||
{
|
||||
if (!ARY_EMBED_P(ary1) && ARY_SHARED_P(ary1) &&
|
||||
!ARY_EMBED_P(ary2) && ARY_SHARED_P(ary2) &&
|
||||
RARRAY(ary1)->as.heap.aux.shared_root == RARRAY(ary2)->as.heap.aux.shared_root &&
|
||||
RARRAY(ary1)->as.heap.len == RARRAY(ary2)->as.heap.len) {
|
||||
return Qtrue;
|
||||
!ARY_EMBED_P(ary2) && ARY_SHARED_P(ary2) &&
|
||||
ARY_SHARED_ROOT(ary1) == ARY_SHARED_ROOT(ary2) &&
|
||||
ARY_HEAP_LEN(ary1) == ARY_HEAP_LEN(ary2)) {
|
||||
return Qtrue;
|
||||
}
|
||||
return Qfalse;
|
||||
}
|
||||
|
|
|
@ -15797,6 +15797,7 @@ transcode.$(OBJEXT): {$(VPATH)}subst.h
|
|||
transcode.$(OBJEXT): {$(VPATH)}transcode.c
|
||||
transcode.$(OBJEXT): {$(VPATH)}transcode_data.h
|
||||
transient_heap.$(OBJEXT): $(hdrdir)/ruby/ruby.h
|
||||
transient_heap.$(OBJEXT): $(top_srcdir)/internal/array.h
|
||||
transient_heap.$(OBJEXT): $(top_srcdir)/internal/compilers.h
|
||||
transient_heap.$(OBJEXT): $(top_srcdir)/internal/gc.h
|
||||
transient_heap.$(OBJEXT): $(top_srcdir)/internal/hash.h
|
||||
|
|
28
gc.c
28
gc.c
|
@ -7205,10 +7205,10 @@ gc_mark_children(rb_objspace_t *objspace, VALUE obj)
|
|||
break;
|
||||
|
||||
case T_ARRAY:
|
||||
if (FL_TEST(obj, ELTS_SHARED)) {
|
||||
VALUE root = any->as.array.as.heap.aux.shared_root;
|
||||
if (ARY_SHARED_P(obj)) {
|
||||
VALUE root = ARY_SHARED_ROOT(obj);
|
||||
gc_mark(objspace, root);
|
||||
}
|
||||
}
|
||||
else {
|
||||
long i, len = RARRAY_LEN(obj);
|
||||
const VALUE *ptr = RARRAY_CONST_PTR_TRANSIENT(obj);
|
||||
|
@ -7217,8 +7217,7 @@ gc_mark_children(rb_objspace_t *objspace, VALUE obj)
|
|||
}
|
||||
|
||||
if (LIKELY(during_gc)) {
|
||||
if (!FL_TEST_RAW(obj, RARRAY_EMBED_FLAG) &&
|
||||
RARRAY_TRANSIENT_P(obj)) {
|
||||
if (!ARY_EMBED_P(obj) && RARRAY_TRANSIENT_P(obj)) {
|
||||
rb_transient_heap_mark(obj, ptr);
|
||||
}
|
||||
}
|
||||
|
@ -9927,8 +9926,7 @@ gc_ref_update_array(rb_objspace_t * objspace, VALUE v)
|
|||
{
|
||||
long i, len;
|
||||
|
||||
if (FL_TEST(v, ELTS_SHARED))
|
||||
return;
|
||||
if (ARY_SHARED_P(v)) return;
|
||||
|
||||
len = RARRAY_LEN(v);
|
||||
if (len > 0) {
|
||||
|
@ -10435,7 +10433,7 @@ gc_update_object_references(rb_objspace_t *objspace, VALUE obj)
|
|||
return;
|
||||
|
||||
case T_ARRAY:
|
||||
if (FL_TEST(obj, ELTS_SHARED)) {
|
||||
if (ARY_SHARED_P(obj)) {
|
||||
UPDATE_IF_MOVED(objspace, any->as.array.as.heap.aux.shared_root);
|
||||
}
|
||||
else {
|
||||
|
@ -13750,14 +13748,6 @@ rb_method_type_name(rb_method_type_t type)
|
|||
rb_bug("rb_method_type_name: unreachable (type: %d)", type);
|
||||
}
|
||||
|
||||
/* from array.c */
|
||||
# define ARY_SHARED_P(ary) \
|
||||
(GC_ASSERT(!FL_TEST((ary), ELTS_SHARED) || !FL_TEST((ary), RARRAY_EMBED_FLAG)), \
|
||||
FL_TEST((ary),ELTS_SHARED)!=0)
|
||||
# define ARY_EMBED_P(ary) \
|
||||
(GC_ASSERT(!FL_TEST((ary), ELTS_SHARED) || !FL_TEST((ary), RARRAY_EMBED_FLAG)), \
|
||||
FL_TEST((ary), RARRAY_EMBED_FLAG)!=0)
|
||||
|
||||
static void
|
||||
rb_raw_iseq_info(char *const buff, const size_t buff_size, const rb_iseq_t *iseq)
|
||||
{
|
||||
|
@ -13862,11 +13852,11 @@ rb_raw_obj_info_buitin_type(char *const buff, const size_t buff_size, const VALU
|
|||
UNEXPECTED_NODE(rb_raw_obj_info);
|
||||
break;
|
||||
case T_ARRAY:
|
||||
if (FL_TEST(obj, ELTS_SHARED)) {
|
||||
if (ARY_SHARED_P(obj)) {
|
||||
APPEND_S("shared -> ");
|
||||
rb_raw_obj_info(BUFF_ARGS, RARRAY(obj)->as.heap.aux.shared_root);
|
||||
rb_raw_obj_info(BUFF_ARGS, ARY_SHARED_ROOT(obj));
|
||||
}
|
||||
else if (FL_TEST(obj, RARRAY_EMBED_FLAG)) {
|
||||
else if (ARY_EMBED_P(obj)) {
|
||||
APPEND_F("[%s%s] len: %ld (embed)",
|
||||
C(ARY_EMBED_P(obj), "E"),
|
||||
C(ARY_SHARED_P(obj), "S"),
|
||||
|
|
|
@ -18,7 +18,9 @@
|
|||
# define ARRAY_DEBUG (0+RUBY_DEBUG)
|
||||
#endif
|
||||
|
||||
#define RARRAY_PTR_IN_USE_FLAG FL_USER14
|
||||
#define RARRAY_SHARED_ROOT_FLAG FL_USER12
|
||||
#define RARRAY_PTR_IN_USE_FLAG FL_USER14
|
||||
#define RARRAY_LITERAL_FLAG FL_USER15
|
||||
|
||||
/* array.c */
|
||||
VALUE rb_ary_last(int, const VALUE *, VALUE);
|
||||
|
@ -73,6 +75,57 @@ ARY_PTR_USING_P(VALUE ary)
|
|||
return FL_TEST_RAW(ary, RARRAY_PTR_IN_USE_FLAG);
|
||||
}
|
||||
|
||||
RBIMPL_ATTR_MAYBE_UNUSED()
|
||||
static inline int
|
||||
ary_should_not_be_shared_and_embedded(VALUE ary)
|
||||
{
|
||||
return !FL_ALL_RAW(ary, ELTS_SHARED|RARRAY_EMBED_FLAG);
|
||||
}
|
||||
|
||||
static inline bool
|
||||
ARY_SHARED_P(VALUE ary)
|
||||
{
|
||||
assert(RB_TYPE_P(ary, T_ARRAY));
|
||||
assert(ary_should_not_be_shared_and_embedded(ary));
|
||||
return FL_TEST_RAW(ary, ELTS_SHARED);
|
||||
}
|
||||
|
||||
static inline bool
|
||||
ARY_EMBED_P(VALUE ary)
|
||||
{
|
||||
assert(RB_TYPE_P(ary, T_ARRAY));
|
||||
assert(ary_should_not_be_shared_and_embedded(ary));
|
||||
return FL_TEST_RAW(ary, RARRAY_EMBED_FLAG);
|
||||
}
|
||||
|
||||
static inline VALUE
|
||||
ARY_SHARED_ROOT(VALUE ary)
|
||||
{
|
||||
assert(ARY_SHARED_P(ary));
|
||||
return RARRAY(ary)->as.heap.aux.shared_root;
|
||||
}
|
||||
|
||||
static inline bool
|
||||
ARY_SHARED_ROOT_P(VALUE ary)
|
||||
{
|
||||
assert(RB_TYPE_P(ary, T_ARRAY));
|
||||
return FL_TEST_RAW(ary, RARRAY_SHARED_ROOT_FLAG);
|
||||
}
|
||||
|
||||
static inline long
|
||||
ARY_SHARED_ROOT_REFCNT(VALUE ary)
|
||||
{
|
||||
assert(ARY_SHARED_ROOT_P(ary));
|
||||
return RARRAY(ary)->as.heap.aux.capa;
|
||||
}
|
||||
|
||||
static inline bool
|
||||
ARY_LITERAL_P(VALUE ary)
|
||||
{
|
||||
assert(RB_TYPE_P(ary, T_ARRAY));
|
||||
return FL_TEST_RAW(ary, RARRAY_LITERAL_FLAG);
|
||||
}
|
||||
|
||||
static inline void
|
||||
RARY_TRANSIENT_SET(VALUE ary)
|
||||
{
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include "debug_counter.h"
|
||||
#include "gc.h"
|
||||
#include "internal.h"
|
||||
#include "internal/array.h"
|
||||
#include "internal/gc.h"
|
||||
#include "internal/hash.h"
|
||||
#include "internal/sanitizers.h"
|
||||
|
@ -592,7 +593,7 @@ transient_heap_ptr(VALUE obj, int error)
|
|||
switch (BUILTIN_TYPE(obj)) {
|
||||
case T_ARRAY:
|
||||
if (RARRAY_TRANSIENT_P(obj)) {
|
||||
TH_ASSERT(!FL_TEST_RAW(obj, RARRAY_EMBED_FLAG));
|
||||
TH_ASSERT(!ARY_EMBED_P(obj));
|
||||
ptr = RARRAY(obj)->as.heap.ptr;
|
||||
}
|
||||
break;
|
||||
|
|
Loading…
Add table
Reference in a new issue