annotate src/share/vm/libadt/dict.cpp @ 20543:e7d0505c8a30

8059758: Footprint regressions with JDK-8038423 Summary: Changes in JDK-8038423 always initialize (zero out) virtual memory used for auxiliary data structures. This causes a footprint regression for G1 in startup benchmarks. This is because they do not touch that memory at all, so the operating system does not actually commit these pages. The fix is to, if the initialization value of the data structures matches the default value of just committed memory (=0), do not do anything. Reviewed-by: jwilhelm, brutisso
author tschatzl
date Fri, 10 Oct 2014 15:51:58 +0200
parents 78bbf4d43a14
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
17937
78bbf4d43a14 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 1972
diff changeset
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 628
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 628
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 628
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
25 #include "precompiled.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
26 #include "libadt/dict.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
27 #include "memory/allocation.inline.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
28 #include "memory/resourceArea.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
29 #include "runtime/thread.hpp"
0
a61af66fc99e Initial load
duke
parents:
diff changeset
30
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
31 // Dictionaries - An Abstract Data Type
0
a61af66fc99e Initial load
duke
parents:
diff changeset
32
a61af66fc99e Initial load
duke
parents:
diff changeset
33 // %%%%% includes not needed with AVM framework - Ungar
a61af66fc99e Initial load
duke
parents:
diff changeset
34
a61af66fc99e Initial load
duke
parents:
diff changeset
35 // #include "port.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
36 //IMPLEMENTATION
a61af66fc99e Initial load
duke
parents:
diff changeset
37 // #include "dict.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
38
a61af66fc99e Initial load
duke
parents:
diff changeset
39 #include <assert.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
40
17937
78bbf4d43a14 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 1972
diff changeset
41 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
78bbf4d43a14 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 1972
diff changeset
42
0
a61af66fc99e Initial load
duke
parents:
diff changeset
43 // The iostream is not needed and it gets confused for gcc by the
a61af66fc99e Initial load
duke
parents:
diff changeset
44 // define of bool.
a61af66fc99e Initial load
duke
parents:
diff changeset
45 //
a61af66fc99e Initial load
duke
parents:
diff changeset
46 // #include <iostream.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
47
a61af66fc99e Initial load
duke
parents:
diff changeset
48 //------------------------------data-----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
49 // String hash tables
a61af66fc99e Initial load
duke
parents:
diff changeset
50 #define MAXID 20
a61af66fc99e Initial load
duke
parents:
diff changeset
51 static byte initflag = 0; // True after 1st initialization
a61af66fc99e Initial load
duke
parents:
diff changeset
52 static const char shft[MAXID] = {1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6};
a61af66fc99e Initial load
duke
parents:
diff changeset
53 static short xsum[MAXID];
a61af66fc99e Initial load
duke
parents:
diff changeset
54
a61af66fc99e Initial load
duke
parents:
diff changeset
55 //------------------------------bucket---------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
56 class bucket : public ResourceObj {
a61af66fc99e Initial load
duke
parents:
diff changeset
57 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
58 uint _cnt, _max; // Size of bucket
a61af66fc99e Initial load
duke
parents:
diff changeset
59 void **_keyvals; // Array of keys and values
a61af66fc99e Initial load
duke
parents:
diff changeset
60 };
a61af66fc99e Initial load
duke
parents:
diff changeset
61
a61af66fc99e Initial load
duke
parents:
diff changeset
62 //------------------------------Dict-----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
63 // The dictionary is kept has a hash table. The hash table is a even power
a61af66fc99e Initial load
duke
parents:
diff changeset
64 // of two, for nice modulo operations. Each bucket in the hash table points
a61af66fc99e Initial load
duke
parents:
diff changeset
65 // to a linear list of key-value pairs; each key & value is just a (void *).
a61af66fc99e Initial load
duke
parents:
diff changeset
66 // The list starts with a count. A hash lookup finds the list head, then a
a61af66fc99e Initial load
duke
parents:
diff changeset
67 // simple linear scan finds the key. If the table gets too full, it's
a61af66fc99e Initial load
duke
parents:
diff changeset
68 // doubled in size; the total amount of EXTRA times all hash functions are
a61af66fc99e Initial load
duke
parents:
diff changeset
69 // computed for the doubling is no more than the current size - thus the
a61af66fc99e Initial load
duke
parents:
diff changeset
70 // doubling in size costs no more than a constant factor in speed.
a61af66fc99e Initial load
duke
parents:
diff changeset
71 Dict::Dict(CmpKey initcmp, Hash inithash) : _hash(inithash), _cmp(initcmp),
a61af66fc99e Initial load
duke
parents:
diff changeset
72 _arena(Thread::current()->resource_area()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
73 int i;
a61af66fc99e Initial load
duke
parents:
diff changeset
74
a61af66fc99e Initial load
duke
parents:
diff changeset
75 // Precompute table of null character hashes
a61af66fc99e Initial load
duke
parents:
diff changeset
76 if( !initflag ) { // Not initializated yet?
a61af66fc99e Initial load
duke
parents:
diff changeset
77 xsum[0] = (1<<shft[0])+1; // Initialize
a61af66fc99e Initial load
duke
parents:
diff changeset
78 for(i=1; i<MAXID; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
79 xsum[i] = (1<<shft[i])+1+xsum[i-1];
a61af66fc99e Initial load
duke
parents:
diff changeset
80 }
a61af66fc99e Initial load
duke
parents:
diff changeset
81 initflag = 1; // Never again
a61af66fc99e Initial load
duke
parents:
diff changeset
82 }
a61af66fc99e Initial load
duke
parents:
diff changeset
83
a61af66fc99e Initial load
duke
parents:
diff changeset
84 _size = 16; // Size is a power of 2
a61af66fc99e Initial load
duke
parents:
diff changeset
85 _cnt = 0; // Dictionary is empty
a61af66fc99e Initial load
duke
parents:
diff changeset
86 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
87 memset(_bin,0,sizeof(bucket)*_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
88 }
a61af66fc99e Initial load
duke
parents:
diff changeset
89
a61af66fc99e Initial load
duke
parents:
diff changeset
90 Dict::Dict(CmpKey initcmp, Hash inithash, Arena *arena, int size)
a61af66fc99e Initial load
duke
parents:
diff changeset
91 : _hash(inithash), _cmp(initcmp), _arena(arena) {
a61af66fc99e Initial load
duke
parents:
diff changeset
92 int i;
a61af66fc99e Initial load
duke
parents:
diff changeset
93
a61af66fc99e Initial load
duke
parents:
diff changeset
94 // Precompute table of null character hashes
a61af66fc99e Initial load
duke
parents:
diff changeset
95 if( !initflag ) { // Not initializated yet?
a61af66fc99e Initial load
duke
parents:
diff changeset
96 xsum[0] = (1<<shft[0])+1; // Initialize
a61af66fc99e Initial load
duke
parents:
diff changeset
97 for(i=1; i<MAXID; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
98 xsum[i] = (1<<shft[i])+1+xsum[i-1];
a61af66fc99e Initial load
duke
parents:
diff changeset
99 }
a61af66fc99e Initial load
duke
parents:
diff changeset
100 initflag = 1; // Never again
a61af66fc99e Initial load
duke
parents:
diff changeset
101 }
a61af66fc99e Initial load
duke
parents:
diff changeset
102
a61af66fc99e Initial load
duke
parents:
diff changeset
103 i=16;
a61af66fc99e Initial load
duke
parents:
diff changeset
104 while( i < size ) i <<= 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
105 _size = i; // Size is a power of 2
a61af66fc99e Initial load
duke
parents:
diff changeset
106 _cnt = 0; // Dictionary is empty
a61af66fc99e Initial load
duke
parents:
diff changeset
107 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
108 memset(_bin,0,sizeof(bucket)*_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
109 }
a61af66fc99e Initial load
duke
parents:
diff changeset
110
a61af66fc99e Initial load
duke
parents:
diff changeset
111 //------------------------------~Dict------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
112 // Delete an existing dictionary.
a61af66fc99e Initial load
duke
parents:
diff changeset
113 Dict::~Dict() {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
115 tty->print("~Dict %d/%d: ",_cnt,_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
116 for( uint i=0; i < _size; i++) // For complete new table do
a61af66fc99e Initial load
duke
parents:
diff changeset
117 tty->print("%d ",_bin[i]._cnt);
a61af66fc99e Initial load
duke
parents:
diff changeset
118 tty->print("\n");*/
a61af66fc99e Initial load
duke
parents:
diff changeset
119 /*for( uint i=0; i<_size; i++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
120 FREE_FAST( _bin[i]._keyvals );
a61af66fc99e Initial load
duke
parents:
diff changeset
121 } */
a61af66fc99e Initial load
duke
parents:
diff changeset
122 }
a61af66fc99e Initial load
duke
parents:
diff changeset
123
a61af66fc99e Initial load
duke
parents:
diff changeset
124 //------------------------------Clear----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
125 // Zap to empty; ready for re-use
a61af66fc99e Initial load
duke
parents:
diff changeset
126 void Dict::Clear() {
a61af66fc99e Initial load
duke
parents:
diff changeset
127 _cnt = 0; // Empty contents
a61af66fc99e Initial load
duke
parents:
diff changeset
128 for( uint i=0; i<_size; i++ )
a61af66fc99e Initial load
duke
parents:
diff changeset
129 _bin[i]._cnt = 0; // Empty buckets, but leave allocated
a61af66fc99e Initial load
duke
parents:
diff changeset
130 // Leave _size & _bin alone, under the assumption that dictionary will
a61af66fc99e Initial load
duke
parents:
diff changeset
131 // grow to this size again.
a61af66fc99e Initial load
duke
parents:
diff changeset
132 }
a61af66fc99e Initial load
duke
parents:
diff changeset
133
a61af66fc99e Initial load
duke
parents:
diff changeset
134 //------------------------------doubhash---------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
135 // Double hash table size. If can't do so, just suffer. If can, then run
a61af66fc99e Initial load
duke
parents:
diff changeset
136 // thru old hash table, moving things to new table. Note that since hash
a61af66fc99e Initial load
duke
parents:
diff changeset
137 // table doubled, exactly 1 new bit is exposed in the mask - so everything
a61af66fc99e Initial load
duke
parents:
diff changeset
138 // in the old table ends up on 1 of two lists in the new table; a hi and a
a61af66fc99e Initial load
duke
parents:
diff changeset
139 // lo list depending on the value of the bit.
a61af66fc99e Initial load
duke
parents:
diff changeset
140 void Dict::doubhash(void) {
a61af66fc99e Initial load
duke
parents:
diff changeset
141 uint oldsize = _size;
a61af66fc99e Initial load
duke
parents:
diff changeset
142 _size <<= 1; // Double in size
a61af66fc99e Initial load
duke
parents:
diff changeset
143 _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*oldsize, sizeof(bucket)*_size );
a61af66fc99e Initial load
duke
parents:
diff changeset
144 memset( &_bin[oldsize], 0, oldsize*sizeof(bucket) );
a61af66fc99e Initial load
duke
parents:
diff changeset
145 // Rehash things to spread into new table
a61af66fc99e Initial load
duke
parents:
diff changeset
146 for( uint i=0; i < oldsize; i++) { // For complete OLD table do
a61af66fc99e Initial load
duke
parents:
diff changeset
147 bucket *b = &_bin[i]; // Handy shortcut for _bin[i]
a61af66fc99e Initial load
duke
parents:
diff changeset
148 if( !b->_keyvals ) continue; // Skip empties fast
a61af66fc99e Initial load
duke
parents:
diff changeset
149
a61af66fc99e Initial load
duke
parents:
diff changeset
150 bucket *nb = &_bin[i+oldsize]; // New bucket shortcut
a61af66fc99e Initial load
duke
parents:
diff changeset
151 uint j = b->_max; // Trim new bucket to nearest power of 2
a61af66fc99e Initial load
duke
parents:
diff changeset
152 while( j > b->_cnt ) j >>= 1; // above old bucket _cnt
a61af66fc99e Initial load
duke
parents:
diff changeset
153 if( !j ) j = 1; // Handle zero-sized buckets
a61af66fc99e Initial load
duke
parents:
diff changeset
154 nb->_max = j<<1;
a61af66fc99e Initial load
duke
parents:
diff changeset
155 // Allocate worst case space for key-value pairs
a61af66fc99e Initial load
duke
parents:
diff changeset
156 nb->_keyvals = (void**)_arena->Amalloc_4( sizeof(void *)*nb->_max*2 );
a61af66fc99e Initial load
duke
parents:
diff changeset
157 uint nbcnt = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
158
a61af66fc99e Initial load
duke
parents:
diff changeset
159 for( j=0; j<b->_cnt; j++ ) { // Rehash all keys in this bucket
a61af66fc99e Initial load
duke
parents:
diff changeset
160 void *key = b->_keyvals[j+j];
a61af66fc99e Initial load
duke
parents:
diff changeset
161 if( (_hash( key ) & (_size-1)) != i ) { // Moving to hi bucket?
a61af66fc99e Initial load
duke
parents:
diff changeset
162 nb->_keyvals[nbcnt+nbcnt] = key;
a61af66fc99e Initial load
duke
parents:
diff changeset
163 nb->_keyvals[nbcnt+nbcnt+1] = b->_keyvals[j+j+1];
a61af66fc99e Initial load
duke
parents:
diff changeset
164 nb->_cnt = nbcnt = nbcnt+1;
a61af66fc99e Initial load
duke
parents:
diff changeset
165 b->_cnt--; // Remove key/value from lo bucket
a61af66fc99e Initial load
duke
parents:
diff changeset
166 b->_keyvals[j+j ] = b->_keyvals[b->_cnt+b->_cnt ];
a61af66fc99e Initial load
duke
parents:
diff changeset
167 b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1];
a61af66fc99e Initial load
duke
parents:
diff changeset
168 j--; // Hash compacted element also
a61af66fc99e Initial load
duke
parents:
diff changeset
169 }
a61af66fc99e Initial load
duke
parents:
diff changeset
170 } // End of for all key-value pairs in bucket
a61af66fc99e Initial load
duke
parents:
diff changeset
171 } // End of for all buckets
a61af66fc99e Initial load
duke
parents:
diff changeset
172
a61af66fc99e Initial load
duke
parents:
diff changeset
173
a61af66fc99e Initial load
duke
parents:
diff changeset
174 }
a61af66fc99e Initial load
duke
parents:
diff changeset
175
a61af66fc99e Initial load
duke
parents:
diff changeset
176 //------------------------------Dict-----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
177 // Deep copy a dictionary.
a61af66fc99e Initial load
duke
parents:
diff changeset
178 Dict::Dict( const Dict &d ) : _size(d._size), _cnt(d._cnt), _hash(d._hash),_cmp(d._cmp), _arena(d._arena) {
a61af66fc99e Initial load
duke
parents:
diff changeset
179 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
180 memcpy( _bin, d._bin, sizeof(bucket)*_size );
a61af66fc99e Initial load
duke
parents:
diff changeset
181 for( uint i=0; i<_size; i++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
182 if( !_bin[i]._keyvals ) continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
183 _bin[i]._keyvals=(void**)_arena->Amalloc_4( sizeof(void *)*_bin[i]._max*2);
a61af66fc99e Initial load
duke
parents:
diff changeset
184 memcpy( _bin[i]._keyvals, d._bin[i]._keyvals,_bin[i]._cnt*2*sizeof(void*));
a61af66fc99e Initial load
duke
parents:
diff changeset
185 }
a61af66fc99e Initial load
duke
parents:
diff changeset
186 }
a61af66fc99e Initial load
duke
parents:
diff changeset
187
a61af66fc99e Initial load
duke
parents:
diff changeset
188 //------------------------------Dict-----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
189 // Deep copy a dictionary.
a61af66fc99e Initial load
duke
parents:
diff changeset
190 Dict &Dict::operator =( const Dict &d ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
191 if( _size < d._size ) { // If must have more buckets
a61af66fc99e Initial load
duke
parents:
diff changeset
192 _arena = d._arena;
a61af66fc99e Initial load
duke
parents:
diff changeset
193 _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*_size, sizeof(bucket)*d._size );
a61af66fc99e Initial load
duke
parents:
diff changeset
194 memset( &_bin[_size], 0, (d._size-_size)*sizeof(bucket) );
a61af66fc99e Initial load
duke
parents:
diff changeset
195 _size = d._size;
a61af66fc99e Initial load
duke
parents:
diff changeset
196 }
a61af66fc99e Initial load
duke
parents:
diff changeset
197 uint i;
a61af66fc99e Initial load
duke
parents:
diff changeset
198 for( i=0; i<_size; i++ ) // All buckets are empty
a61af66fc99e Initial load
duke
parents:
diff changeset
199 _bin[i]._cnt = 0; // But leave bucket allocations alone
a61af66fc99e Initial load
duke
parents:
diff changeset
200 _cnt = d._cnt;
a61af66fc99e Initial load
duke
parents:
diff changeset
201 *(Hash*)(&_hash) = d._hash;
a61af66fc99e Initial load
duke
parents:
diff changeset
202 *(CmpKey*)(&_cmp) = d._cmp;
a61af66fc99e Initial load
duke
parents:
diff changeset
203 for( i=0; i<_size; i++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
204 bucket *b = &d._bin[i]; // Shortcut to source bucket
a61af66fc99e Initial load
duke
parents:
diff changeset
205 for( uint j=0; j<b->_cnt; j++ )
a61af66fc99e Initial load
duke
parents:
diff changeset
206 Insert( b->_keyvals[j+j], b->_keyvals[j+j+1] );
a61af66fc99e Initial load
duke
parents:
diff changeset
207 }
a61af66fc99e Initial load
duke
parents:
diff changeset
208 return *this;
a61af66fc99e Initial load
duke
parents:
diff changeset
209 }
a61af66fc99e Initial load
duke
parents:
diff changeset
210
a61af66fc99e Initial load
duke
parents:
diff changeset
211 //------------------------------Insert----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
212 // Insert or replace a key/value pair in the given dictionary. If the
a61af66fc99e Initial load
duke
parents:
diff changeset
213 // dictionary is too full, it's size is doubled. The prior value being
a61af66fc99e Initial load
duke
parents:
diff changeset
214 // replaced is returned (NULL if this is a 1st insertion of that key). If
a61af66fc99e Initial load
duke
parents:
diff changeset
215 // an old value is found, it's swapped with the prior key-value pair on the
a61af66fc99e Initial load
duke
parents:
diff changeset
216 // list. This moves a commonly searched-for value towards the list head.
a61af66fc99e Initial load
duke
parents:
diff changeset
217 void *Dict::Insert(void *key, void *val, bool replace) {
a61af66fc99e Initial load
duke
parents:
diff changeset
218 uint hash = _hash( key ); // Get hash key
a61af66fc99e Initial load
duke
parents:
diff changeset
219 uint i = hash & (_size-1); // Get hash key, corrected for size
a61af66fc99e Initial load
duke
parents:
diff changeset
220 bucket *b = &_bin[i]; // Handy shortcut
a61af66fc99e Initial load
duke
parents:
diff changeset
221 for( uint j=0; j<b->_cnt; j++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
222 if( !_cmp(key,b->_keyvals[j+j]) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
223 if (!replace) {
a61af66fc99e Initial load
duke
parents:
diff changeset
224 return b->_keyvals[j+j+1];
a61af66fc99e Initial load
duke
parents:
diff changeset
225 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
226 void *prior = b->_keyvals[j+j+1];
a61af66fc99e Initial load
duke
parents:
diff changeset
227 b->_keyvals[j+j ] = key; // Insert current key-value
a61af66fc99e Initial load
duke
parents:
diff changeset
228 b->_keyvals[j+j+1] = val;
a61af66fc99e Initial load
duke
parents:
diff changeset
229 return prior; // Return prior
a61af66fc99e Initial load
duke
parents:
diff changeset
230 }
a61af66fc99e Initial load
duke
parents:
diff changeset
231 }
a61af66fc99e Initial load
duke
parents:
diff changeset
232 }
a61af66fc99e Initial load
duke
parents:
diff changeset
233 if( ++_cnt > _size ) { // Hash table is full
a61af66fc99e Initial load
duke
parents:
diff changeset
234 doubhash(); // Grow whole table if too full
a61af66fc99e Initial load
duke
parents:
diff changeset
235 i = hash & (_size-1); // Rehash
a61af66fc99e Initial load
duke
parents:
diff changeset
236 b = &_bin[i]; // Handy shortcut
a61af66fc99e Initial load
duke
parents:
diff changeset
237 }
a61af66fc99e Initial load
duke
parents:
diff changeset
238 if( b->_cnt == b->_max ) { // Must grow bucket?
a61af66fc99e Initial load
duke
parents:
diff changeset
239 if( !b->_keyvals ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
240 b->_max = 2; // Initial bucket size
a61af66fc99e Initial load
duke
parents:
diff changeset
241 b->_keyvals = (void**)_arena->Amalloc_4(sizeof(void*) * b->_max * 2);
a61af66fc99e Initial load
duke
parents:
diff changeset
242 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
243 b->_keyvals = (void**)_arena->Arealloc(b->_keyvals, sizeof(void*) * b->_max * 2, sizeof(void*) * b->_max * 4);
a61af66fc99e Initial load
duke
parents:
diff changeset
244 b->_max <<= 1; // Double bucket
a61af66fc99e Initial load
duke
parents:
diff changeset
245 }
a61af66fc99e Initial load
duke
parents:
diff changeset
246 }
a61af66fc99e Initial load
duke
parents:
diff changeset
247 b->_keyvals[b->_cnt+b->_cnt ] = key;
a61af66fc99e Initial load
duke
parents:
diff changeset
248 b->_keyvals[b->_cnt+b->_cnt+1] = val;
a61af66fc99e Initial load
duke
parents:
diff changeset
249 b->_cnt++;
a61af66fc99e Initial load
duke
parents:
diff changeset
250 return NULL; // Nothing found prior
a61af66fc99e Initial load
duke
parents:
diff changeset
251 }
a61af66fc99e Initial load
duke
parents:
diff changeset
252
a61af66fc99e Initial load
duke
parents:
diff changeset
253 //------------------------------Delete---------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
254 // Find & remove a value from dictionary. Return old value.
a61af66fc99e Initial load
duke
parents:
diff changeset
255 void *Dict::Delete(void *key) {
a61af66fc99e Initial load
duke
parents:
diff changeset
256 uint i = _hash( key ) & (_size-1); // Get hash key, corrected for size
a61af66fc99e Initial load
duke
parents:
diff changeset
257 bucket *b = &_bin[i]; // Handy shortcut
a61af66fc99e Initial load
duke
parents:
diff changeset
258 for( uint j=0; j<b->_cnt; j++ )
a61af66fc99e Initial load
duke
parents:
diff changeset
259 if( !_cmp(key,b->_keyvals[j+j]) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
260 void *prior = b->_keyvals[j+j+1];
a61af66fc99e Initial load
duke
parents:
diff changeset
261 b->_cnt--; // Remove key/value from lo bucket
a61af66fc99e Initial load
duke
parents:
diff changeset
262 b->_keyvals[j+j ] = b->_keyvals[b->_cnt+b->_cnt ];
a61af66fc99e Initial load
duke
parents:
diff changeset
263 b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1];
a61af66fc99e Initial load
duke
parents:
diff changeset
264 _cnt--; // One less thing in table
a61af66fc99e Initial load
duke
parents:
diff changeset
265 return prior;
a61af66fc99e Initial load
duke
parents:
diff changeset
266 }
a61af66fc99e Initial load
duke
parents:
diff changeset
267 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
268 }
a61af66fc99e Initial load
duke
parents:
diff changeset
269
a61af66fc99e Initial load
duke
parents:
diff changeset
270 //------------------------------FindDict-------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
271 // Find a key-value pair in the given dictionary. If not found, return NULL.
a61af66fc99e Initial load
duke
parents:
diff changeset
272 // If found, move key-value pair towards head of list.
a61af66fc99e Initial load
duke
parents:
diff changeset
273 void *Dict::operator [](const void *key) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
274 uint i = _hash( key ) & (_size-1); // Get hash key, corrected for size
a61af66fc99e Initial load
duke
parents:
diff changeset
275 bucket *b = &_bin[i]; // Handy shortcut
a61af66fc99e Initial load
duke
parents:
diff changeset
276 for( uint j=0; j<b->_cnt; j++ )
a61af66fc99e Initial load
duke
parents:
diff changeset
277 if( !_cmp(key,b->_keyvals[j+j]) )
a61af66fc99e Initial load
duke
parents:
diff changeset
278 return b->_keyvals[j+j+1];
a61af66fc99e Initial load
duke
parents:
diff changeset
279 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
280 }
a61af66fc99e Initial load
duke
parents:
diff changeset
281
a61af66fc99e Initial load
duke
parents:
diff changeset
282 //------------------------------CmpDict--------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
283 // CmpDict compares two dictionaries; they must have the same keys (their
a61af66fc99e Initial load
duke
parents:
diff changeset
284 // keys must match using CmpKey) and they must have the same values (pointer
a61af66fc99e Initial load
duke
parents:
diff changeset
285 // comparison). If so 1 is returned, if not 0 is returned.
a61af66fc99e Initial load
duke
parents:
diff changeset
286 int32 Dict::operator ==(const Dict &d2) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
287 if( _cnt != d2._cnt ) return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
288 if( _hash != d2._hash ) return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
289 if( _cmp != d2._cmp ) return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
290 for( uint i=0; i < _size; i++) { // For complete hash table do
a61af66fc99e Initial load
duke
parents:
diff changeset
291 bucket *b = &_bin[i]; // Handy shortcut
a61af66fc99e Initial load
duke
parents:
diff changeset
292 if( b->_cnt != d2._bin[i]._cnt ) return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
293 if( memcmp(b->_keyvals, d2._bin[i]._keyvals, b->_cnt*2*sizeof(void*) ) )
a61af66fc99e Initial load
duke
parents:
diff changeset
294 return 0; // Key-value pairs must match
a61af66fc99e Initial load
duke
parents:
diff changeset
295 }
a61af66fc99e Initial load
duke
parents:
diff changeset
296 return 1; // All match, is OK
a61af66fc99e Initial load
duke
parents:
diff changeset
297 }
a61af66fc99e Initial load
duke
parents:
diff changeset
298
a61af66fc99e Initial load
duke
parents:
diff changeset
299 //------------------------------print------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
300 // Handier print routine
a61af66fc99e Initial load
duke
parents:
diff changeset
301 void Dict::print() {
a61af66fc99e Initial load
duke
parents:
diff changeset
302 DictI i(this); // Moved definition in iterator here because of g++.
a61af66fc99e Initial load
duke
parents:
diff changeset
303 tty->print("Dict@0x%lx[%d] = {", this, _cnt);
a61af66fc99e Initial load
duke
parents:
diff changeset
304 for( ; i.test(); ++i ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
305 tty->print("(0x%lx,0x%lx),", i._key, i._value);
a61af66fc99e Initial load
duke
parents:
diff changeset
306 }
a61af66fc99e Initial load
duke
parents:
diff changeset
307 tty->print_cr("}");
a61af66fc99e Initial load
duke
parents:
diff changeset
308 }
a61af66fc99e Initial load
duke
parents:
diff changeset
309
a61af66fc99e Initial load
duke
parents:
diff changeset
310 //------------------------------Hashing Functions----------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
311 // Convert string to hash key. This algorithm implements a universal hash
a61af66fc99e Initial load
duke
parents:
diff changeset
312 // function with the multipliers frozen (ok, so it's not universal). The
a61af66fc99e Initial load
duke
parents:
diff changeset
313 // multipliers (and allowable characters) are all odd, so the resultant sum
605
98cb887364d3 6810672: Comment typos
twisti
parents: 562
diff changeset
314 // is odd - guaranteed not divisible by any power of two, so the hash tables
0
a61af66fc99e Initial load
duke
parents:
diff changeset
315 // can be any power of two with good results. Also, I choose multipliers
a61af66fc99e Initial load
duke
parents:
diff changeset
316 // that have only 2 bits set (the low is always set to be odd) so
a61af66fc99e Initial load
duke
parents:
diff changeset
317 // multiplication requires only shifts and adds. Characters are required to
a61af66fc99e Initial load
duke
parents:
diff changeset
318 // be in the range 0-127 (I double & add 1 to force oddness). Keys are
a61af66fc99e Initial load
duke
parents:
diff changeset
319 // limited to MAXID characters in length. Experimental evidence on 150K of
a61af66fc99e Initial load
duke
parents:
diff changeset
320 // C text shows excellent spreading of values for any size hash table.
a61af66fc99e Initial load
duke
parents:
diff changeset
321 int hashstr(const void *t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
322 register char c, k = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
323 register int32 sum = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
324 register const char *s = (const char *)t;
a61af66fc99e Initial load
duke
parents:
diff changeset
325
a61af66fc99e Initial load
duke
parents:
diff changeset
326 while( ((c = *s++) != '\0') && (k < MAXID-1) ) { // Get characters till null or MAXID-1
a61af66fc99e Initial load
duke
parents:
diff changeset
327 c = (c<<1)+1; // Characters are always odd!
a61af66fc99e Initial load
duke
parents:
diff changeset
328 sum += c + (c<<shft[k++]); // Universal hash function
a61af66fc99e Initial load
duke
parents:
diff changeset
329 }
a61af66fc99e Initial load
duke
parents:
diff changeset
330 return (int)((sum+xsum[k]) >> 1); // Hash key, un-modulo'd table size
a61af66fc99e Initial load
duke
parents:
diff changeset
331 }
a61af66fc99e Initial load
duke
parents:
diff changeset
332
a61af66fc99e Initial load
duke
parents:
diff changeset
333 //------------------------------hashptr--------------------------------------
605
98cb887364d3 6810672: Comment typos
twisti
parents: 562
diff changeset
334 // Slimey cheap hash function; no guaranteed performance. Better than the
0
a61af66fc99e Initial load
duke
parents:
diff changeset
335 // default for pointers, especially on MS-DOS machines.
a61af66fc99e Initial load
duke
parents:
diff changeset
336 int hashptr(const void *key) {
a61af66fc99e Initial load
duke
parents:
diff changeset
337 #ifdef __TURBOC__
a61af66fc99e Initial load
duke
parents:
diff changeset
338 return ((intptr_t)key >> 16);
a61af66fc99e Initial load
duke
parents:
diff changeset
339 #else // __TURBOC__
a61af66fc99e Initial load
duke
parents:
diff changeset
340 return ((intptr_t)key >> 2);
a61af66fc99e Initial load
duke
parents:
diff changeset
341 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
342 }
a61af66fc99e Initial load
duke
parents:
diff changeset
343
605
98cb887364d3 6810672: Comment typos
twisti
parents: 562
diff changeset
344 // Slimey cheap hash function; no guaranteed performance.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
345 int hashkey(const void *key) {
a61af66fc99e Initial load
duke
parents:
diff changeset
346 return (intptr_t)key;
a61af66fc99e Initial load
duke
parents:
diff changeset
347 }
a61af66fc99e Initial load
duke
parents:
diff changeset
348
a61af66fc99e Initial load
duke
parents:
diff changeset
349 //------------------------------Key Comparator Functions---------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
350 int32 cmpstr(const void *k1, const void *k2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
351 return strcmp((const char *)k1,(const char *)k2);
a61af66fc99e Initial load
duke
parents:
diff changeset
352 }
a61af66fc99e Initial load
duke
parents:
diff changeset
353
562
1580954e694c 6798785: Crash in OopFlow::build_oop_map: incorrect comparison of 64bit pointers
never
parents: 0
diff changeset
354 // Cheap key comparator.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
355 int32 cmpkey(const void *key1, const void *key2) {
562
1580954e694c 6798785: Crash in OopFlow::build_oop_map: incorrect comparison of 64bit pointers
never
parents: 0
diff changeset
356 if (key1 == key2) return 0;
1580954e694c 6798785: Crash in OopFlow::build_oop_map: incorrect comparison of 64bit pointers
never
parents: 0
diff changeset
357 intptr_t delta = (intptr_t)key1 - (intptr_t)key2;
1580954e694c 6798785: Crash in OopFlow::build_oop_map: incorrect comparison of 64bit pointers
never
parents: 0
diff changeset
358 if (delta > 0) return 1;
1580954e694c 6798785: Crash in OopFlow::build_oop_map: incorrect comparison of 64bit pointers
never
parents: 0
diff changeset
359 return -1;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
360 }
a61af66fc99e Initial load
duke
parents:
diff changeset
361
a61af66fc99e Initial load
duke
parents:
diff changeset
362 //=============================================================================
a61af66fc99e Initial load
duke
parents:
diff changeset
363 //------------------------------reset------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
364 // Create an iterator and initialize the first variables.
a61af66fc99e Initial load
duke
parents:
diff changeset
365 void DictI::reset( const Dict *dict ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
366 _d = dict; // The dictionary
a61af66fc99e Initial load
duke
parents:
diff changeset
367 _i = (uint)-1; // Before the first bin
a61af66fc99e Initial load
duke
parents:
diff changeset
368 _j = 0; // Nothing left in the current bin
a61af66fc99e Initial load
duke
parents:
diff changeset
369 ++(*this); // Step to first real value
a61af66fc99e Initial load
duke
parents:
diff changeset
370 }
a61af66fc99e Initial load
duke
parents:
diff changeset
371
a61af66fc99e Initial load
duke
parents:
diff changeset
372 //------------------------------next-------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
373 // Find the next key-value pair in the dictionary, or return a NULL key and
a61af66fc99e Initial load
duke
parents:
diff changeset
374 // value.
a61af66fc99e Initial load
duke
parents:
diff changeset
375 void DictI::operator ++(void) {
a61af66fc99e Initial load
duke
parents:
diff changeset
376 if( _j-- ) { // Still working in current bin?
a61af66fc99e Initial load
duke
parents:
diff changeset
377 _key = _d->_bin[_i]._keyvals[_j+_j];
a61af66fc99e Initial load
duke
parents:
diff changeset
378 _value = _d->_bin[_i]._keyvals[_j+_j+1];
a61af66fc99e Initial load
duke
parents:
diff changeset
379 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
380 }
a61af66fc99e Initial load
duke
parents:
diff changeset
381
a61af66fc99e Initial load
duke
parents:
diff changeset
382 while( ++_i < _d->_size ) { // Else scan for non-zero bucket
a61af66fc99e Initial load
duke
parents:
diff changeset
383 _j = _d->_bin[_i]._cnt;
a61af66fc99e Initial load
duke
parents:
diff changeset
384 if( !_j ) continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
385 _j--;
a61af66fc99e Initial load
duke
parents:
diff changeset
386 _key = _d->_bin[_i]._keyvals[_j+_j];
a61af66fc99e Initial load
duke
parents:
diff changeset
387 _value = _d->_bin[_i]._keyvals[_j+_j+1];
a61af66fc99e Initial load
duke
parents:
diff changeset
388 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
389 }
a61af66fc99e Initial load
duke
parents:
diff changeset
390 _key = _value = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
391 }