src/share/vm/libadt/dict.cpp

Mon, 12 Aug 2019 18:30:40 +0300

author
apetushkov
date
Mon, 12 Aug 2019 18:30:40 +0300
changeset 9858
b985cbb00e68
parent 6680
78bbf4d43a14
child 6876
710a3c8b516e
permissions
-rw-r--r--

8223147: JFR Backport
8199712: Flight Recorder
8203346: JFR: Inconsistent signature of jfr_add_string_constant
8195817: JFR.stop should require name of recording
8195818: JFR.start should increase autogenerated name by one
8195819: Remove recording=x from jcmd JFR.check output
8203921: JFR thread sampling is missing fixes from JDK-8194552
8203929: Limit amount of data for JFR.dump
8203664: JFR start failure after AppCDS archive created with JFR StartFlightRecording
8003209: JFR events for network utilization
8207392: [PPC64] Implement JFR profiling
8202835: jfr/event/os/TestSystemProcess.java fails on missing events
Summary: Backport JFR from JDK11. Initial integration
Reviewed-by: neugens

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

mercurial