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

ruby-changes:72487

From: Nobuyoshi <ko1@a...>
Date: Sun, 10 Jul 2022 16:38:41 +0900 (JST)
Subject: [ruby-changes:72487] 2bf0313561 (master): Rewrite `APPENDF` using variadic arguments

https://git.ruby-lang.org/ruby.git/commit/?id=2bf0313561

From 2bf0313561304506cf3cceba6ef4c66a6f2c09b0 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Tue, 5 Jul 2022 22:55:27 +0900
Subject: Rewrite `APPENDF` using variadic arguments

---
 gc.c | 84 ++++++++++++++++++++++++++++++++++----------------------------------
 1 file changed, 42 insertions(+), 42 deletions(-)

diff --git a/gc.c b/gc.c
index 3425061a64..f0ea1e1dd1 100644
--- a/gc.c
+++ b/gc.c
@@ -13678,15 +13678,15 @@ rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj) https://github.com/ruby/ruby/blob/trunk/gc.c#L13678
     void *poisoned = asan_unpoison_object_temporary(obj);
 
 #define BUFF_ARGS buff + pos, buff_size - pos
-#define APPENDF(f) if ((pos += snprintf f) >= buff_size) goto end
+#define APPEND_F(...) if ((pos += snprintf(BUFF_ARGS, "" __VA_ARGS__)) >= buff_size) goto end
     if (SPECIAL_CONST_P(obj)) {
-        APPENDF((BUFF_ARGS, "%s", obj_type_name(obj)));
+        APPEND_F("%s", obj_type_name(obj));
 
         if (FIXNUM_P(obj)) {
-            APPENDF((BUFF_ARGS, " %ld", FIX2LONG(obj)));
+            APPEND_F(" %ld", FIX2LONG(obj));
         }
         else if (SYMBOL_P(obj)) {
-            APPENDF((BUFF_ARGS, " %s", rb_id2name(SYM2ID(obj))));
+            APPEND_F(" %s", rb_id2name(SYM2ID(obj)));
         }
     }
     else {
@@ -13696,7 +13696,7 @@ rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj) https://github.com/ruby/ruby/blob/trunk/gc.c#L13696
 	const int age = RVALUE_FLAGS_AGE(RBASIC(obj)->flags);
 
         if (is_pointer_to_heap(&rb_objspace, (void *)obj)) {
-            APPENDF((BUFF_ARGS, "%p [%d%s%s%s%s%s%s] %s ",
+            APPEND_F("%p [%d%s%s%s%s%s%s] %s ",
                      (void *)obj, age,
                      C(RVALUE_UNCOLLECTIBLE_BITMAP(obj),  "L"),
                      C(RVALUE_MARK_BITMAP(obj),           "M"),
@@ -13704,30 +13704,30 @@ rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj) https://github.com/ruby/ruby/blob/trunk/gc.c#L13704
                      C(RVALUE_MARKING_BITMAP(obj),        "R"),
                      C(RVALUE_WB_UNPROTECTED_BITMAP(obj), "U"),
                      C(rb_objspace_garbage_object_p(obj), "G"),
-                     obj_type_name(obj)));
+                     obj_type_name(obj));
         }
         else {
             /* fake */
-            APPENDF((BUFF_ARGS, "%p [%dXXXX] %s",
+            APPEND_F("%p [%dXXXX] %s",
                      (void *)obj, age,
-                     obj_type_name(obj)));
+                     obj_type_name(obj));
         }
 
 	if (internal_object_p(obj)) {
 	    /* ignore */
 	}
 	else if (RBASIC(obj)->klass == 0) {
-            APPENDF((BUFF_ARGS, "(temporary internal)"));
+            APPEND_F("(temporary internal)");
 	}
 	else if (RTEST(RBASIC(obj)->klass)) {
             VALUE class_path = rb_class_path_cached(RBASIC(obj)->klass);
 	    if (!NIL_P(class_path)) {
-                APPENDF((BUFF_ARGS, "(%s)", RSTRING_PTR(class_path)));
+                APPEND_F("(%s)", RSTRING_PTR(class_path));
 	    }
 	}
 
 #if GC_DEBUG
-        APPENDF((BUFF_ARGS, "@%s:%d", RANY(obj)->file, RANY(obj)->line));
+        APPEND_F("@%s:%d", RANY(obj)->file, RANY(obj)->line);
 #endif
 
 	switch (type) {
@@ -13736,57 +13736,57 @@ rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj) https://github.com/ruby/ruby/blob/trunk/gc.c#L13736
 	    break;
 	  case T_ARRAY:
             if (FL_TEST(obj, ELTS_SHARED)) {
-                APPENDF((BUFF_ARGS, "shared -> "));
+                APPEND_F("shared -> ");
                 rb_raw_obj_info(BUFF_ARGS, RARRAY(obj)->as.heap.aux.shared_root);
             }
             else if (FL_TEST(obj, RARRAY_EMBED_FLAG)) {
-                APPENDF((BUFF_ARGS, "[%s%s] len: %ld (embed)",
+                APPEND_F("[%s%s] len: %ld (embed)",
                          C(ARY_EMBED_P(obj),  "E"),
                          C(ARY_SHARED_P(obj), "S"),
-                         RARRAY_LEN(obj)));
+                         RARRAY_LEN(obj));
             }
             else {
-                APPENDF((BUFF_ARGS, "[%s%s%s] len: %ld, capa:%ld ptr:%p",
+                APPEND_F("[%s%s%s] len: %ld, capa:%ld ptr:%p",
                          C(ARY_EMBED_P(obj),  "E"),
                          C(ARY_SHARED_P(obj), "S"),
                          C(RARRAY_TRANSIENT_P(obj), "T"),
                          RARRAY_LEN(obj),
                          ARY_EMBED_P(obj) ? -1L : RARRAY(obj)->as.heap.aux.capa,
-                         (void *)RARRAY_CONST_PTR_TRANSIENT(obj)));
+                         (void *)RARRAY_CONST_PTR_TRANSIENT(obj));
             }
 	    break;
 	  case T_STRING: {
             if (STR_SHARED_P(obj)) {
-                APPENDF((BUFF_ARGS, " [shared] len: %ld", RSTRING_LEN(obj)));
+                APPEND_F(" [shared] len: %ld", RSTRING_LEN(obj));
             }
             else {
-                if (STR_EMBED_P(obj)) APPENDF((BUFF_ARGS, " [embed]"));
+                if (STR_EMBED_P(obj)) APPEND_F(" [embed]");
 
-                APPENDF((BUFF_ARGS, " len: %ld, capa: %" PRIdSIZE, RSTRING_LEN(obj), rb_str_capacity(obj)));
+                APPEND_F(" len: %ld, capa: %" PRIdSIZE, RSTRING_LEN(obj), rb_str_capacity(obj));
             }
-            APPENDF((BUFF_ARGS, " \"%.*s\"", str_len_no_raise(obj), RSTRING_PTR(obj)));
+            APPEND_F(" \"%.*s\"", str_len_no_raise(obj), RSTRING_PTR(obj));
 	    break;
 	  }
           case T_SYMBOL: {
             VALUE fstr = RSYMBOL(obj)->fstr;
             ID id = RSYMBOL(obj)->id;
             if (RB_TYPE_P(fstr, T_STRING)) {
-                APPENDF((BUFF_ARGS, ":%s id:%d", RSTRING_PTR(fstr), (unsigned int)id));
+                APPEND_F(":%s id:%d", RSTRING_PTR(fstr), (unsigned int)id);
             }
             else {
-                APPENDF((BUFF_ARGS, "(%p) id:%d", (void *)fstr, (unsigned int)id));
+                APPEND_F("(%p) id:%d", (void *)fstr, (unsigned int)id);
             }
             break;
           }
           case T_MOVED: {
-            APPENDF((BUFF_ARGS, "-> %p", (void*)rb_gc_location(obj)));
+            APPEND_F("-> %p", (void*)rb_gc_location(obj));
             break;
           }
           case T_HASH: {
-            APPENDF((BUFF_ARGS, "[%c%c] %"PRIdSIZE,
+            APPEND_F("[%c%c] %"PRIdSIZE,
                      RHASH_AR_TABLE_P(obj) ? 'A' : 'S',
                      RHASH_TRANSIENT_P(obj) ? 'T' : ' ',
-                     RHASH_SIZE(obj)));
+                     RHASH_SIZE(obj));
             break;
           }
           case T_CLASS:
@@ -13794,10 +13794,10 @@ rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj) https://github.com/ruby/ruby/blob/trunk/gc.c#L13794
             {
                 VALUE class_path = rb_class_path_cached(obj);
                 if (!NIL_P(class_path)) {
-                    APPENDF((BUFF_ARGS, "%s", RSTRING_PTR(class_path)));
+                    APPEND_F("%s", RSTRING_PTR(class_path));
                 }
                 else {
-                    APPENDF((BUFF_ARGS, "(annon)"));
+                    APPEND_F("(annon)");
                 }
                 break;
             }
@@ -13805,7 +13805,7 @@ rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj) https://github.com/ruby/ruby/blob/trunk/gc.c#L13805
             {
                 VALUE class_path = rb_class_path_cached(RBASIC_CLASS(obj));
                 if (!NIL_P(class_path)) {
-                    APPENDF((BUFF_ARGS, "src:%s", RSTRING_PTR(class_path)));
+                    APPEND_F("src:%s", RSTRING_PTR(class_path));
                 }
                 break;
             }
@@ -13814,11 +13814,11 @@ rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj) https://github.com/ruby/ruby/blob/trunk/gc.c#L13814
                 uint32_t len = ROBJECT_NUMIV(obj);
 
                 if (RANY(obj)->as.basic.flags & ROBJECT_EMBED) {
-                    APPENDF((BUFF_ARGS, "(embed) len:%d", len));
+                    APPEND_F("(embed) len:%d", len);
                 }
                 else {
                     VALUE *ptr = ROBJECT_IVPTR(obj);
-                    APPENDF((BUFF_ARGS, "len:%d ptr:%p", len, (void *)ptr));
+                    APPEND_F("len:%d ptr:%p", len, (void *)ptr);
                 }
             }
             break;
@@ -13834,26 +13834,26 @@ rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj) https://github.com/ruby/ruby/blob/trunk/gc.c#L13834
             else if (rb_ractor_p(obj)) {
                 rb_ractor_t *r = (void *)DATA_PTR(obj);
                 if (r) {
-                    APPENDF((BUFF_ARGS, "r:%d", r->pub.id));
+                    APPEND_F("r:%d", r->pub.id);
                 }
             }
 	    else {
 		const char * const type_name = rb_objspace_data_type_name(obj);
 		if (type_name) {
-                    APPENDF((BUFF_ARGS, "%s", type_name));
+                    APPEND_F("%s", type_name);
 		}
 	    }
 	    break;
 	  }
 	  case T_IMEMO: {
-            APPENDF((BUFF_ARGS, "<%s> ", rb_imemo_name(imemo_type(obj))));
+            APPEND_F("<%s> ", rb_imemo_name(imemo_type(obj)));
 
 	    switch (imemo_type(obj)) {
 	      case imemo_ment:
                 {
                     const rb_method_entry_t *me = &RANY(obj)->as.imemo.ment;
 
-                    APPENDF((BUFF_ARGS, ":%s (%s%s%s%s) type:%s alias:%d owner:%p defined_class:%p",
+                    APPEND_F(":%s (%s%s%s%s) type:%s alias:%d owner:%p defined_class:%p",
                              rb_id2name(me->called_id),
                              METHOD_ENTRY_VISI(me) == METHOD_VISI_PUBLIC ?  "pub" :
                              METHOD_ENTRY_VISI(me) == METHOD_VISI_PRIVATE ? "pri" : "pro",
@@ -13863,14 +13863,14 @@ rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj) https://github.com/ruby/ruby/blob/trunk/gc.c#L13863
                              me->def ? rb_method_type_name(me->def->type) : "NULL",
                             (... truncated)

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

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