X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=tables.c;h=47bcdb794fb04afab9fb815cdf2fb9a05396c8a7;hb=6e0a1ddae11315b0f3d0d48812f0eb9799a9f2e2;hp=96891cbee54942bff3727095705e52fa1bf5c3ef;hpb=9df2bcaa5c50904777635c1afb45527e71a9c2d2;p=cacao.git diff --git a/tables.c b/tables.c index 96891cbee..47bcdb794 100644 --- a/tables.c +++ b/tables.c @@ -35,7 +35,7 @@ - the heap - additional support functions - $Id: tables.c 1067 2004-05-18 10:25:51Z stefan $ + $Id: tables.c 1445 2004-11-05 13:55:33Z twisti $ */ @@ -47,26 +47,28 @@ #include #include #include +#include "builtin.h" +#include "exceptions.h" #include "types.h" -#include "main.h" +#include "native.h" +#include "options.h" #include "tables.h" #include "loader.h" #include "asmpart.h" +#include "statistics.h" #include "threads/thread.h" #include "threads/locks.h" #include "toolbox/logging.h" #include "toolbox/memory.h" -/* statistics */ -int count_utf_len = 0; /* size of utf hash */ -int count_utf_new = 0; /* calls of utf_new */ -int count_utf_new_found = 0; /* calls of utf_new with fast return */ - hashtable utf_hash; /* hashtable for utf8-symbols */ hashtable string_hash; /* hashtable for javastrings */ hashtable class_hash; /* hashtable for classes */ +list unlinkedclasses; /* this is only used for eager class loading */ + + /****************************************************************************** *********************** hashtable functions ********************************** ******************************************************************************/ @@ -109,11 +111,14 @@ void tables_init() init_hashtable(&utf_hash, UTF_HASHSTART); /* hashtable for utf8-symbols */ init_hashtable(&string_hash, HASHSTART); /* hashtable for javastrings */ init_hashtable(&class_hash, HASHSTART); /* hashtable for classes */ - -#ifdef STATISTICS - count_utf_len += sizeof(utf*) * utf_hash.size; -#endif +/* if (opt_eager) */ +/* list_init(&unlinkedclasses, OFFSET(classinfo, listnode)); */ + +#if defined(STATISTICS) + if (opt_stat) + count_utf_len += sizeof(utf*) * utf_hash.size; +#endif } @@ -123,7 +128,7 @@ void tables_init() *****************************************************************************/ -void tables_close(stringdeleter del) +void tables_close() { utf *u = NULL; literalstring *s; @@ -147,7 +152,7 @@ void tables_close(stringdeleter del) while (u) { /* process elements in external hash chain */ literalstring *nexts = s->hashlink; - del(s->string); + literalstring_free(s->string); FREE(s, literalstring); s = nexts; } @@ -243,7 +248,7 @@ void log_plain_utf(utf *u) write utf symbol into c-string (debugging purposes) -******************************************************************************/ +******************************************************************************/ void utf_sprint(char *buffer, utf *u) { @@ -288,7 +293,7 @@ void utf_sprint_classname(char *buffer, utf *u) write utf symbol into file -******************************************************************************/ +******************************************************************************/ void utf_fprint(FILE *file, utf *u) { @@ -308,6 +313,31 @@ void utf_fprint(FILE *file, utf *u) } +/********************* Funktion: utf_fprint ********************************** + + write utf symbol into file + +******************************************************************************/ + +void utf_fprint_classname(FILE *file, utf *u) +{ + char *endpos = utf_end(u); /* points behind utf string */ + char *utf_ptr = u->text; /* current position in utf text */ + + if (!u) + return; + + while (utf_ptr < endpos) { + /* read next unicode character */ + u2 c = utf_nextu2(&utf_ptr); + if (c == '/') c = '.'; + + if (c >= 32 && c <= 127) fprintf(file, "%c", c); + else fprintf(file, "?"); + } +} + + /****************** internal function: utf_hashkey *************************** The hashkey is computed from the utf-text by using up to 8 characters. @@ -457,7 +487,7 @@ u4 unicode_hashkey(u2 *text, u2 len) ******************************************************************************/ -utf *utf_new_int(char *text, u2 length) +utf *utf_new_intern(char *text, u2 length) { u4 key; /* hashkey computed from utf-text */ u4 slot; /* slot in hashtable */ @@ -465,7 +495,8 @@ utf *utf_new_int(char *text, u2 length) u2 i; #ifdef STATISTICS - count_utf_new++; + if (opt_stat) + count_utf_new++; #endif key = utf_hashkey(text, length); @@ -481,7 +512,8 @@ utf *utf_new_int(char *text, u2 length) if (text[i] != u->text[i]) goto nomatch; #ifdef STATISTICS - count_utf_new_found++; + if (opt_stat) + count_utf_new_found++; #endif /* log_text("symbol found in hash table");*/ /* symbol found in hashtable */ @@ -499,7 +531,8 @@ utf *utf_new_int(char *text, u2 length) } #ifdef STATISTICS - count_utf_len += sizeof(utf) + length; + if (opt_stat) + count_utf_len += sizeof(utf) + length; #endif /* location in hashtable found, create new utf element */ @@ -527,7 +560,8 @@ utf *utf_new_int(char *text, u2 length) newhash.entries = utf_hash.entries; #ifdef STATISTICS - count_utf_len += sizeof(utf*) * utf_hash.size; + if (opt_stat) + count_utf_len += sizeof(utf*) * utf_hash.size; #endif /* transfer elements to new hashtable */ @@ -562,7 +596,7 @@ utf *utf_new(char *text, u2 length) tables_lock(); #endif - r = utf_new_int(text, length); + r = utf_new_intern(text, length); #if defined(USE_THREADS) && defined(NATIVE_THREADS) tables_unlock(); @@ -785,7 +819,7 @@ u2 utf_nextu2(char **utf_ptr) switch ((ch1 = utf[0]) >> 4) { default: /* 1 byte */ (*utf_ptr)++; - return ch1; + return (u2) ch1; case 0xC: case 0xD: /* 2 bytes */ if (((ch2 = utf[1]) & 0xC0) == 0x80) { @@ -929,7 +963,7 @@ is_valid_name_utf(utf *u) *******************************************************************************/ -classinfo *class_new_int(utf *classname) +classinfo *class_new_intern(utf *classname) { classinfo *c; /* hashtable element */ u4 key; /* hashkey computed from classname */ @@ -956,8 +990,9 @@ classinfo *class_new_int(utf *classname) /* location in hashtable found, create new classinfo structure */ -#ifdef STATISTICS - count_class_infos += sizeof(classinfo); +#if defined(STATISTICS) + if (opt_stat) + count_class_infos += sizeof(classinfo); #endif if (initverbose) { @@ -968,6 +1003,7 @@ classinfo *class_new_int(utf *classname) } c = GCNEW(classinfo, 1); /*JOWENN: NEW*/ + /*c=NEW(classinfo);*/ c->vmClass = 0; c->flags = 0; c->name = classname; @@ -993,6 +1029,7 @@ classinfo *class_new_int(utf *classname) c->innerclass = NULL; c->vftbl = NULL; c->initialized = false; + c->initializing = false; c->classvftbl = false; c->classUsed = 0; c->impldBy = NULL; @@ -1041,7 +1078,7 @@ classinfo *class_new_int(utf *classname) /* Array classes need further initialization. */ if (c->name->text[0] == '[') { /* Array classes are not loaded from classfiles. */ - c->loaded = 1; + c->loaded = true; class_new_array(c); c->packagename = array_packagename; @@ -1057,26 +1094,78 @@ classinfo *class_new_int(utf *classname) } } } - +#if defined(USE_THREADS) && defined(NATIVE_THREADS) + initObjectLock(&c->header); +#endif + return c; } classinfo *class_new(utf *classname) { - classinfo *r; + classinfo *c; #if defined(USE_THREADS) && defined(NATIVE_THREADS) tables_lock(); #endif - r = class_new_int(classname); + c = class_new_intern(classname); + + /* we support eager class loading and linking on demand */ + + if (opt_eager) { + classinfo *tc; + classinfo *tmp; + + list_init(&unlinkedclasses, OFFSET(classinfo, listnode)); + + if (!c->loaded) { + if (!class_load(c)) { +#if defined(USE_THREADS) && defined(NATIVE_THREADS) + tables_unlock(); +#endif + return c; + } + } + + /* link all referenced classes */ + + tc = list_first(&unlinkedclasses); + + while (tc) { + /* skip the current loaded/linked class */ + if (tc != c) { + if (!class_link(tc)) { +#if defined(USE_THREADS) && defined(NATIVE_THREADS) + tables_unlock(); +#endif + return c; + } + } + + /* we need a tmp variable here, because list_remove sets prev and + next to NULL */ + tmp = list_next(&unlinkedclasses, tc); + list_remove(&unlinkedclasses, tc); + tc = tmp; + } + + if (!c->linked) { + if (!class_link(c)) { +#if defined(USE_THREADS) && defined(NATIVE_THREADS) + tables_unlock(); +#endif + return c; + } + } + } #if defined(USE_THREADS) && defined(NATIVE_THREADS) tables_unlock(); #endif - return r; + return c; } @@ -1087,24 +1176,24 @@ classinfo *class_new(utf *classname) *******************************************************************************/ -classinfo *class_get(utf *u) +classinfo *class_get(utf *classname) { classinfo *c; /* hashtable element */ u4 key; /* hashkey computed from classname */ u4 slot; /* slot in hashtable */ u2 i; - key = utf_hashkey (u->text, u->blength); + key = utf_hashkey(classname->text, classname->blength); slot = key & (class_hash.size-1); c = class_hash.ptr[slot]; /* search external hash-chain */ while (c) { - if (c->name->blength == u->blength) { - + if (c->name->blength == classname->blength) { /* compare classnames */ - for (i=0; iblength; i++) - if (u->text[i] != c->name->text[i]) goto nomatch; + for (i = 0; i < classname->blength; i++) + if (classname->text[i] != c->name->text[i]) + goto nomatch; /* class found in hashtable */ return c; @@ -1119,6 +1208,59 @@ classinfo *class_get(utf *u) } +/* class_remove **************************************************************** + + removes the class entry wth the specified name in the classes hashtable, + furthermore the class' resources are freed + if there is no such class false is returned + +*******************************************************************************/ + +bool class_remove(classinfo *c) +{ + classinfo *tc; /* hashtable element */ + classinfo *pc; + u4 key; /* hashkey computed from classname */ + u4 slot; /* slot in hashtable */ + u2 i; + + key = utf_hashkey(c->name->text, c->name->blength); + slot = key & (class_hash.size - 1); + tc = class_hash.ptr[slot]; + pc = NULL; + + /* search external hash-chain */ + while (tc) { + if (tc->name->blength == c->name->blength) { + + /* compare classnames */ + for (i = 0; i < c->name->blength; i++) + if (tc->name->text[i] != c->name->text[i]) + goto nomatch; + + /* class found in hashtable */ + if (!pc) { + class_hash.ptr[slot] = tc->hashlink; + + } else { + pc->hashlink = tc->hashlink; + } + + class_free(tc); + + return true; + } + + nomatch: + pc = tc; + tc = tc->hashlink; + } + + /* class not found */ + return false; +} + + /***************** Function: class_array_of *********************************** Returns an array class with the given component class. @@ -1167,12 +1309,12 @@ classinfo *class_array_of(classinfo *component) *******************************************************************************/ -classinfo *class_multiarray_of(int dim,classinfo *element) +classinfo *class_multiarray_of(int dim, classinfo *element) { int namelen; char *namebuf; - if (dim<1) + if (dim < 1) panic("Invalid array dimension requested"); /* Assemble the array class name */ @@ -1180,21 +1322,21 @@ classinfo *class_multiarray_of(int dim,classinfo *element) if (element->name->text[0] == '[') { /* the element is itself an array */ - namebuf = DMNEW(char,namelen+dim); - memcpy(namebuf+dim,element->name->text,namelen); + namebuf = DMNEW(char, namelen + dim); + memcpy(namebuf + dim, element->name->text, namelen); namelen += dim; } else { /* the element is a non-array class */ - namebuf = DMNEW(char,namelen+2+dim); + namebuf = DMNEW(char, namelen + 2 + dim); namebuf[dim] = 'L'; - memcpy(namebuf+dim+1,element->name->text,namelen); - namelen += (2+dim); - namebuf[namelen-1] = ';'; + memcpy(namebuf + dim + 1, element->name->text, namelen); + namelen += (2 + dim); + namebuf[namelen - 1] = ';'; } - memset(namebuf,'[',dim); + memset(namebuf, '[', dim); - return class_new( utf_new(namebuf,namelen) ); + return class_new(utf_new(namebuf, namelen)); } /************************** function: utf_strlen ****************************** @@ -1205,9 +1347,17 @@ classinfo *class_multiarray_of(int dim,classinfo *element) u4 utf_strlen(utf *u) { - char *endpos = utf_end(u); /* points behind utf string */ - char *utf_ptr = u->text; /* current position in utf text */ - u4 len = 0; /* number of unicode characters */ + char *endpos; /* points behind utf string */ + char *utf_ptr; /* current position in utf text */ + u4 len = 0; /* number of unicode characters */ + + if (!u) { + *exceptionptr = new_nullpointerexception(); + return 0; + } + + endpos = utf_end(u); + utf_ptr = u->text; while (utf_ptr < endpos) { len++;