1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_CLASSFILE_COMPACTHASHTABLE_INLINE_HPP
  26 #define SHARE_VM_CLASSFILE_COMPACTHASHTABLE_INLINE_HPP
  27 
  28 #include "classfile/compactHashtable.hpp"
  29 #include "oops/oop.inline.hpp"
  30 
  31 template <class T, class N>
  32 Symbol* CompactHashtable<T, N>::lookup_entry(CompactHashtable<Symbol*, char>* const t,
  33                                              juint* addr, const char* name, int len) {
  34   Symbol* sym = (Symbol*)((void*)(_base_address + *addr));
  35   if (sym->equals(name, len)) {
  36     assert(sym->refcount() == -1, "must be shared");
  37     return sym;
  38   }
  39 
  40   return NULL;
  41 }
  42 
  43 template <class T, class N>
  44 inline oop CompactHashtable<T, N>::lookup_entry(CompactHashtable<oop, char>* const t,
  45                                                 juint* addr, const char* name, int len) {
  46   narrowOop obj = (narrowOop)(*addr);
  47   oop string = oopDesc::decode_heap_oop(obj);
  48   if (java_lang_String::equals(string, (jchar*)name, len)) {
  49     return string;
  50   }
  51 
  52   return NULL;
  53 }
  54 
  55 template <class T, class N>
  56 inline T CompactHashtable<T,N>::lookup(const N* name, unsigned int hash, int len) {
  57   if (_entry_count > 0) {
  58     assert(!DumpSharedSpaces, "run-time only");
  59     int index = hash % _bucket_count;
  60     juint bucket_info = _buckets[index];
  61     juint bucket_offset = BUCKET_OFFSET(bucket_info);
  62     int   bucket_type = BUCKET_TYPE(bucket_info);
  63     juint* bucket = _buckets + bucket_offset;
  64     juint* bucket_end = _buckets;
  65 
  66     if (bucket_type == COMPACT_BUCKET_TYPE) {
  67       // the compact bucket has one entry with entry offset only
  68       T res = lookup_entry(this, &bucket[0], name, len);
  69       if (res != NULL) {
  70         return res;
  71       }
  72     } else {
  73       // This is a regular bucket, which has more than one
  74       // entries. Each entry is a pair of entry (hash, offset).
  75       // Seek until the end of the bucket.
  76       bucket_end += BUCKET_OFFSET(_buckets[index + 1]);
  77       while (bucket < bucket_end) {
  78         unsigned int h = (unsigned int)(bucket[0]);
  79         if (h == hash) {
  80           T res = lookup_entry(this, &bucket[1], name, len);
  81           if (res != NULL) {
  82             return res;
  83           }
  84         }
  85         bucket += 2;
  86       }
  87     }
  88   }
  89   return NULL;
  90 }
  91 
  92 #endif // SHARE_VM_CLASSFILE_COMPACTHASHTABLE_INLINE_HPP