Wed, 03 Jul 2019 20:42:37 +0800
Merge
1 /*
2 * Copyright (c) 1999, 2010, 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 */
25 #include "precompiled.hpp"
26 #include "ci/ciConstantPoolCache.hpp"
27 #include "ci/ciUtilities.hpp"
28 #include "memory/allocation.hpp"
29 #include "memory/allocation.inline.hpp"
31 // ciConstantPoolCache
32 //
33 // This class caches indexed constant pool lookups.
35 // ------------------------------------------------------------------
36 // ciConstantPoolCache::ciConstantPoolCache
37 ciConstantPoolCache::ciConstantPoolCache(Arena* arena,
38 int expected_size) {
39 _elements =
40 new (arena) GrowableArray<void*>(arena, expected_size, 0, 0);
41 _keys = new (arena) GrowableArray<intptr_t>(arena, expected_size, 0, 0);
42 }
44 // ------------------------------------------------------------------
45 // ciConstantPoolCache::get
46 //
47 // Get the entry at some index
48 void* ciConstantPoolCache::get(int index) {
49 ASSERT_IN_VM;
50 int pos = find(index);
51 if (pos >= _keys->length() ||
52 _keys->at(pos) != index) {
53 // This element is not present in the cache.
54 return NULL;
55 }
56 return _elements->at(pos);
57 }
59 // ------------------------------------------------------------------
60 // ciConstantPoolCache::find
61 //
62 // Use binary search to find the position of this index in the cache.
63 // If there is no entry in the cache corresponding to this oop, return
64 // the position at which the index would be inserted.
65 int ciConstantPoolCache::find(int key) {
66 int min = 0;
67 int max = _keys->length()-1;
69 while (max >= min) {
70 int mid = (max + min) / 2;
71 int value = _keys->at(mid);
72 if (value < key) {
73 min = mid + 1;
74 } else if (value > key) {
75 max = mid - 1;
76 } else {
77 return mid;
78 }
79 }
80 return min;
81 }
83 // ------------------------------------------------------------------
84 // ciConstantPoolCache::insert
85 //
86 // Insert a ciObject into the table at some index.
87 void ciConstantPoolCache::insert(int index, void* elem) {
88 int i;
89 int pos = find(index);
90 for (i = _keys->length()-1; i >= pos; i--) {
91 _keys->at_put_grow(i+1, _keys->at(i));
92 _elements->at_put_grow(i+1, _elements->at(i));
93 }
94 _keys->at_put_grow(pos, index);
95 _elements->at_put_grow(pos, elem);
96 }
98 // ------------------------------------------------------------------
99 // ciConstantPoolCache::print
100 //
101 // Print debugging information about the cache.
102 void ciConstantPoolCache::print() {
103 Unimplemented();
104 }