From: Per Bothner Date: Thu, 12 Aug 2004 06:53:42 +0000 (-0700) Subject: javaprims.h (_Jv_Utf8Const): Change struct to a class, with private fields and access... X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=b4d49f49bf21837bef59aa30788e2e9bf2ce2e3b;p=gcc.git javaprims.h (_Jv_Utf8Const): Change struct to a class, with private fields and access methods. * gcj/javaprims.h (_Jv_Utf8Const): Change struct to a class, with private fields and access methods. (_Jv_NewStringUTF, _Jv_hashUtf8String): New function declarations. * gcj/cni.h (_Jv_NewStringUTF): Move to javaprims.h. * prims.cc (_Jv_Utf8COnst::init): New method implementation. ( _Jv_makeUtf8Const): Rewrite using new constructors. (hashUtf8String): Rename to +_Jv_hashUtf8String and make non-static. * defineclass.cc: Use new _Utf8Const access/convenience methods. * jni.cc: Likewise. * resolve.cc: Likewise. * gcj/field.h: Likewise. * include/jvm.h: Likewise. * java/lang/Class.h: Likewise. * java/lang/natClass.cc: Likwise. * java/lang/natClassLoader.cc: Likewise * java/lang/reflect/natMethod.cc: Likewise * verify.cc: Likewise. (_Jv_BytecodeVerifier::make_utf8_const): Optimize. (~_Jv_BytecodeVerifier): Don't need second _Jv_Free call. From-SVN: r85854 --- diff --git a/libjava/ChangeLog b/libjava/ChangeLog index a29fece89b8..1d2af7aba92 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,25 @@ +2004-08-09 Per Bothner + + * gcj/javaprims.h (_Jv_Utf8Const): Change struct to a class, + with private fields and access methods. + (_Jv_NewStringUTF, _Jv_hashUtf8String): New function declarations. + * gcj/cni.h (_Jv_NewStringUTF): Move to javaprims.h. + * prims.cc (_Jv_Utf8COnst::init): New method implementation. + ( _Jv_makeUtf8Const): Rewrite using new constructors. + (hashUtf8String): Rename to +_Jv_hashUtf8String and make non-static. + * defineclass.cc: Use new _Utf8Const access/convenience methods. + * jni.cc: Likewise. + * resolve.cc: Likewise. + * gcj/field.h: Likewise. + * include/jvm.h: Likewise. + * java/lang/Class.h: Likewise. + * java/lang/natClass.cc: Likwise. + * java/lang/natClassLoader.cc: Likewise + * java/lang/reflect/natMethod.cc: Likewise + * verify.cc: Likewise. + (_Jv_BytecodeVerifier::make_utf8_const): Optimize. + (~_Jv_BytecodeVerifier): Don't need second _Jv_Free call. + 2004-08-10 Andrew Haley * testsuite/libjava.lang/err14.java: New file. diff --git a/libjava/defineclass.cc b/libjava/defineclass.cc index 0648ad98287..ad40fc8f7fc 100644 --- a/libjava/defineclass.cc +++ b/libjava/defineclass.cc @@ -337,7 +337,7 @@ _Jv_ClassReader::parse () // tell everyone we're done. def->state = JV_STATE_LOADED; if (gcj::verbose_class_flag) - fprintf (stderr, "[Loaded (bytecode) %s]\n", (const char*)(def->name->data)); + fprintf (stderr, "[Loaded (bytecode) %s]\n", def->name->chars()); def->notifyAll (); } @@ -890,7 +890,7 @@ _Jv_ClassReader::handleClassBegin jstring msg = JvNewStringUTF ("loaded class "); msg = msg->concat (def->getName ()); msg = msg->concat (_Jv_NewStringUTF (" was in fact named ")); - jstring klass_name = _Jv_NewStringUTF (loadedName->data); + jstring klass_name = loadedName->toString(); msg = msg->concat (klass_name); throw_no_class_def_found_error (msg); @@ -1367,8 +1367,8 @@ void _Jv_ClassReader::throw_class_format_error (char *msg) if (def->name != NULL) { jsize mlen = strlen (msg); - unsigned char* data = (unsigned char*) def->name->data; - int ulen = def->name->length; + unsigned char* data = (unsigned char*) def->name->chars(); + int ulen = def->name->len(); unsigned char* limit = data + ulen; jsize nlen = _Jv_strLengthUtf8 ((char *) data, ulen); jsize len = nlen + mlen + 3; @@ -1500,8 +1500,8 @@ _Jv_VerifyOne (unsigned char* ptr, unsigned char* limit, bool void_ok) bool _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig) { - unsigned char* ptr = (unsigned char*) sig->data; - unsigned char* limit = ptr + sig->length; + unsigned char* ptr = (unsigned char*) sig->chars(); + unsigned char* limit = ptr + sig->len(); ptr = _Jv_VerifyOne (ptr, limit, false); @@ -1511,8 +1511,8 @@ _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig) bool _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig) { - unsigned char* ptr = (unsigned char*) sig->data; - unsigned char* limit = ptr + sig->length; + unsigned char* ptr = (unsigned char*) sig->chars(); + unsigned char* limit = ptr + sig->len(); if (ptr == limit || UTF8_GET(ptr,limit) != '(') return false; @@ -1566,8 +1566,8 @@ is_identifier_part (int c) bool _Jv_VerifyIdentifier (_Jv_Utf8Const* name) { - unsigned char *ptr = (unsigned char*) name->data; - unsigned char *limit = ptr + name->length; + unsigned char *ptr = (unsigned char*) name->chars(); + unsigned char *limit = (unsigned char*) name->limit(); int ch; if ((ch = UTF8_GET (ptr, limit))==-1 @@ -1622,8 +1622,7 @@ _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length) bool _Jv_VerifyClassName (_Jv_Utf8Const *name) { - return _Jv_VerifyClassName ((unsigned char*)&name->data[0], - (_Jv_ushort) name->length); + return _Jv_VerifyClassName ((unsigned char*)name->chars(), name->len()); } /* Returns true, if NAME1 and NAME2 represent classes in the same @@ -1631,8 +1630,8 @@ _Jv_VerifyClassName (_Jv_Utf8Const *name) bool _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2) { - unsigned char* ptr1 = (unsigned char*) name1->data; - unsigned char* limit1 = ptr1 + name1->length; + unsigned char* ptr1 = (unsigned char*) name1->chars(); + unsigned char* limit1 = (unsigned char*) name1->limit(); unsigned char* last1 = ptr1; @@ -1648,20 +1647,19 @@ _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2) } // Now the length of NAME1's package name is LEN. - int len = last1 - (unsigned char*) name1->data; + int len = last1 - (unsigned char*) name1->chars(); // If this is longer than NAME2, then we're off. - if (len > name2->length) + if (len > name2->len()) return false; // Then compare the first len bytes for equality. - if (memcmp ((void*) name1->data, (void*) name2->data, len) == 0) + if (memcmp ((void*) name1->chars(), (void*) name2->chars(), len) == 0) { // Check that there are no .'s after position LEN in NAME2. - unsigned char* ptr2 = (unsigned char*) name2->data + len; - unsigned char* limit2 = - (unsigned char*) name2->data + name2->length; + unsigned char* ptr2 = (unsigned char*) name2->chars() + len; + unsigned char* limit2 = (unsigned char*) name2->limit(); while (ptr2 < limit2) { diff --git a/libjava/gcj/cni.h b/libjava/gcj/cni.h index b9ee3829ce1..ee39738d228 100644 --- a/libjava/gcj/cni.h +++ b/libjava/gcj/cni.h @@ -20,7 +20,6 @@ details. */ #include -extern "C" jstring _Jv_NewStringUTF (const char *bytes); extern "C" void _Jv_InitClass (jclass); extern "C" void *_Jv_AllocBytes (jsize size) __attribute__((__malloc__)); diff --git a/libjava/gcj/field.h b/libjava/gcj/field.h index 70c5901417b..bd6b2c38046 100644 --- a/libjava/gcj/field.h +++ b/libjava/gcj/field.h @@ -68,7 +68,7 @@ struct _Jv_Field { if (!isResolved ()) { - char first = ((_Jv_Utf8Const*)type)->data[0]; + char first = ((_Jv_Utf8Const*)type)->first(); return first == '[' || first == 'L'; } else diff --git a/libjava/gcj/javaprims.h b/libjava/gcj/javaprims.h index 9cf2741cd18..09e0c5534c7 100644 --- a/libjava/gcj/javaprims.h +++ b/libjava/gcj/javaprims.h @@ -472,10 +472,13 @@ extern jint _Jv_FormatInt (jchar* bufend, jint num); extern "C" jchar* _Jv_GetStringChars (jstring str); extern "C" void _Jv_MonitorEnter (jobject); extern "C" void _Jv_MonitorExit (jobject); +extern "C" jstring _Jv_NewStringUTF (const char *bytes) + __attribute__((__malloc__)); extern "C" jstring _Jv_NewStringLatin1(const char*, jsize) __attribute__((__malloc__)); extern "C" jsize _Jv_GetStringUTFLength (jstring); extern "C" jsize _Jv_GetStringUTFRegion (jstring, jsize, jsize, char *); +extern "C" jint _Jv_hashUtf8String (char*, int); extern jint _Jv_CreateJavaVM (void* /*vm_args*/); @@ -500,11 +503,40 @@ typedef unsigned short _Jv_ushort __attribute__((__mode__(__HI__))); typedef unsigned int _Jv_uint __attribute__((__mode__(__SI__))); typedef unsigned int _Jv_ulong __attribute__((__mode__(__DI__))); -struct _Jv_Utf8Const +class _Jv_Utf8Const { _Jv_ushort hash; _Jv_ushort length; /* In bytes, of data portion, without final '\0'. */ char data[1]; /* In Utf8 format, with final '\0'. */ + public: + /** Return same value of java.lang.String's hashCode. */ + jint hash32() { return _Jv_hashUtf8String(data, length); } + /** Return a hash code that has at least 16 bits of information. */ + _Jv_ushort hash16 () { return hash; } + /** Return a hash code that has at least 8 bits of information. */ + _Jv_ushort hash8 () { return hash; } + /** Length in bytes of the UTF8-encoding. */ + _Jv_ushort len () const { return length; } + /** Pointer to the first byte in the NUL-terminated UTF8-encoding. */ + char* chars() { return data; } + /** Pointer to the NUL byte that terminated the UTF8-encoding. */ + char* limit() { return data+length; } + /** Return the first byte in the UTF8-encoding. */ + char first() const { return data[0]; } + /** Create a (non-interned) java.lang.String from this UTF8Const. */ + jstring toString() { return _Jv_NewStringUTF(data); } + /** Given an UTF8 string, how many bytes needed for a UTF8Const, + including struct header, and final NUL. I.e. what to pas to malloc. */ + static int space_needed (char *, int len) + { return sizeof (_Jv_Utf8Const) + len + 1; } + /** Given an allocated _Jv_Utf8Const, copy / fill it in. */ + void init (char *s, int len); + friend jboolean _Jv_equalUtf8Consts (const _Jv_Utf8Const*, const _Jv_Utf8Const *); + friend jboolean _Jv_equal (_Jv_Utf8Const*, jstring, jint); + friend jboolean _Jv_equaln (_Jv_Utf8Const*, jstring, jint); + friend _Jv_Utf8Const *_Jv_makeUtf8Const (char*, int); + friend _Jv_Utf8Const *_Jv_makeUtf8Const (jstring); + friend jstring _Jv_NewStringUtf8Const (_Jv_Utf8Const*); }; diff --git a/libjava/include/jvm.h b/libjava/include/jvm.h index b8592772923..554e3459a47 100644 --- a/libjava/include/jvm.h +++ b/libjava/include/jvm.h @@ -352,7 +352,7 @@ static inline jboolean _Jv_isVirtualMethod (_Jv_Method *meth) { using namespace java::lang::reflect; return (((meth->accflags & (Modifier::STATIC | Modifier::PRIVATE)) == 0) - && meth->name->data[0] != '<'); + && meth->name->first() != '<'); } // This function is used to determine the hash code of an object. diff --git a/libjava/java/lang/Class.h b/libjava/java/lang/Class.h index 8d2b4a0cf24..f6c6eec510d 100644 --- a/libjava/java/lang/Class.h +++ b/libjava/java/lang/Class.h @@ -212,7 +212,7 @@ public: inline jboolean isArray (void) { - return name->data[0] == '['; + return name->first() == '['; } inline jclass getComponentType (void) diff --git a/libjava/java/lang/natClass.cc b/libjava/java/lang/natClass.cc index 1b1a32d1bab..f68f2a9a74d 100644 --- a/libjava/java/lang/natClass.cc +++ b/libjava/java/lang/natClass.cc @@ -83,7 +83,7 @@ java::lang::Class::forName (jstring className, jboolean initialize, throw new java::lang::ClassNotFoundException (className); jclass klass = (buffer[0] == '[' - ? _Jv_FindClassFromSignature (name->data, loader) + ? _Jv_FindClassFromSignature (name->chars(), loader) : _Jv_FindClass (name, loader)); if (klass == NULL) @@ -443,10 +443,7 @@ java::lang::Class::getDeclaredMethods (void) jstring java::lang::Class::getName (void) { - char buffer[name->length + 1]; - memcpy (buffer, name->data, name->length); - buffer[name->length] = '\0'; - return _Jv_NewStringUTF (buffer); + return name->toString(); } JArray * @@ -889,7 +886,7 @@ _Jv_FindMethodInCache (jclass klass, _Jv_Utf8Const *name, _Jv_Utf8Const *signature) { - int index = name->hash & MCACHE_SIZE; + int index = name->hash16() & MCACHE_SIZE; _Jv_mcache *mc = method_cache + index; _Jv_Method *m = mc->method; @@ -907,7 +904,7 @@ _Jv_AddMethodToCache (jclass klass, { _Jv_MonitorEnter (&java::lang::Class::class$); - int index = method->name->hash & MCACHE_SIZE; + int index = method->name->hash16() & MCACHE_SIZE; method_cache[index].method = method; method_cache[index].klass = klass; @@ -1285,9 +1282,9 @@ _Jv_GenerateITable (jclass klass, _Jv_ifaces *ifaces, jshort *itable_offsets) jstring _Jv_GetMethodString (jclass klass, _Jv_Utf8Const *name) { - jstring r = JvNewStringUTF (klass->name->data); + jstring r = klass->name->toString(); r = r->concat (JvNewStringUTF (".")); - r = r->concat (JvNewStringUTF (name->data)); + r = r->concat (name->toString()); return r; } @@ -1329,7 +1326,7 @@ _Jv_AppendPartialITable (jclass klass, jclass iface, void **itable, break; } - if (meth && (meth->name->data[0] == '<')) + if (meth && (meth->name->first() == '<')) { // leave a placeholder in the itable for hidden init methods. itable[pos] = NULL; @@ -1588,8 +1585,7 @@ _Jv_LinkSymbolTable(jclass klass) // We're looking for a field or a method, and we can tell // which is needed by looking at the signature. - if (signature->length >= 2 - && signature->data[0] == '(') + if (signature->first() == '(' && signature->len() >= 2) { // If the target class does not have a vtable_method_count yet, // then we can't tell the offsets for its methods, so we must lay @@ -1683,8 +1679,7 @@ _Jv_LinkSymbolTable(jclass klass) // We're looking for a static field or a static method, and we // can tell which is needed by looking at the signature. - if (signature->length >= 2 - && signature->data[0] == '(') + if (signature->first() == '(' && signature->len() >= 2) { // If the target class does not have a vtable_method_count yet, // then we can't tell the offsets for its methods, so we must lay @@ -1695,7 +1690,7 @@ _Jv_LinkSymbolTable(jclass klass) _Jv_LayoutVTableMethods (target_class); } - meth = _Jv_LookupDeclaredMethod(target_class, sym.name, + meth = _Jv_LookupDeclaredMethod(target_class, sym.name, sym.signature); if (meth != NULL) @@ -1829,8 +1824,7 @@ _Jv_LayoutVTableMethods (jclass klass) superclass = _Jv_FindClass (name, klass->loader); if (! superclass) { - jstring str = _Jv_NewStringUTF (name->data); - throw new java::lang::NoClassDefFoundError (str); + throw new java::lang::NoClassDefFoundError (name->toString()); } } else diff --git a/libjava/java/lang/natClassLoader.cc b/libjava/java/lang/natClassLoader.cc index b38c2db1625..1ff47b4b54d 100644 --- a/libjava/java/lang/natClassLoader.cc +++ b/libjava/java/lang/natClassLoader.cc @@ -52,7 +52,7 @@ _Jv_WaitForState (jclass klass, int state) { klass->state = JV_STATE_LOADED; if (gcj::verbose_class_flag) - fprintf (stderr, "[Loaded (pre-compiled) %s]\n", (const char*)(klass->name->data)); + fprintf (stderr, "[Loaded (pre-compiled) %s]\n", klass->name->chars()); } if (state == JV_STATE_LINKED) { @@ -110,15 +110,15 @@ _Jv_PrepareCompiledClass (jclass klass) _Jv_Utf8Const *name = pool->data[index].utf8; jclass found; - if (name->data[0] == '[') - found = _Jv_FindClassFromSignature (&name->data[0], + if (name->first() == '[') + found = _Jv_FindClassFromSignature (name->chars(), klass->loader); else found = _Jv_FindClass (name, klass->loader); if (! found) { - jstring str = _Jv_NewStringUTF (name->data); + jstring str = name->toString(); throw new java::lang::NoClassDefFoundError (str); } @@ -182,7 +182,7 @@ _Jv_PrepareCompiledClass (jclass klass) if (state == JV_STATE_COMPILED && gcj::verbose_class_flag) fprintf (stderr, "[Loaded (pre-compiled) %s]\n", - (const char*)(klass->name->data)); + klass->name->chars()); klass->notifyAll (); @@ -207,7 +207,7 @@ _Jv_PrepareCompiledClass (jclass klass) #define HASH_LEN 1013 // Hash function for Utf8Consts. -#define HASH_UTF(Utf) (((Utf)->hash) % HASH_LEN) +#define HASH_UTF(Utf) ((Utf)->hash16() % HASH_LEN) struct _Jv_LoaderInfo { @@ -348,7 +348,7 @@ _Jv_RegisterClassHookDefault (jclass klass) // We size-limit MESSAGE so that you can't trash the stack. char message[200]; strcpy (message, TEXT); - strncpy (message + sizeof (TEXT) - 1, klass->name->data, + strncpy (message + sizeof (TEXT) - 1, klass->name->chars(), sizeof (message) - sizeof (TEXT)); message[sizeof (message) - 1] = '\0'; if (! gcj::runtimeInitialized) @@ -390,7 +390,7 @@ _Jv_FindClass (_Jv_Utf8Const *name, java::lang::ClassLoader *loader) if (! klass) { - jstring sname = _Jv_NewStringUTF (name->data); + jstring sname = name->toString(); java::lang::ClassLoader *sys = java::lang::ClassLoader::getSystemClassLoader (); @@ -470,7 +470,7 @@ _Jv_NewArrayClass (jclass element, java::lang::ClassLoader *loader, len = 3; } else - len = element->name->length + 5; + len = element->name->len() + 5; { char signature[len]; @@ -483,8 +483,8 @@ _Jv_NewArrayClass (jclass element, java::lang::ClassLoader *loader, } else { - size_t length = element->name->length; - const char *const name = element->name->data; + size_t length = element->name->len(); + const char *const name = element->name->chars(); if (name[0] != '[') signature[index++] = 'L'; memcpy (&signature[index], name, length); diff --git a/libjava/java/lang/reflect/natMethod.cc b/libjava/java/lang/reflect/natMethod.cc index 8f9efdbb53b..58b1cf6fecd 100644 --- a/libjava/java/lang/reflect/natMethod.cc +++ b/libjava/java/lang/reflect/natMethod.cc @@ -246,7 +246,7 @@ _Jv_GetTypesFromSignature (jmethodID method, _Jv_Utf8Const* sig = method->signature; java::lang::ClassLoader *loader = declaringClass->getClassLoaderInternal(); - char *ptr = sig->data; + char *ptr = sig->chars(); int numArgs = 0; /* First just count the number of parameters. */ for (; ; ptr++) @@ -283,7 +283,7 @@ _Jv_GetTypesFromSignature (jmethodID method, JArray *args = (JArray *) JvNewObjectArray (numArgs, &java::lang::Class::class$, NULL); jclass* argPtr = elements (args); - for (ptr = sig->data; *ptr != '\0'; ptr++) + for (ptr = sig->chars(); *ptr != '\0'; ptr++) { int num_arrays = 0; jclass type; diff --git a/libjava/jni.cc b/libjava/jni.cc index f5a66d0948d..7943fe0ef04 100644 --- a/libjava/jni.cc +++ b/libjava/jni.cc @@ -1880,8 +1880,8 @@ _Jv_JNI_RegisterNatives (JNIEnv *env, jclass klass, { _Jv_Method *self = &imeths[i]; - if (! strcmp (self->name->data, methods[j].name) - && ! strcmp (self->signature->data, methods[j].signature)) + if (! strcmp (self->name->chars (), methods[j].name) + && ! strcmp (self->signature->chars (), methods[j].signature)) { if (! (self->accflags & java::lang::reflect::Modifier::NATIVE)) @@ -1985,8 +1985,8 @@ mangled_name (jclass klass, _Jv_Utf8Const *func_name, // Don't use add_char because we need a literal `_'. buf[here++] = '_'; - const unsigned char *fn = (const unsigned char *) func_name->data; - const unsigned char *limit = fn + func_name->length; + const unsigned char *fn = (const unsigned char *) func_name->chars (); + const unsigned char *limit = fn + func_name->len (); for (int i = 0; ; ++i) { int ch = UTF8_GET (fn, limit); @@ -2000,8 +2000,8 @@ mangled_name (jclass klass, _Jv_Utf8Const *func_name, buf[here++] = '_'; buf[here++] = '_'; - const unsigned char *sig = (const unsigned char *) signature->data; - limit = sig + signature->length; + const unsigned char *sig = (const unsigned char *) signature->chars (); + limit = sig + signature->len (); JvAssert (sig[0] == '('); ++sig; while (1) @@ -2058,7 +2058,9 @@ extern "C" void * _Jv_LookupJNIMethod (jclass klass, _Jv_Utf8Const *name, _Jv_Utf8Const *signature, MAYBE_UNUSED int args_size) { - char buf[10 + 6 * (name->length + signature->length) + 12]; + int name_length = name->len(); + int sig_length = signature->len(); + char buf[10 + 6 * (name_length + sig_length) + 12]; int long_start; void *function; @@ -2066,13 +2068,13 @@ _Jv_LookupJNIMethod (jclass klass, _Jv_Utf8Const *name, JvSynchronize sync (global_ref_table); // First see if we have an override in the hash table. - strncpy (buf, name->data, name->length); - buf[name->length] = '\0'; - strncpy (buf + name->length + 1, signature->data, signature->length); - buf[name->length + signature->length + 1] = '\0'; + strncpy (buf, name->chars (), name_length); + buf[name_length] = '\0'; + strncpy (buf + name_length + 1, signature->chars (), sig_length); + buf[name_length + sig_length + 1] = '\0'; JNINativeMethod meth; meth.name = buf; - meth.signature = buf + name->length + 1; + meth.signature = buf + name_length + 1; function = nathash_find (&meth); if (function != NULL) return function; @@ -2139,7 +2141,7 @@ _Jv_LookupJNIMethod (jclass klass, _Jv_Utf8Const *name, if (function == NULL) #endif /* WIN32 */ { - jstring str = JvNewStringUTF (name->data); + jstring str = JvNewStringUTF (name->chars ()); throw new java::lang::UnsatisfiedLinkError (str); } } diff --git a/libjava/prims.cc b/libjava/prims.cc index 0346b02371b..7f3276e77d8 100644 --- a/libjava/prims.cc +++ b/libjava/prims.cc @@ -255,8 +255,8 @@ _Jv_strLengthUtf8(char* str, int len) /* Calculate a hash value for a string encoded in Utf8 format. * This returns the same hash value as specified or java.lang.String.hashCode. */ -static jint -hashUtf8String (char* str, int len) +jint +_Jv_hashUtf8String (char* str, int len) { unsigned char* ptr = (unsigned char*) str; unsigned char* limit = ptr + len; @@ -272,17 +272,24 @@ hashUtf8String (char* str, int len) return hash; } +void +_Jv_Utf8Const::init(char *s, int len) +{ + ::memcpy (data, s, len); + data[len] = 0; + length = len; + hash = _Jv_hashUtf8String (s, len) & 0xFFFF; +} + _Jv_Utf8Const * _Jv_makeUtf8Const (char* s, int len) { if (len < 0) len = strlen (s); - Utf8Const* m = (Utf8Const*) _Jv_AllocBytes (sizeof(Utf8Const) + len + 1); - memcpy (m->data, s, len); - m->data[len] = 0; - m->length = len; - m->hash = hashUtf8String (s, len) & 0xFFFF; - return (m); + Utf8Const* m + = (Utf8Const*) _Jv_AllocBytes (_Jv_Utf8Const::space_needed(s, len)); + m->init(s, len); + return m; } _Jv_Utf8Const * diff --git a/libjava/resolve.cc b/libjava/resolve.cc index 85c988f6758..5ebefebecef 100644 --- a/libjava/resolve.cc +++ b/libjava/resolve.cc @@ -43,7 +43,7 @@ _Jv_ResolveField (_Jv_Field *field, java::lang::ClassLoader *loader) if (! field->isResolved ()) { _Jv_Utf8Const *sig = (_Jv_Utf8Const*)field->type; - field->type = _Jv_FindClassFromSignature (sig->data, loader); + field->type = _Jv_FindClassFromSignature (sig->chars(), loader); field->flags &= ~_Jv_FIELD_UNRESOLVED_FLAG; } } @@ -87,15 +87,15 @@ _Jv_ResolvePoolEntry (jclass klass, int index) _Jv_Utf8Const *name = pool->data[index].utf8; jclass found; - if (name->data[0] == '[') - found = _Jv_FindClassFromSignature (&name->data[0], + if (name->first() == '[') + found = _Jv_FindClassFromSignature (name->chars(), klass->loader); else found = _Jv_FindClass (name, klass->loader); if (! found) { - jstring str = _Jv_NewStringUTF (name->data); + jstring str = name->toString(); // This exception is specified in JLS 2nd Ed, section 5.1. throw new java::lang::NoClassDefFoundError (str); } @@ -154,7 +154,7 @@ _Jv_ResolvePoolEntry (jclass klass, int index) jclass field_type = 0; if (owner->loader != klass->loader) - field_type = _Jv_FindClassFromSignature (field_type_name->data, + field_type = _Jv_FindClassFromSignature (field_type_name->chars(), klass->loader); _Jv_Field* the_field = 0; @@ -204,7 +204,7 @@ _Jv_ResolvePoolEntry (jclass klass, int index) sb->append(JvNewStringLatin1("field ")); sb->append(owner->getName()); sb->append(JvNewStringLatin1(".")); - sb->append(_Jv_NewStringUTF(field_name->data)); + sb->append(field_name->toString()); sb->append(JvNewStringLatin1(" was not found.")); throw_incompatible_class_change_error(sb->toString()); } @@ -306,7 +306,7 @@ _Jv_ResolvePoolEntry (jclass klass, int index) sb->append(JvNewStringLatin1("method ")); sb->append(owner->getName()); sb->append(JvNewStringLatin1(".")); - sb->append(_Jv_NewStringUTF(method_name->data)); + sb->append(method_name->toString()); sb->append(JvNewStringLatin1(" was not found.")); throw new java::lang::NoSuchMethodError (sb->toString()); } @@ -356,8 +356,8 @@ _Jv_SearchMethodInClass (jclass cls, jclass klass, sb->append(JvNewStringLatin1(": ")); sb->append(cls->getName()); sb->append(JvNewStringLatin1(".")); - sb->append(_Jv_NewStringUTF(method_name->data)); - sb->append(_Jv_NewStringUTF(method_signature->data)); + sb->append(method_name->toString()); + sb->append(method_signature->toString()); throw new java::lang::IllegalAccessError (sb->toString()); } } @@ -376,7 +376,7 @@ _Jv_PrepareMissingMethods (jclass base, jclass iface_class) { _Jv_Method *meth = &iface_class->interfaces[i]->methods[j]; // Don't bother with . - if (meth->name->data[0] == '<') + if (meth->name->first() == '<') continue; _Jv_Method *new_meth = _Jv_LookupDeclaredMethod (base, meth->name, meth->signature); @@ -859,7 +859,7 @@ int _Jv_count_arguments (_Jv_Utf8Const *signature, jboolean staticp) { - unsigned char *ptr = (unsigned char*) signature->data; + unsigned char *ptr = (unsigned char*) signature->chars(); int arg_count = staticp ? 0 : 1; /* first, count number of arguments */ @@ -890,7 +890,7 @@ init_cif (_Jv_Utf8Const* signature, ffi_type **arg_types, ffi_type **rtype_p) { - unsigned char *ptr = (unsigned char*) signature->data; + unsigned char *ptr = (unsigned char*) signature->chars(); int arg_index = 0; // arg number int item_count = 0; // stack-item count @@ -923,7 +923,7 @@ init_cif (_Jv_Utf8Const* signature, ffi_type *rtype = get_ffi_type_from_signature (ptr); ptr = skip_one_type (ptr); - if (ptr != (unsigned char*)signature->data + signature->length) + if (ptr != (unsigned char*)signature->chars() + signature->len()) throw_internal_error ("did not find end of signature"); if (ffi_prep_cif (cif, FFI_DEFAULT_ABI, diff --git a/libjava/verify.cc b/libjava/verify.cc index 7ba2ba4daee..85a50aaba67 100644 --- a/libjava/verify.cc +++ b/libjava/verify.cc @@ -179,28 +179,21 @@ private: // This method. _Jv_InterpMethod *current_method; - // A linked list of utf8 objects we allocate. This is really ugly, - // but without this our utf8 objects would be collected. + // A linked list of utf8 objects we allocate. linked<_Jv_Utf8Const> *utf8_list; // A linked list of all ref_intersection objects we allocate. ref_intersection *isect_list; // Create a new Utf-8 constant and return it. We do this to avoid - // having our Utf-8 constants prematurely collected. FIXME this is - // ugly. + // having our Utf-8 constants prematurely collected. _Jv_Utf8Const *make_utf8_const (char *s, int len) { - _Jv_Utf8Const *val = _Jv_makeUtf8Const (s, len); - _Jv_Utf8Const *r = (_Jv_Utf8Const *) _Jv_Malloc (sizeof (_Jv_Utf8Const) - + val->length - + 1); - r->length = val->length; - r->hash = val->hash; - memcpy (r->data, val->data, val->length + 1); - - linked<_Jv_Utf8Const> *lu - = (linked<_Jv_Utf8Const> *) _Jv_Malloc (sizeof (linked<_Jv_Utf8Const>)); + linked<_Jv_Utf8Const> *lu = (linked<_Jv_Utf8Const> *) + _Jv_Malloc (sizeof (linked<_Jv_Utf8Const>) + + _Jv_Utf8Const::space_needed(s, len)); + _Jv_Utf8Const *r = (_Jv_Utf8Const *) (lu + 1); + r->init(s, len); lu->val = r; lu->next = utf8_list; utf8_list = lu; @@ -226,9 +219,9 @@ private: buf->append (JvNewStringLatin1 (" in ")); buf->append (current_class->getName()); buf->append ((jchar) ':'); - buf->append (JvNewStringUTF (method->get_method()->name->data)); + buf->append (method->get_method()->name->toString()); buf->append ((jchar) '('); - buf->append (JvNewStringUTF (method->get_method()->signature->data)); + buf->append (method->get_method()->signature->toString()); buf->append ((jchar) ')'); buf->append (JvNewStringLatin1 (": ")); @@ -372,9 +365,8 @@ private: java::lang::ClassLoader *loader = verifier->current_class->getClassLoaderInternal(); // We might see either kind of name. Sigh. - if (data.name->data[0] == 'L' - && data.name->data[data.name->length - 1] == ';') - data.klass = _Jv_FindClassFromSignature (data.name->data, loader); + if (data.name->first() == 'L' && data.name->limit()[-1] == ';') + data.klass = _Jv_FindClassFromSignature (data.name->chars(), loader); else data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name), false, loader); @@ -422,7 +414,7 @@ private: if (is_resolved) return data.klass->isArray (); else - return data.name->data[0] == '['; + return data.name->first() == '['; } bool isinterface (_Jv_BytecodeVerifier *verifier) @@ -463,7 +455,7 @@ private: } else { - char *p = data.name->data; + char *p = data.name->chars(); while (*p++ == '[') ++ndims; } @@ -1992,9 +1984,9 @@ private: &name, &field_type); if (class_type) *class_type = ct; - if (field_type->data[0] == '[' || field_type->data[0] == 'L') + if (field_type->first() == '[' || field_type->first() == 'L') return type (field_type, this); - return get_type_val_for_signature (field_type->data[0]); + return get_type_val_for_signature (field_type->first()); } type check_method_constant (int index, bool is_interface, @@ -2050,7 +2042,8 @@ private: void compute_argument_types (_Jv_Utf8Const *signature, type *types) { - char *p = signature->data; + char *p = signature->chars(); + // Skip `('. ++p; @@ -2061,7 +2054,7 @@ private: type compute_return_type (_Jv_Utf8Const *signature) { - char *p = signature->data; + char *p = signature->chars(); while (*p != ')') ++p; ++p; @@ -2845,7 +2838,7 @@ private: if (opcode != op_invokespecial) verify_fail ("can't invoke "); } - else if (method_name->data[0] == '<') + else if (method_name->first() == '<') verify_fail ("can't invoke method starting with `<'"); // Pop arguments and check types. @@ -3060,7 +3053,7 @@ public: // We just print the text as utf-8. This is just for debugging // anyway. debug_print ("--------------------------------\n"); - debug_print ("-- Verifying method `%s'\n", m->self->name->data); + debug_print ("-- Verifying method `%s'\n", m->self->name->chars()); current_method = m; bytecode = m->bytecode (); @@ -3081,7 +3074,6 @@ public: while (utf8_list != NULL) { linked<_Jv_Utf8Const> *n = utf8_list->next; - _Jv_Free (utf8_list->val); _Jv_Free (utf8_list); utf8_list = n; }