From 00fb802284de17da913506377f4a346210baa4a6 Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 13 Jan 2008 01:21:42 +0000 Subject: * encoding.c (enc_table): packed all enc_table stuff. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15015 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 4 +++ enc/make_encdb.rb | 18 ++++------ encoding.c | 102 ++++++++++++++++++++++++++++-------------------------- 3 files changed, 64 insertions(+), 60 deletions(-) diff --git a/ChangeLog b/ChangeLog index fb9be7bac6..be36f2df86 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Sun Jan 13 10:21:40 2008 Nobuyoshi Nakada + + * encoding.c (enc_table): packed all enc_table stuff. + Sun Jan 13 09:58:17 2008 NARUSE, Yui * encoding.c (rb_enc_init): revert removing SJIS. diff --git a/enc/make_encdb.rb b/enc/make_encdb.rb index b49f13c16e..32dacbc740 100755 --- a/enc/make_encdb.rb +++ b/enc/make_encdb.rb @@ -40,25 +40,21 @@ open('encdb.h', 'wb') do |f| replicas.each_key {|name| f.puts' "%s",' % name} aliases.each_key {|name| f.puts' "%s",' % name} f.puts(<<"_TEXT_") - NULL }; -static const int enc_name_list_size = #{encodings.length + replicas.length + aliases.length}; -static const int enc_aliases_size = #{aliases.length}; -static st_table *enc_table_replica_name; -static st_table *enc_table_alias_name; +#define enc_name_list_size (sizeof(enc_name_list)/sizeof(enc_name_list[0])) static void enc_init_db(void) { - if (!enc_table_replica_name) { - enc_table_replica_name = st_init_strcasetable(); + if (!enc_table.replica_name) { + enc_table.replica_name = st_init_strcasetable(); } - if (!enc_table_alias_name) { - enc_table_alias_name = st_init_strcasetable(); + if (!enc_table.alias_name) { + enc_table.alias_name = st_init_strcasetable(); } _TEXT_ replicas.each_pair {|name, orig| - f.puts' st_insert(enc_table_replica_name, (st_data_t)"%s", (st_data_t)"%s");' % [name, orig]} + f.puts' st_insert(enc_table.replica_name, (st_data_t)"%s", (st_data_t)"%s");' % [name, orig]} aliases.each_pair {|name, orig| - f.puts' st_insert(enc_table_alias_name, (st_data_t)"%s", (st_data_t)"%s");' % [name, orig]} + f.puts' st_insert(enc_table.alias_name, (st_data_t)"%s", (st_data_t)"%s");' % [name, orig]} f.puts '}' end diff --git a/encoding.c b/encoding.c index b7debda0ba..c201213da8 100644 --- a/encoding.c +++ b/encoding.c @@ -12,7 +12,6 @@ #include "ruby/ruby.h" #include "ruby/encoding.h" #include "regenc.h" -#include "encdb.h" #include #ifdef HAVE_LANGINFO_H #include @@ -26,10 +25,16 @@ struct rb_encoding_entry { rb_encoding *enc; }; -static struct rb_encoding_entry *enc_table; -static int enc_table_count; -static int enc_table_size; -static st_table *enc_table_alias; +static struct { + struct rb_encoding_entry *list; + int count; + int size; + st_table *alias; + st_table *replica_name; + st_table *alias_name; +} enc_table; + +#include "encdb.h" #define ENC_UNINITIALIZED (&rb_cEncoding) #define enc_initialized_p(enc) ((enc)->auxiliary_data != &rb_cEncoding) @@ -108,8 +113,8 @@ void rb_gc_mark_encodings(void) { int i; - for (i = 0; i < enc_table_size; ++i) { - rb_encoding *enc = enc_table[i].enc; + for (i = 0; i < enc_table.size; ++i) { + rb_encoding *enc = enc_table.list[i].enc; if (enc && enc_initialized_p(enc)) { rb_gc_mark(ENC_FROM_ENCODING(enc)); } @@ -121,19 +126,19 @@ enc_table_expand(int newsize) { struct rb_encoding_entry *ent; - if (enc_table_size >= newsize) return newsize; - ent = realloc(enc_table, sizeof(*enc_table) * newsize); + if (enc_table.size >= newsize) return newsize; + ent = realloc(enc_table.list, sizeof(*enc_table.list) * newsize); if (!ent) return -1; - memset(ent + enc_table_size, 0, sizeof(*ent)*(newsize - enc_table_size)); - enc_table = ent; - enc_table_size = newsize; + memset(ent + enc_table.size, 0, sizeof(*ent)*(newsize - enc_table.size)); + enc_table.list = ent; + enc_table.size = newsize; return newsize; } static int enc_register_at(int index, const char *name, rb_encoding *encoding) { - struct rb_encoding_entry *ent = &enc_table[index]; + struct rb_encoding_entry *ent = &enc_table.list[index]; ent->name = name; if (!ent->enc) ent->enc = malloc(sizeof(rb_encoding)); @@ -154,11 +159,11 @@ enc_register_at(int index, const char *name, rb_encoding *encoding) static int enc_register(const char *name, rb_encoding *encoding) { - int index = enc_table_count; + int index = enc_table.count; - if (index >= ENCODING_INLINE_MAX) index = enc_table_size; + if (index >= ENCODING_INLINE_MAX) index = enc_table.size; if ((index = enc_table_expand(index + 1)) < 0) return -1; - enc_table_count = index; + enc_table.count = index; return enc_register_at(index - 1, name, encoding); } @@ -174,7 +179,7 @@ rb_enc_register(const char *name, rb_encoding *encoding) rb_encoding *oldenc = rb_enc_from_index(index); if (STRCASECMP(name, rb_enc_name(oldenc))) { st_data_t key = (st_data_t)name, alias; - st_delete(enc_table_alias, &key, &alias); + st_delete(enc_table.alias, &key, &alias); index = enc_register(name, encoding); } else if (enc_initialized_p(oldenc) && @@ -203,7 +208,7 @@ enc_check_duplication(const char *name) static VALUE set_base_encoding(int index, rb_encoding *base) { - VALUE enc = rb_enc_from_encoding(enc_table[index].enc); + VALUE enc = rb_enc_from_encoding(enc_table.list[index].enc); rb_ivar_set(enc, id_base_encoding, rb_enc_from_encoding(base)); if (rb_enc_dummy_p(base)) FL_SET(enc, ENC_DUMMY); @@ -213,7 +218,7 @@ set_base_encoding(int index, rb_encoding *base) int rb_enc_replicate(const char *name, rb_encoding *encoding) { - int index = enc_table_size; + int index = enc_table.size; enc_check_duplication(name); if (enc_table_expand(index + 1) < 0) return -1; @@ -225,7 +230,7 @@ rb_enc_replicate(const char *name, rb_encoding *encoding) int rb_define_dummy_encoding(const char *name) { - int index = enc_table_size; + int index = enc_table.size; rb_encoding *encoding; VALUE enc; @@ -271,15 +276,15 @@ enc_alias(const char *alias, const char *orig) st_data_t data; int idx; - if (!enc_table_alias) { - enc_table_alias = st_init_strcasetable(); + if (!enc_table.alias) { + enc_table.alias = st_init_strcasetable(); } if ((idx = rb_enc_find_index(orig)) < 0) { - if (!st_lookup(enc_table_alias, (st_data_t)orig, &data)) + if (!st_lookup(enc_table.alias, (st_data_t)orig, &data)) return -1; idx = (int)data; } - st_insert(enc_table_alias, (st_data_t)alias, (st_data_t)idx); + st_insert(enc_table.alias, (st_data_t)alias, (st_data_t)idx); return idx; } @@ -301,7 +306,7 @@ enum { void rb_enc_init(void) { - enc_table_count = enc_table_expand(ENCINDEX_BUILTIN_MAX); + enc_table.count = enc_table_expand(ENCINDEX_BUILTIN_MAX); #define ENC_REGISTER(enc) enc_register_at(ENCINDEX_##enc, rb_enc_name(ONIG_ENCODING_##enc), ONIG_ENCODING_##enc) ENC_REGISTER(ASCII); ENC_REGISTER(EUC_JP); @@ -313,13 +318,13 @@ rb_enc_init(void) rb_encoding * rb_enc_from_index(int index) { - if (!enc_table) { + if (!enc_table.list) { rb_enc_init(); } - if (index < 0 || enc_table_size <= index) { + if (index < 0 || enc_table.size <= index) { return 0; } - return enc_table[index].enc; + return enc_table.list[index].enc; } int @@ -329,20 +334,20 @@ rb_enc_registered(const char *name) st_data_t alias = 0; if (!name) return -1; - if (!enc_table) { + if (!enc_table.list) { rb_enc_init(); } - for (i=0; i