1 /*
2  * Copyright (c) 2020, 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 #include "precompiled.hpp"
26 #include "runtime/flags/jvmFlag.hpp"
27 #include "runtime/flags/jvmFlagLookup.hpp"
28 #include "utilities/defaultStream.hpp"
29 
30 #define DO_FLAG(type, name,...) DO_HASH(FLAG_MEMBER_ENUM(name), XSTR(name))
31 
32 #define DO_HASH(flag_enum, flag_name) {          \
33   unsigned int hash = hash_code(flag_name);      \
34   int bucket_index = (int)(hash % NUM_BUCKETS);  \
35   _hashes[flag_enum] = (u2)(hash);               \
36   _table[flag_enum] = _buckets[bucket_index];    \
37   _buckets[bucket_index] = (short)flag_enum;     \
38 }
39 
JVMFlagLookup()40 constexpr JVMFlagLookup::JVMFlagLookup() : _buckets(), _table(), _hashes() {
41   for (int i = 0; i < NUM_BUCKETS; i++) {
42     _buckets[i] = -1;
43   }
44 
45   ALL_FLAGS(DO_FLAG,
46             DO_FLAG,
47             DO_FLAG,
48             DO_FLAG,
49             DO_FLAG,
50             IGNORE_RANGE,
51             IGNORE_CONSTRAINT)
52 }
53 
54 constexpr JVMFlagLookup _flag_lookup_table;
55 
find_impl(const char * name,size_t length) const56 JVMFlag* JVMFlagLookup::find_impl(const char* name, size_t length) const {
57   unsigned int hash = hash_code(name, length);
58   int bucket_index = (int)(hash % NUM_BUCKETS);
59   for (int flag_enum = _buckets[bucket_index]; flag_enum >= 0; ) {
60     if (_hashes[flag_enum] == (u2)hash) {
61       JVMFlag* flag = JVMFlag::flags + flag_enum;
62       if (strncmp(name, flag->name(), length) == 0) {
63         // We know flag->name() has at least <length> bytes.
64         // Make sure it has exactly <length> bytes
65         if (flag->name()[length] == 0) {
66           return flag;
67         }
68       }
69     }
70     flag_enum = (int)_table[flag_enum];
71   }
72 
73   return NULL;
74 }
75 
find(const char * name,size_t length)76 JVMFlag* JVMFlagLookup::find(const char* name, size_t length) {
77   return _flag_lookup_table.find_impl(name, length);
78 }
79