* class.c, constant.h, gc.c, method.h, object.c, variable.c,

vm_insnhelper.c: use struct rb_constant_entry_t as entry of
  RCLASS_CONST_TBL.  RCLASS_CONST_TBL has contained VALUE of constant
  directly.  Now instead rb_const_entry_t is contained in
  RCLASS_CONST_TBL,  rb_const_entry_t is managed by malloc, and
  have not only the value itself but also visibility flag.
  This is another preparation for private constant (see
  [ruby-dev:39685][ruby-core:32698]).

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29602 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mame 2010-10-26 17:27:32 +00:00
parent e169ea0cac
commit a115768161
8 changed files with 116 additions and 18 deletions

View File

@ -1,3 +1,14 @@
Wed Oct 27 02:02:54 2010 Yusuke Endoh <mame@tsg.ne.jp>
* class.c, constant.h, gc.c, method.h, object.c, variable.c,
vm_insnhelper.c: use struct rb_constant_entry_t as entry of
RCLASS_CONST_TBL. RCLASS_CONST_TBL has contained VALUE of constant
directly. Now instead rb_const_entry_t is contained in
RCLASS_CONST_TBL, rb_const_entry_t is managed by malloc, and
have not only the value itself but also visibility flag.
This is another preparation for private constant (see
[ruby-dev:39685][ruby-core:32698]).
Wed Oct 27 01:56:34 2010 Yusuke Endoh <mame@tsg.ne.jp> Wed Oct 27 01:56:34 2010 Yusuke Endoh <mame@tsg.ne.jp>
* class.c, gc.c, object.c, variable.c, vm_insnhelper.c, * class.c, gc.c, object.c, variable.c, vm_insnhelper.c,

18
class.c
View File

@ -26,6 +26,7 @@
#include "ruby/ruby.h" #include "ruby/ruby.h"
#include "ruby/st.h" #include "ruby/st.h"
#include "method.h" #include "method.h"
#include "constant.h"
#include "vm_core.h" #include "vm_core.h"
#include <ctype.h> #include <ctype.h>
@ -140,6 +141,14 @@ clone_method(ID mid, const rb_method_entry_t *me, struct clone_method_data *data
return ST_CONTINUE; return ST_CONTINUE;
} }
static int
clone_const(ID key, const rb_const_entry_t *ce, st_table *tbl)
{
rb_const_entry_t *nce = ALLOC(rb_const_entry_t);
*nce = *ce;
st_insert(tbl, key, (st_data_t)nce);
}
/* :nodoc: */ /* :nodoc: */
VALUE VALUE
rb_mod_init_copy(VALUE clone, VALUE orig) rb_mod_init_copy(VALUE clone, VALUE orig)
@ -164,15 +173,15 @@ rb_mod_init_copy(VALUE clone, VALUE orig)
} }
if (RCLASS_CONST_TBL(orig)) { if (RCLASS_CONST_TBL(orig)) {
if (RCLASS_CONST_TBL(clone)) { if (RCLASS_CONST_TBL(clone)) {
st_free_table(RCLASS_CONST_TBL(clone)); rb_free_const_table(RCLASS_CONST_TBL(clone));
} }
RCLASS_CONST_TBL(clone) = st_copy(RCLASS_CONST_TBL(orig)); RCLASS_CONST_TBL(clone) = st_init_numtable();
st_foreach(RCLASS_CONST_TBL(orig), clone_const, (st_data_t)RCLASS_CONST_TBL(clone));
} }
if (RCLASS_M_TBL(orig)) { if (RCLASS_M_TBL(orig)) {
struct clone_method_data data; struct clone_method_data data;
if (RCLASS_M_TBL(clone)) { if (RCLASS_M_TBL(clone)) {
extern void rb_free_m_table(st_table *tbl);
rb_free_m_table(RCLASS_M_TBL(clone)); rb_free_m_table(RCLASS_M_TBL(clone));
} }
data.tbl = RCLASS_M_TBL(clone) = st_init_numtable(); data.tbl = RCLASS_M_TBL(clone) = st_init_numtable();
@ -224,7 +233,8 @@ rb_singleton_class_clone(VALUE obj)
RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(klass)); RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(klass));
} }
if (RCLASS_CONST_TBL(klass)) { if (RCLASS_CONST_TBL(klass)) {
RCLASS_CONST_TBL(clone) = st_copy(RCLASS_CONST_TBL(klass)); RCLASS_CONST_TBL(clone) = st_init_numtable();
st_foreach(RCLASS_CONST_TBL(klass), clone_const, (st_data_t)RCLASS_CONST_TBL(clone));
} }
RCLASS_M_TBL(clone) = st_init_numtable(); RCLASS_M_TBL(clone) = st_init_numtable();
data.tbl = RCLASS_M_TBL(clone); data.tbl = RCLASS_M_TBL(clone);

26
constant.h Normal file
View File

@ -0,0 +1,26 @@
/**********************************************************************
constant.h -
$Author$
created at: Sun Nov 15 00:09:33 2009
Copyright (C) 2009 Yusuke Endoh
**********************************************************************/
#ifndef CONSTANT_H
#define CONSTANT_H
typedef enum {
CONST_PUBLIC = 0x00,
CONST_PRIVATE = 0x01,
} rb_const_flag_t;
typedef struct rb_const_entry_struct {
rb_const_flag_t flag;
VALUE value; /* should be mark */
} rb_const_entry_t;
void rb_free_const_table(st_table *tbl);
#endif /* CONSTANT_H */

37
gc.c
View File

@ -19,6 +19,7 @@
#include "eval_intern.h" #include "eval_intern.h"
#include "vm_core.h" #include "vm_core.h"
#include "gc.h" #include "gc.h"
#include "constant.h"
#include <stdio.h> #include <stdio.h>
#include <setjmp.h> #include <setjmp.h>
#include <sys/types.h> #include <sys/types.h>
@ -1504,6 +1505,38 @@ rb_free_m_table(st_table *tbl)
st_free_table(tbl); st_free_table(tbl);
} }
static int
mark_const_entry_i(ID key, const rb_const_entry_t *ce, st_data_t data)
{
struct mark_tbl_arg *arg = (void*)data;
gc_mark(arg->objspace, ce->value, arg->lev);
return ST_CONTINUE;
}
static void
mark_const_tbl(rb_objspace_t *objspace, st_table *tbl, int lev)
{
struct mark_tbl_arg arg;
if (!tbl) return;
arg.objspace = objspace;
arg.lev = lev;
st_foreach(tbl, mark_const_entry_i, (st_data_t)&arg);
}
static int
free_const_entry_i(ID key, rb_const_entry_t *ce, st_data_t data)
{
xfree(ce);
return ST_CONTINUE;
}
void
rb_free_const_table(st_table *tbl)
{
st_foreach(tbl, free_const_entry_i, 0);
st_free_table(tbl);
}
void void
rb_mark_tbl(st_table *tbl) rb_mark_tbl(st_table *tbl)
{ {
@ -1718,7 +1751,7 @@ gc_mark_children(rb_objspace_t *objspace, VALUE ptr, int lev)
case T_MODULE: case T_MODULE:
mark_m_tbl(objspace, RCLASS_M_TBL(obj), lev); mark_m_tbl(objspace, RCLASS_M_TBL(obj), lev);
mark_tbl(objspace, RCLASS_IV_TBL(obj), lev); mark_tbl(objspace, RCLASS_IV_TBL(obj), lev);
mark_tbl(objspace, RCLASS_CONST_TBL(obj), lev); mark_const_tbl(objspace, RCLASS_CONST_TBL(obj), lev);
ptr = RCLASS_SUPER(obj); ptr = RCLASS_SUPER(obj);
goto again; goto again;
@ -2181,7 +2214,7 @@ obj_free(rb_objspace_t *objspace, VALUE obj)
st_free_table(RCLASS_IV_TBL(obj)); st_free_table(RCLASS_IV_TBL(obj));
} }
if (RCLASS_CONST_TBL(obj)) { if (RCLASS_CONST_TBL(obj)) {
st_free_table(RCLASS_CONST_TBL(obj)); rb_free_const_table(RCLASS_CONST_TBL(obj));
} }
if (RCLASS_IV_INDEX_TBL(obj)) { if (RCLASS_IV_INDEX_TBL(obj)) {
st_free_table(RCLASS_IV_INDEX_TBL(obj)); st_free_table(RCLASS_IV_INDEX_TBL(obj));

View File

@ -99,5 +99,6 @@ int rb_method_entry_arity(const rb_method_entry_t *me);
void rb_mark_method_entry(const rb_method_entry_t *me); void rb_mark_method_entry(const rb_method_entry_t *me);
void rb_free_method_entry(rb_method_entry_t *me); void rb_free_method_entry(rb_method_entry_t *me);
void rb_sweep_method_entry(void *vm); void rb_sweep_method_entry(void *vm);
void rb_free_m_table(st_table *tbl);
#endif /* METHOD_H */ #endif /* METHOD_H */

View File

@ -19,6 +19,7 @@
#include <ctype.h> #include <ctype.h>
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include "constant.h"
VALUE rb_cBasicObject; VALUE rb_cBasicObject;
VALUE rb_mKernel; VALUE rb_mKernel;
@ -222,7 +223,7 @@ init_copy(VALUE dest, VALUE obj)
RCLASS_IV_TBL(dest) = 0; RCLASS_IV_TBL(dest) = 0;
} }
if (RCLASS_CONST_TBL(dest)) { if (RCLASS_CONST_TBL(dest)) {
st_free_table(RCLASS_CONST_TBL(dest)); rb_free_const_table(RCLASS_CONST_TBL(dest));
RCLASS_CONST_TBL(dest) = 0; RCLASS_CONST_TBL(dest) = 0;
} }
if (RCLASS_IV_TBL(obj)) { if (RCLASS_IV_TBL(obj)) {

View File

@ -16,6 +16,7 @@
#include "ruby/util.h" #include "ruby/util.h"
#include "ruby/encoding.h" #include "ruby/encoding.h"
#include "node.h" #include "node.h"
#include "constant.h"
void rb_vm_change_state(void); void rb_vm_change_state(void);
void rb_vm_inc_const_missing_count(void); void rb_vm_inc_const_missing_count(void);
@ -71,8 +72,9 @@ fc_path(struct fc_result *fc, ID name)
} }
static int static int
fc_i(ID key, VALUE value, struct fc_result *res) fc_i(ID key, rb_const_entry_t *ce, struct fc_result *res)
{ {
VALUE value = ce->value;
if (!rb_is_const_id(key)) return ST_CONTINUE; if (!rb_is_const_id(key)) return ST_CONTINUE;
if (value == res->klass) { if (value == res->klass) {
@ -1439,7 +1441,7 @@ rb_autoload(VALUE mod, ID id, const char *file)
rb_raise(rb_eArgError, "empty file name"); rb_raise(rb_eArgError, "empty file name");
} }
if ((tbl = RCLASS_CONST_TBL(mod)) && st_lookup(tbl, (st_data_t)id, &av) && (VALUE)av != Qundef) if ((tbl = RCLASS_CONST_TBL(mod)) && st_lookup(tbl, (st_data_t)id, &av) && ((rb_const_entry_t*)av)->value != Qundef)
return; return;
rb_const_set(mod, id, Qundef); rb_const_set(mod, id, Qundef);
@ -1463,8 +1465,11 @@ static NODE*
autoload_delete(VALUE mod, ID id) autoload_delete(VALUE mod, ID id)
{ {
st_data_t val, load = 0, n = id; st_data_t val, load = 0, n = id;
rb_const_entry_t *ce;
st_delete(RCLASS_CONST_TBL(mod), &n, 0); st_delete(RCLASS_CONST_TBL(mod), &n, &val);
ce = (rb_const_entry_t*)val;
if (ce) xfree(ce);
if (st_lookup(RCLASS_IV_TBL(mod), (st_data_t)autoload, &val)) { if (st_lookup(RCLASS_IV_TBL(mod), (st_data_t)autoload, &val)) {
struct st_table *tbl = check_autoload_table((VALUE)val); struct st_table *tbl = check_autoload_table((VALUE)val);
@ -1473,6 +1478,8 @@ autoload_delete(VALUE mod, ID id)
if (tbl->num_entries == 0) { if (tbl->num_entries == 0) {
n = autoload; n = autoload;
st_delete(RCLASS_CONST_TBL(mod), &n, &val); st_delete(RCLASS_CONST_TBL(mod), &n, &val);
ce = (rb_const_entry_t*)val;
if (ce) xfree(ce);
} }
} }
@ -1532,7 +1539,7 @@ autoload_node_id(VALUE mod, ID id)
struct st_table *tbl = RCLASS_CONST_TBL(mod); struct st_table *tbl = RCLASS_CONST_TBL(mod);
st_data_t val; st_data_t val;
if (!tbl || !st_lookup(tbl, (st_data_t)id, &val) || (VALUE)val != Qundef) { if (!tbl || !st_lookup(tbl, (st_data_t)id, &val) || ((rb_const_entry_t*)val)->value != Qundef) {
return 0; return 0;
} }
return 1; return 1;
@ -1579,7 +1586,7 @@ rb_const_get_0(VALUE klass, ID id, int exclude, int recurse)
VALUE am = 0; VALUE am = 0;
st_data_t data; st_data_t data;
while (RCLASS_CONST_TBL(tmp) && st_lookup(RCLASS_CONST_TBL(tmp), (st_data_t)id, &data)) { while (RCLASS_CONST_TBL(tmp) && st_lookup(RCLASS_CONST_TBL(tmp), (st_data_t)id, &data)) {
value = (VALUE)data; value = ((rb_const_entry_t*)data)->value;
if (value == Qundef) { if (value == Qundef) {
if (am == tmp) break; if (am == tmp) break;
am = tmp; am = tmp;
@ -1666,16 +1673,17 @@ rb_const_remove(VALUE mod, ID id)
rb_vm_change_state(); rb_vm_change_state();
val = (VALUE)v; val = ((rb_const_entry_t*)v)->value;
if (val == Qundef) { if (val == Qundef) {
autoload_delete(mod, id); autoload_delete(mod, id);
val = Qnil; val = Qnil;
} }
xfree((rb_const_entry_t*)v);
return val; return val;
} }
static int static int
sv_i(ID key, VALUE value, st_table *tbl) sv_i(ID key, rb_const_entry_t *ce, st_table *tbl)
{ {
if (rb_is_const_id(key)) { if (rb_is_const_id(key)) {
if (!st_lookup(tbl, (st_data_t)key, 0)) { if (!st_lookup(tbl, (st_data_t)key, 0)) {
@ -1779,7 +1787,7 @@ rb_const_defined_0(VALUE klass, ID id, int exclude, int recurse)
retry: retry:
while (tmp) { while (tmp) {
if (RCLASS_CONST_TBL(tmp) && st_lookup(RCLASS_CONST_TBL(tmp), (st_data_t)id, &value)) { if (RCLASS_CONST_TBL(tmp) && st_lookup(RCLASS_CONST_TBL(tmp), (st_data_t)id, &value)) {
if ((VALUE)value == Qundef && !autoload_node((VALUE)klass, id, 0)) if (((rb_const_entry_t*)value)->value == Qundef && !autoload_node((VALUE)klass, id, 0))
return (int)Qfalse; return (int)Qfalse;
return (int)Qtrue; return (int)Qtrue;
} }
@ -1823,6 +1831,8 @@ check_before_mod_set(VALUE klass, ID id, VALUE val, const char *dest)
void void
rb_const_set(VALUE klass, ID id, VALUE val) rb_const_set(VALUE klass, ID id, VALUE val)
{ {
rb_const_entry_t *ce;
if (NIL_P(klass)) { if (NIL_P(klass)) {
rb_raise(rb_eTypeError, "no class/module to define constant %s", rb_raise(rb_eTypeError, "no class/module to define constant %s",
rb_id2name(id)); rb_id2name(id));
@ -1836,7 +1846,7 @@ rb_const_set(VALUE klass, ID id, VALUE val)
st_data_t value; st_data_t value;
if (st_lookup(RCLASS_CONST_TBL(klass), (st_data_t)id, &value)) { if (st_lookup(RCLASS_CONST_TBL(klass), (st_data_t)id, &value)) {
if ((VALUE)value == Qundef) if (((rb_const_entry_t*)value)->value == Qundef)
autoload_delete(klass, id); autoload_delete(klass, id);
else else
rb_warn("already initialized constant %s", rb_id2name(id)); rb_warn("already initialized constant %s", rb_id2name(id));
@ -1844,7 +1854,12 @@ rb_const_set(VALUE klass, ID id, VALUE val)
} }
rb_vm_change_state(); rb_vm_change_state();
st_insert(RCLASS_CONST_TBL(klass), (st_data_t)id, (st_data_t)val);
ce = ALLOC(rb_const_entry_t);
ce->flag = CONST_PUBLIC;
ce->value = val;
st_insert(RCLASS_CONST_TBL(klass), (st_data_t)id, (st_data_t)ce);
} }
void void

View File

@ -11,6 +11,7 @@
/* finish iseq array */ /* finish iseq array */
#include "insns.inc" #include "insns.inc"
#include <math.h> #include <math.h>
#include "constant.h"
/* control stack frame */ /* control stack frame */
@ -1171,7 +1172,7 @@ vm_get_ev_const(rb_thread_t *th, const rb_iseq_t *iseq,
search_continue: search_continue:
if (RCLASS_CONST_TBL(klass) && if (RCLASS_CONST_TBL(klass) &&
st_lookup(RCLASS_CONST_TBL(klass), id, &data)) { st_lookup(RCLASS_CONST_TBL(klass), id, &data)) {
val = (st_data_t)data; val = ((rb_const_entry_t*)data)->value;
if (val == Qundef) { if (val == Qundef) {
if (am == klass) break; if (am == klass) break;
am = klass; am = klass;