1 /* |
1 /* |
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
478 }; |
478 }; |
479 #endif |
479 #endif |
480 |
480 |
481 |
481 |
482 // -------------------------------------------------------------------------- |
482 // -------------------------------------------------------------------------- |
483 |
|
484 |
|
485 // Compute the hash value for a java.lang.String object which would |
|
486 // contain the characters passed in. This hash value is used for at |
|
487 // least two purposes. |
|
488 // |
|
489 // (a) As the hash value used by the StringTable for bucket selection |
|
490 // and comparison (stored in the HashtableEntry structures). This |
|
491 // is used in the String.intern() method. |
|
492 // |
|
493 // (b) As the hash value used by the String object itself, in |
|
494 // String.hashCode(). This value is normally calculate in Java code |
|
495 // in the String.hashCode method(), but is precomputed for String |
|
496 // objects in the shared archive file. |
|
497 // |
|
498 // For this reason, THIS ALGORITHM MUST MATCH String.hashCode(). |
|
499 |
|
500 int StringTable::hash_string(jchar* s, int len) { |
|
501 unsigned h = 0; |
|
502 while (len-- > 0) { |
|
503 h = 31*h + (unsigned) *s; |
|
504 s++; |
|
505 } |
|
506 return h; |
|
507 } |
|
508 |
|
509 |
|
510 StringTable* StringTable::_the_table = NULL; |
483 StringTable* StringTable::_the_table = NULL; |
511 |
484 |
512 oop StringTable::lookup(int index, jchar* name, |
485 oop StringTable::lookup(int index, jchar* name, |
513 int len, unsigned int hash) { |
486 int len, unsigned int hash) { |
514 for (HashtableEntry<oop>* l = bucket(index); l != NULL; l = l->next()) { |
487 for (HashtableEntry<oop>* l = bucket(index); l != NULL; l = l->next()) { |
559 |
532 |
560 oop StringTable::lookup(Symbol* symbol) { |
533 oop StringTable::lookup(Symbol* symbol) { |
561 ResourceMark rm; |
534 ResourceMark rm; |
562 int length; |
535 int length; |
563 jchar* chars = symbol->as_unicode(length); |
536 jchar* chars = symbol->as_unicode(length); |
564 unsigned int hashValue = hash_string(chars, length); |
537 unsigned int hashValue = java_lang_String::hash_string(chars, length); |
565 int index = the_table()->hash_to_index(hashValue); |
538 int index = the_table()->hash_to_index(hashValue); |
566 return the_table()->lookup(index, chars, length, hashValue); |
539 return the_table()->lookup(index, chars, length, hashValue); |
567 } |
540 } |
568 |
541 |
569 |
542 |
570 oop StringTable::intern(Handle string_or_null, jchar* name, |
543 oop StringTable::intern(Handle string_or_null, jchar* name, |
571 int len, TRAPS) { |
544 int len, TRAPS) { |
572 unsigned int hashValue = hash_string(name, len); |
545 unsigned int hashValue = java_lang_String::hash_string(name, len); |
573 int index = the_table()->hash_to_index(hashValue); |
546 int index = the_table()->hash_to_index(hashValue); |
574 oop string = the_table()->lookup(index, name, len, hashValue); |
547 oop string = the_table()->lookup(index, name, len, hashValue); |
575 |
548 |
576 // Found |
549 // Found |
577 if (string != NULL) return string; |
550 if (string != NULL) return string; |
661 HashtableEntry<oop>* p = the_table()->bucket(i); |
634 HashtableEntry<oop>* p = the_table()->bucket(i); |
662 for ( ; p != NULL; p = p->next()) { |
635 for ( ; p != NULL; p = p->next()) { |
663 oop s = p->literal(); |
636 oop s = p->literal(); |
664 guarantee(s != NULL, "interned string is NULL"); |
637 guarantee(s != NULL, "interned string is NULL"); |
665 guarantee(s->is_perm() || !JavaObjectsInPerm, "interned string not in permspace"); |
638 guarantee(s->is_perm() || !JavaObjectsInPerm, "interned string not in permspace"); |
666 |
639 unsigned int h = java_lang_String::hash_string(s); |
667 int length; |
|
668 jchar* chars = java_lang_String::as_unicode_string(s, length); |
|
669 unsigned int h = hash_string(chars, length); |
|
670 guarantee(p->hash() == h, "broken hash in string table entry"); |
640 guarantee(p->hash() == h, "broken hash in string table entry"); |
671 guarantee(the_table()->hash_to_index(h) == i, |
641 guarantee(the_table()->hash_to_index(h) == i, |
672 "wrong index in string table"); |
642 "wrong index in string table"); |
673 } |
643 } |
674 } |
644 } |