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

ruby-changes:30938

From: nobu <ko1@a...>
Date: Mon, 23 Sep 2013 05:10:46 +0900 (JST)
Subject: [ruby-changes:30938] nobu:r43017 (trunk): tracepoint.c: wrap data in a struct

nobu	2013-09-23 05:10:40 +0900 (Mon, 23 Sep 2013)

  New Revision: 43017

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43017

  Log:
    tracepoint.c: wrap data in a struct
    
    * ext/-test-/tracepoint/tracepoint.c (struct tracepoint_track): wrap
      tracepoint tracking data in a struct to be placed on the stack.

  Modified files:
    trunk/ext/-test-/tracepoint/tracepoint.c
Index: ext/-test-/tracepoint/tracepoint.c
===================================================================
--- ext/-test-/tracepoint/tracepoint.c	(revision 43016)
+++ ext/-test-/tracepoint/tracepoint.c	(revision 43017)
@@ -1,38 +1,45 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/tracepoint/tracepoint.c#L1
 #include "ruby/ruby.h"
 #include "ruby/debug.h"
 
-static size_t newobj_count;
-static size_t free_count;
-static size_t gc_start_count;
-static size_t gc_end_count;
-static size_t objects_count;
-static VALUE objects[10];
+struct tracepoint_track {
+    size_t newobj_count;
+    size_t free_count;
+    size_t gc_start_count;
+    size_t gc_end_count;
+    size_t objects_count;
+    VALUE objects[10];
+};
 
-void
+#define objects_max (sizeof(((struct tracepoint_track *)NULL)->objects)/sizeof(VALUE))
+
+static void
 tracepoint_track_objspace_events_i(VALUE tpval, void *data)
 {
     rb_trace_arg_t *tparg = rb_tracearg_from_tracepoint(tpval);
+    struct tracepoint_track *track = data;
+
     switch (rb_tracearg_event_flag(tparg)) {
       case RUBY_INTERNAL_EVENT_NEWOBJ:
 	{
 	    VALUE obj = rb_tracearg_object(tparg);
-	    if (objects_count < sizeof(objects)/sizeof(VALUE)) objects[objects_count++] = obj;
-	    newobj_count++;
+	    if (track->objects_count < objects_max)
+		track->objects[track->objects_count++] = obj;
+	    track->newobj_count++;
 	    break;
 	}
       case RUBY_INTERNAL_EVENT_FREEOBJ:
 	{
-	    free_count++;
+	    track->free_count++;
 	    break;
 	}
       case RUBY_INTERNAL_EVENT_GC_START:
 	{
-	    gc_start_count++;
+	    track->gc_start_count++;
 	    break;
 	}
       case RUBY_INTERNAL_EVENT_GC_END:
 	{
-	    gc_end_count++;
+	    track->gc_end_count++;
 	    break;
 	}
       default:
@@ -40,28 +47,24 @@ tracepoint_track_objspace_events_i(VALUE https://github.com/ruby/ruby/blob/trunk/ext/-test-/tracepoint/tracepoint.c#L47
     }
 }
 
-VALUE
+static VALUE
 tracepoint_track_objspace_events(VALUE self)
 {
+    struct tracepoint_track track = {0, 0, 0, 0, 0, {}};
     VALUE tpval = rb_tracepoint_new(0, RUBY_INTERNAL_EVENT_NEWOBJ | RUBY_INTERNAL_EVENT_FREEOBJ |
 				    RUBY_INTERNAL_EVENT_GC_START | RUBY_INTERNAL_EVENT_GC_END,
-				    tracepoint_track_objspace_events_i, 0);
+				    tracepoint_track_objspace_events_i, &track);
     VALUE result = rb_ary_new();
-    size_t i;
-
-    newobj_count = free_count = gc_start_count = objects_count = 0;
 
     rb_tracepoint_enable(tpval);
     rb_yield(Qundef);
     rb_tracepoint_disable(tpval);
 
-    rb_ary_push(result, SIZET2NUM(newobj_count));
-    rb_ary_push(result, SIZET2NUM(free_count));
-    rb_ary_push(result, SIZET2NUM(gc_start_count));
-    rb_ary_push(result, SIZET2NUM(gc_end_count));
-    for (i=0; i<objects_count; i++) {
-	rb_ary_push(result, objects[i]);
-    }
+    rb_ary_push(result, SIZET2NUM(track.newobj_count));
+    rb_ary_push(result, SIZET2NUM(track.free_count));
+    rb_ary_push(result, SIZET2NUM(track.gc_start_count));
+    rb_ary_push(result, SIZET2NUM(track.gc_end_count));
+    rb_ary_cat(result, track.objects, track.objects_count);
 
     return result;
 }
@@ -69,10 +72,6 @@ tracepoint_track_objspace_events(VALUE s https://github.com/ruby/ruby/blob/trunk/ext/-test-/tracepoint/tracepoint.c#L72
 void
 Init_tracepoint(void)
 {
-    size_t i;
     VALUE mBug = rb_define_module("Bug");
     rb_define_module_function(mBug, "tracepoint_track_objspace_events", tracepoint_track_objspace_events, 0);
-    for (i=0; i<sizeof(objects)/sizeof(VALUE); i++) {
-	rb_global_variable(objects+i);
-    }
 }

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

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