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

ruby-changes:38426

From: nobu <ko1@a...>
Date: Sat, 16 May 2015 21:21:35 +0900 (JST)
Subject: [ruby-changes:38426] nobu:r50507 (trunk): proc.c: rb_proc_alloc

nobu	2015-05-16 21:21:25 +0900 (Sat, 16 May 2015)

  New Revision: 50507

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

  Log:
    proc.c: rb_proc_alloc
    
    * proc.c (rb_proc_alloc, proc_dup): allocate rb_proc_t instead of
      wrapping to get rid of potential memory leak.
    
    * vm.c (rb_proc_create): ditto.

  Modified files:
    trunk/proc.c
    trunk/vm.c
    trunk/vm_core.h
Index: vm_core.h
===================================================================
--- vm_core.h	(revision 50506)
+++ vm_core.h	(revision 50507)
@@ -920,7 +920,7 @@ rb_block_t *rb_vm_control_frame_block_pt https://github.com/ruby/ruby/blob/trunk/vm_core.h#L920
 
 /* VM related object allocate functions */
 VALUE rb_thread_alloc(VALUE klass);
-VALUE rb_proc_wrap(VALUE klass, rb_proc_t *); /* may use with rb_proc_alloc */
+VALUE rb_proc_alloc(VALUE klass);
 VALUE rb_binding_alloc(VALUE klass);
 
 /* for debug */
Index: proc.c
===================================================================
--- proc.c	(revision 50506)
+++ proc.c	(revision 50507)
@@ -71,11 +71,10 @@ static const rb_data_type_t proc_data_ty https://github.com/ruby/ruby/blob/trunk/proc.c#L71
 };
 
 VALUE
-rb_proc_wrap(VALUE klass, rb_proc_t *proc)
+rb_proc_alloc(VALUE klass)
 {
-    proc->block.proc = TypedData_Wrap_Struct(klass, &proc_data_type, proc);
-
-    return proc->block.proc;
+    rb_proc_t *proc;
+    return TypedData_Make_Struct(klass, rb_proc_t, &proc_data_type, proc);
 }
 
 VALUE
@@ -95,11 +94,13 @@ proc_dup(VALUE self) https://github.com/ruby/ruby/blob/trunk/proc.c#L94
 {
     VALUE procval;
     rb_proc_t *src;
-    rb_proc_t *dst = ALLOC(rb_proc_t);
+    rb_proc_t *dst;
 
     GetProcPtr(self, src);
+    procval = rb_proc_alloc(rb_cProc);
+    GetProcPtr(procval, dst);
     *dst = *src;
-    procval = rb_proc_wrap(rb_cProc, dst);
+    dst->block.proc = procval;
     RB_GC_GUARD(self); /* for: body = proc_dup(body) */
 
     return procval;
Index: vm.c
===================================================================
--- vm.c	(revision 50506)
+++ vm.c	(revision 50507)
@@ -658,24 +658,18 @@ vm_make_proc_from_block(rb_thread_t *th, https://github.com/ruby/ruby/blob/trunk/vm.c#L658
 }
 
 static inline VALUE
-rb_proc_alloc(VALUE klass, const rb_block_t *block,
-		VALUE envval, VALUE blockprocval,
-		int8_t safe_level, int8_t is_from_method, int8_t is_lambda)
+rb_proc_create(VALUE klass, const rb_block_t *block,
+	       VALUE envval, VALUE blockprocval,
+	       int8_t safe_level, int8_t is_from_method, int8_t is_lambda)
 {
-    VALUE procval;
-    rb_proc_t *proc = ALLOC(rb_proc_t);
+    VALUE procval = rb_proc_alloc(klass);
+    rb_proc_t *proc = RTYPEDDATA_DATA(procval);
 
     proc->block = *block;
+    proc->block.proc = procval;
     proc->safe_level = safe_level;
     proc->is_from_method = is_from_method;
     proc->is_lambda = is_lambda;
-
-    procval = rb_proc_wrap(klass, proc);
-
-    /*
-     * ensure VALUEs are markable here as rb_proc_wrap may trigger allocation
-     * and clobber envval + blockprocval
-     */
     proc->envval = envval;
     proc->blockprocval = blockprocval;
 
@@ -704,8 +698,8 @@ rb_vm_make_proc_lambda(rb_thread_t *th, https://github.com/ruby/ruby/blob/trunk/vm.c#L698
 	check_env_value(envval);
     }
 
-    procval = rb_proc_alloc(klass, block, envval, blockprocval,
-			    (int8_t)th->safe_level, 0, is_lambda);
+    procval = rb_proc_create(klass, block, envval, blockprocval,
+			     (int8_t)th->safe_level, 0, is_lambda);
 
     if (VMDEBUG) {
 	if (th->stack < block->ep && block->ep < th->stack + th->stack_size) {

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

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