hashtable.cpp revision 0
0N/A/*
0N/A * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A *
0N/A */
0N/A
0N/A# include "incls/_precompiled.incl"
0N/A# include "incls/_hashtable.cpp.incl"
0N/A
0N/AHS_DTRACE_PROBE_DECL4(hs_private, hashtable__new_entry,
0N/A void*, unsigned int, oop, void*);
0N/A
0N/A// This is a generic hashtable, designed to be used for the symbol
0N/A// and string tables.
0N/A//
0N/A// It is implemented as an open hash table with a fixed number of buckets.
0N/A//
0N/A// %note:
0N/A// - HashtableEntrys are allocated in blocks to reduce the space overhead.
0N/A
0N/ABasicHashtableEntry* BasicHashtable::new_entry(unsigned int hashValue) {
0N/A BasicHashtableEntry* entry;
0N/A
0N/A if (_free_list) {
0N/A entry = _free_list;
0N/A _free_list = _free_list->next();
0N/A } else {
0N/A const int block_size = 500;
0N/A if (_first_free_entry == _end_block) {
0N/A int len = _entry_size * block_size;
0N/A _first_free_entry = NEW_C_HEAP_ARRAY(char, len);
0N/A _end_block = _first_free_entry + len;
0N/A }
0N/A entry = (BasicHashtableEntry*)_first_free_entry;
0N/A _first_free_entry += _entry_size;
0N/A }
0N/A
0N/A entry->set_hash(hashValue);
0N/A return entry;
0N/A}
0N/A
0N/A
0N/AHashtableEntry* Hashtable::new_entry(unsigned int hashValue, oop obj) {
0N/A HashtableEntry* entry;
0N/A
0N/A entry = (HashtableEntry*)BasicHashtable::new_entry(hashValue);
0N/A entry->set_literal(obj); // clears literal string field
0N/A HS_DTRACE_PROBE4(hs_private, hashtable__new_entry,
0N/A this, hashValue, obj, entry);
0N/A return entry;
0N/A}
0N/A
0N/A
0N/A// GC support
0N/A
0N/Avoid Hashtable::unlink(BoolObjectClosure* is_alive) {
0N/A // Readers of the table are unlocked, so we should only be removing
0N/A // entries at a safepoint.
0N/A assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
0N/A for (int i = 0; i < table_size(); ++i) {
0N/A for (HashtableEntry** p = bucket_addr(i); *p != NULL; ) {
0N/A HashtableEntry* entry = *p;
0N/A if (entry->is_shared()) {
0N/A break;
0N/A }
0N/A assert(entry->literal() != NULL, "just checking");
0N/A if (is_alive->do_object_b(entry->literal())) {
0N/A p = entry->next_addr();
0N/A } else {
0N/A *p = entry->next();
0N/A free_entry(entry);
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid Hashtable::oops_do(OopClosure* f) {
0N/A for (int i = 0; i < table_size(); ++i) {
0N/A HashtableEntry** p = bucket_addr(i);
0N/A HashtableEntry* entry = bucket(i);
0N/A while (entry != NULL) {
0N/A f->do_oop(entry->literal_addr());
0N/A
0N/A // Did the closure remove the literal from the table?
0N/A if (entry->literal() == NULL) {
0N/A assert(!entry->is_shared(), "immutable hashtable entry?");
0N/A *p = entry->next();
0N/A free_entry(entry);
0N/A } else {
0N/A p = entry->next_addr();
0N/A }
0N/A entry = (HashtableEntry*)HashtableEntry::make_ptr(*p);
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A// Reverse the order of elements in the hash buckets.
0N/A
0N/Avoid BasicHashtable::reverse() {
0N/A
0N/A for (int i = 0; i < _table_size; ++i) {
0N/A BasicHashtableEntry* new_list = NULL;
0N/A BasicHashtableEntry* p = bucket(i);
0N/A while (p != NULL) {
0N/A BasicHashtableEntry* next = p->next();
0N/A p->set_next(new_list);
0N/A new_list = p;
0N/A p = next;
0N/A }
0N/A *bucket_addr(i) = new_list;
0N/A }
0N/A}
0N/A
0N/A
0N/A// Copy the table to the shared space.
0N/A
0N/Avoid BasicHashtable::copy_table(char** top, char* end) {
0N/A
0N/A // Dump the hash table entries.
0N/A
0N/A intptr_t *plen = (intptr_t*)(*top);
0N/A *top += sizeof(*plen);
0N/A
0N/A int i;
0N/A for (i = 0; i < _table_size; ++i) {
0N/A for (BasicHashtableEntry** p = _buckets[i].entry_addr();
0N/A *p != NULL;
0N/A p = (*p)->next_addr()) {
0N/A if (*top + entry_size() > end) {
0N/A warning("\nThe shared miscellaneous data space is not large "
0N/A "enough to \npreload requested classes. Use "
0N/A "-XX:SharedMiscDataSize= to increase \nthe initial "
0N/A "size of the miscellaneous data space.\n");
0N/A exit(2);
0N/A }
0N/A *p = (BasicHashtableEntry*)memcpy(*top, *p, entry_size());
0N/A *top += entry_size();
0N/A }
0N/A }
0N/A *plen = (char*)(*top) - (char*)plen - sizeof(*plen);
0N/A
0N/A // Set the shared bit.
0N/A
0N/A for (i = 0; i < _table_size; ++i) {
0N/A for (BasicHashtableEntry* p = bucket(i); p != NULL; p = p->next()) {
0N/A p->set_shared();
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A
0N/A// Reverse the order of elements in the hash buckets.
0N/A
0N/Avoid Hashtable::reverse(void* boundary) {
0N/A
0N/A for (int i = 0; i < table_size(); ++i) {
0N/A HashtableEntry* high_list = NULL;
0N/A HashtableEntry* low_list = NULL;
0N/A HashtableEntry* last_low_entry = NULL;
0N/A HashtableEntry* p = bucket(i);
0N/A while (p != NULL) {
0N/A HashtableEntry* next = p->next();
0N/A if ((void*)p->literal() >= boundary) {
0N/A p->set_next(high_list);
0N/A high_list = p;
0N/A } else {
0N/A p->set_next(low_list);
0N/A low_list = p;
0N/A if (last_low_entry == NULL) {
0N/A last_low_entry = p;
0N/A }
0N/A }
0N/A p = next;
0N/A }
0N/A if (low_list != NULL) {
0N/A *bucket_addr(i) = low_list;
0N/A last_low_entry->set_next(high_list);
0N/A } else {
0N/A *bucket_addr(i) = high_list;
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A// Dump the hash table buckets.
0N/A
0N/Avoid BasicHashtable::copy_buckets(char** top, char* end) {
0N/A intptr_t len = _table_size * sizeof(HashtableBucket);
0N/A *(intptr_t*)(*top) = len;
0N/A *top += sizeof(intptr_t);
0N/A
0N/A *(intptr_t*)(*top) = _number_of_entries;
0N/A *top += sizeof(intptr_t);
0N/A
0N/A if (*top + len > end) {
0N/A warning("\nThe shared miscellaneous data space is not large "
0N/A "enough to \npreload requested classes. Use "
0N/A "-XX:SharedMiscDataSize= to increase \nthe initial "
0N/A "size of the miscellaneous data space.\n");
0N/A exit(2);
0N/A }
0N/A _buckets = (HashtableBucket*)memcpy(*top, _buckets, len);
0N/A *top += len;
0N/A}
0N/A
0N/A
0N/A#ifndef PRODUCT
0N/A
0N/Avoid Hashtable::print() {
0N/A ResourceMark rm;
0N/A
0N/A for (int i = 0; i < table_size(); i++) {
0N/A HashtableEntry* entry = bucket(i);
0N/A while(entry != NULL) {
0N/A tty->print("%d : ", i);
0N/A entry->literal()->print();
0N/A tty->cr();
0N/A entry = entry->next();
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid BasicHashtable::verify() {
0N/A int count = 0;
0N/A for (int i = 0; i < table_size(); i++) {
0N/A for (BasicHashtableEntry* p = bucket(i); p != NULL; p = p->next()) {
0N/A ++count;
0N/A }
0N/A }
0N/A assert(count == number_of_entries(), "number of hashtable entries incorrect");
0N/A}
0N/A
0N/A
0N/A#endif // PRODUCT
0N/A
0N/A
0N/A#ifdef ASSERT
0N/A
0N/Avoid BasicHashtable::verify_lookup_length(double load) {
0N/A if ((double)_lookup_length / (double)_lookup_count > load * 2.0) {
0N/A warning("Performance bug: SystemDictionary lookup_count=%d "
0N/A "lookup_length=%d average=%lf load=%f",
0N/A _lookup_count, _lookup_length,
0N/A (double) _lookup_length / _lookup_count, load);
0N/A }
0N/A}
0N/A
0N/A#endif