[前][次][番号順一覧][スレッド一覧]

ruby-changes:59498

From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Fri, 27 Dec 2019 00:23:36 +0900 (JST)
Subject: [ruby-changes:59498] 7d71d916a2 (master): internal/struct.h rework

https://git.ruby-lang.org/ruby.git/commit/?id=7d71d916a2

From 7d71d916a225c87ab15d62512f0e9cebad16832f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?=
 <shyouhei@r...>
Date: Tue, 3 Dec 2019 13:18:53 +0900
Subject: internal/struct.h rework

Replace macros with inline functions of equivalent contents, for much
improved readability.

diff --git a/internal/struct.h b/internal/struct.h
index f63b450..b0f7f2e 100644
--- a/internal/struct.h
+++ b/internal/struct.h
@@ -9,30 +9,17 @@ https://github.com/ruby/ruby/blob/trunk/internal/struct.h#L9
  *             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 { https://github.com/ruby/ruby/blob/trunk/internal/struct.h#L31
     } 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 *
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]