1 /* src/vm/string.c - java.lang.String related functions
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Reinhard Grafl
31 Changes: Christian Thalinger
33 $Id: string.c 2663 2005-06-13 14:20:15Z twisti $
43 #include "vm/global.h"
45 #include "mm/memory.h"
46 #include "native/include/java_lang_String.h"
47 #include "vm/exceptions.h"
48 #include "vm/loader.h"
49 #include "vm/options.h"
50 #include "vm/stringlocal.h"
54 /* global string definitions **************************************************/
56 /* exception/error super class */
58 const char *string_java_lang_Throwable =
59 "java/lang/Throwable";
61 const char *string_java_lang_VMThrowable =
62 "java/lang/VMThrowable";
65 /* specify some exception strings for code generation */
67 const char *string_java_lang_ArithmeticException =
68 "java/lang/ArithmeticException";
70 const char *string_java_lang_ArithmeticException_message =
73 const char *string_java_lang_ArrayIndexOutOfBoundsException =
74 "java/lang/ArrayIndexOutOfBoundsException";
76 const char *string_java_lang_ArrayStoreException =
77 "java/lang/ArrayStoreException";
79 const char *string_java_lang_ClassCastException =
80 "java/lang/ClassCastException";
82 const char *string_java_lang_ClassNotFoundException =
83 "java/lang/ClassNotFoundException";
85 const char *string_java_lang_CloneNotSupportedException =
86 "java/lang/CloneNotSupportedException";
88 const char *string_java_lang_Exception =
89 "java/lang/Exception";
91 const char *string_java_lang_IllegalAccessException =
92 "java/lang/IllegalAccessException";
94 const char *string_java_lang_IllegalArgumentException =
95 "java/lang/IllegalArgumentException";
97 const char *string_java_lang_IllegalMonitorStateException =
98 "java/lang/IllegalMonitorStateException";
100 const char *string_java_lang_IndexOutOfBoundsException =
101 "java/lang/IndexOutOfBoundsException";
103 const char *string_java_lang_InstantiationException =
104 "java/lang/InstantiationException";
106 const char *string_java_lang_InterruptedException =
107 "java/lang/InterruptedException";
109 const char *string_java_lang_NegativeArraySizeException =
110 "java/lang/NegativeArraySizeException";
112 const char *string_java_lang_NoSuchFieldException =
113 "java/lang/NoSuchFieldException";
115 const char *string_java_lang_NoSuchMethodException =
116 "java/lang/NoSuchMethodException";
118 const char *string_java_lang_NullPointerException =
119 "java/lang/NullPointerException";
121 const char *string_java_lang_reflect_InvocationTargetException =
122 "java/lang/reflect/InvocationTargetException";
125 /* specify some error strings for code generation */
127 const char *string_java_lang_AbstractMethodError =
128 "java/lang/AbstractMethodError";
130 const char *string_java_lang_ClassCircularityError =
131 "java/lang/ClassCircularityError";
133 const char *string_java_lang_ClassFormatError =
134 "java/lang/ClassFormatError";
136 const char *string_java_lang_Error =
139 const char *string_java_lang_ExceptionInInitializerError =
140 "java/lang/ExceptionInInitializerError";
142 const char *string_java_lang_IncompatibleClassChangeError =
143 "java/lang/IncompatibleClassChangeError";
145 const char *string_java_lang_InternalError =
146 "java/lang/InternalError";
148 const char *string_java_lang_LinkageError =
149 "java/lang/LinkageError";
151 const char *string_java_lang_NoClassDefFoundError =
152 "java/lang/NoClassDefFoundError";
154 const char *string_java_lang_NoSuchFieldError =
155 "java/lang/NoSuchFieldError";
157 const char *string_java_lang_NoSuchMethodError =
158 "java/lang/NoSuchMethodError";
160 const char *string_java_lang_OutOfMemoryError =
161 "java/lang/OutOfMemoryError";
163 const char *string_java_lang_UnsatisfiedLinkError =
164 "java/lang/UnsatisfiedLinkError";
166 const char *string_java_lang_UnsupportedClassVersionError =
167 "java/lang/UnsupportedClassVersionError";
169 const char *string_java_lang_VerifyError =
170 "java/lang/VerifyError";
172 const char *string_java_lang_VirtualMachineError =
173 "java/lang/VirtualMachineError";
176 /* stringtable_update **********************************************************
178 Traverses the javastring hashtable and sets the vftbl-entries of
179 javastrings which were temporarily set to NULL, because
180 java.lang.Object was not yet loaded.
182 *******************************************************************************/
184 void stringtable_update(void)
186 java_lang_String *js;
188 literalstring *s; /* hashtable entry */
191 for (i = 0; i < string_hash.size; i++) {
192 s = string_hash.ptr[i];
196 js = (java_lang_String *) s->string;
198 if (!js || !js->value) {
199 /* error in hashtable found */
200 log_text("invalid literalstring in hashtable");
206 if (!js->header.vftbl)
207 /* vftbl of javastring is NULL */
208 js->header.vftbl = class_java_lang_String->vftbl;
210 if (!a->header.objheader.vftbl)
211 /* vftbl of character-array is NULL */
212 a->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
214 /* follow link in external hash chain */
222 /* javastring_new **************************************************************
224 creates a new object of type java/lang/String with the text of
225 the specified utf8-string
227 return: pointer to the string or NULL if memory is exhausted.
229 *******************************************************************************/
231 java_lang_String *javastring_new(utf *u)
233 char *utf_ptr; /* current utf character in utf string */
234 u4 utflength; /* length of utf-string if uncompressed */
235 java_lang_String *s; /* result-string */
240 *exceptionptr = new_nullpointerexception();
245 utflength = utf_strlen(u);
247 s = (java_lang_String *) builtin_new(class_java_lang_String);
248 a = builtin_newarray_char(utflength);
250 /* javastring or character-array could not be created */
254 /* decompress utf-string */
255 for (i = 0; i < utflength; i++)
256 a->data[i] = utf_nextu2(&utf_ptr);
258 /* set fields of the javastring-object */
261 s->count = utflength;
266 /* javastring_new_slash_to_dot *************************************************
268 creates a new object of type java/lang/String with the text of
269 the specified utf8-string with slashes changed to dots
271 return: pointer to the string or NULL if memory is exhausted.
273 *******************************************************************************/
275 java_lang_String *javastring_new_slash_to_dot(utf *u)
277 char *utf_ptr; /* current utf character in utf string */
278 u4 utflength; /* length of utf-string if uncompressed */
279 java_lang_String *s; /* result-string */
285 *exceptionptr = new_nullpointerexception();
290 utflength = utf_strlen(u);
292 s = (java_lang_String *) builtin_new(class_java_lang_String);
293 a = builtin_newarray_char(utflength);
295 /* javastring or character-array could not be created */
299 /* decompress utf-string */
300 for (i = 0; i < utflength; i++) {
301 ch = utf_nextu2(&utf_ptr);
307 /* set fields of the javastring-object */
310 s->count = utflength;
316 /* javastring_new_char *********************************************************
318 creates a new java/lang/String object which contains the convertet
319 C-string passed via text.
321 return: the object pointer or NULL if memory is exhausted.
323 *******************************************************************************/
325 java_lang_String *javastring_new_char(const char *text)
328 s4 len; /* length of the string */
329 java_lang_String *s; /* result-string */
333 *exceptionptr = new_nullpointerexception();
339 s = (java_lang_String *) builtin_new(class_java_lang_String);
340 a = builtin_newarray_char(len);
342 /* javastring or character-array could not be created */
347 for (i = 0; i < len; i++)
348 a->data[i] = text[i];
350 /* set fields of the javastring-object */
359 /* javastring_tochar ***********************************************************
361 converts a Java string into a C string.
363 return: pointer to C string
365 Caution: calling method MUST release the allocated memory!
367 *******************************************************************************/
369 char *javastring_tochar(java_objectheader *so)
371 java_lang_String *s = (java_lang_String *) so;
384 buf = MNEW(char, s->count + 1);
386 for (i = 0; i < s->count; i++)
387 buf[i] = a->data[s->offset + i];
395 /* javastring_toutf ************************************************************
397 Make utf symbol from javastring.
399 *******************************************************************************/
401 utf *javastring_toutf(java_lang_String *string, bool isclassname)
403 java_lang_String *str = (java_lang_String *) string;
405 return utf_new_u2(str->value->data + str->offset, str->count, isclassname);
409 /* javastring_strlen ***********************************************************
411 Returns the length of the Java string.
413 *******************************************************************************/
415 s4 javastring_strlen(java_objectheader *so)
417 java_lang_String *s = (java_lang_String *) so;
426 /* literalstring_u2 ************************************************************
428 Searches for the javastring with the specified u2-array in the
429 string hashtable, if there is no such string a new one is created.
431 If copymode is true a copy of the u2-array is made.
433 *******************************************************************************/
435 java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset,
438 literalstring *s; /* hashtable element */
439 java_lang_String *js; /* u2-array wrapped in javastring */
440 java_chararray *stringdata; /* copy of u2-array */
445 /* find location in hashtable */
446 key = unicode_hashkey(a->data + offset, length);
447 slot = key & (string_hash.size - 1);
448 s = string_hash.ptr[slot];
451 js = (java_lang_String *) s->string;
453 if (length == js->count) {
455 for (i = 0; i < length; i++) {
456 if (a->data[offset + i] != js->value->data[i])
460 /* string already in hashtable, free memory */
462 mem_free(a, sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10);
464 return (java_objectheader *) js;
468 /* follow link in external hash chain */
473 /* create copy of u2-array for new javastring */
474 u4 arraysize = sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10;
475 stringdata = mem_alloc(arraysize);
476 /* memcpy(stringdata, a, arraysize); */
477 memcpy(&(stringdata->header), &(a->header), sizeof(java_arrayheader));
478 memcpy(&(stringdata->data), &(a->data) + offset, sizeof(u2) * (length - 1) + 10);
484 /* location in hashtable found, complete arrayheader */
486 stringdata->header.objheader.vftbl =
487 primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
488 stringdata->header.size = length;
490 /* XXX TWISTI: is this necessary? */
491 if (!class_java_lang_String)
492 class_java_lang_String = load_class_bootstrap(utf_java_lang_String);
494 assert(class_java_lang_String);
495 assert(class_java_lang_String->loaded);
497 /* if we use eager loading, we have to check loaded String class */
500 list_addfirst(&unlinkedclasses, class_java_lang_String);
502 /* create new javastring */
504 js = NEW(java_lang_String);
505 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
506 initObjectLock(&js->header);
508 js->header.vftbl = class_java_lang_String->vftbl;
509 js->value = stringdata;
513 /* create new literalstring */
514 s = NEW(literalstring);
515 s->hashlink = string_hash.ptr[slot];
516 s->string = (java_objectheader *) js;
517 string_hash.ptr[slot] = s;
519 /* update number of hashtable entries */
520 string_hash.entries++;
522 /* reorganization of hashtable */
523 if (string_hash.entries > (string_hash.size * 2)) {
524 /* reorganization of hashtable, average length of
525 the external chains is approx. 2 */
529 hashtable newhash; /* the new hashtable */
531 /* create new hashtable, double the size */
532 init_hashtable(&newhash, string_hash.size * 2);
533 newhash.entries = string_hash.entries;
535 /* transfer elements to new hashtable */
536 for (i = 0; i < string_hash.size; i++) {
537 s = string_hash.ptr[i];
539 literalstring *nexts = s->hashlink;
540 js = (java_lang_String *) s->string;
541 slot = unicode_hashkey(js->value->data, js->count) & (newhash.size - 1);
543 s->hashlink = newhash.ptr[slot];
544 newhash.ptr[slot] = s;
546 /* follow link in external hash chain */
551 /* dispose old table */
552 MFREE(string_hash.ptr, void*, string_hash.size);
553 string_hash = newhash;
556 return (java_objectheader *) js;
560 /* literalstring_new ***********************************************************
562 Creates a new javastring with the text of the utf-symbol and inserts it into
563 the string hashtable.
565 *******************************************************************************/
567 java_objectheader *literalstring_new(utf *u)
569 char *utf_ptr; /* pointer to current unicode character */
571 u4 utflength; /* length of utf-string if uncompressed */
572 java_chararray *a; /* u2-array constructed from utf string */
576 utflength = utf_strlen(u);
578 /* allocate memory */
579 a = mem_alloc(sizeof(java_chararray) + sizeof(u2) * (utflength - 1) + 10);
581 /* convert utf-string to u2-array */
582 for (i = 0; i < utflength; i++)
583 a->data[i] = utf_nextu2(&utf_ptr);
585 return literalstring_u2(a, utflength, 0, false);
589 /* literalstring_free **********************************************************
591 Removes a javastring from memory.
593 *******************************************************************************/
595 void literalstring_free(java_objectheader* sobj)
600 s = (java_lang_String *) sobj;
603 /* dispose memory of java.lang.String object */
604 FREE(s, java_lang_String);
606 /* dispose memory of java-characterarray */
607 FREE(a, sizeof(java_chararray) + sizeof(u2) * (a->header.size - 1)); /* +10 ?? */
612 * These are local overrides for various environment variables in Emacs.
613 * Please do not remove this and leave it at the end of the file, where
614 * Emacs will automagically detect them.
615 * ---------------------------------------------------------------------
618 * indent-tabs-mode: t