|
1 /* |
|
2 * Copyright (c) 2015, 2019, 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 #ifndef SHARE_GC_Z_ZFORWARDING_INLINE_HPP |
|
25 #define SHARE_GC_Z_ZFORWARDING_INLINE_HPP |
|
26 |
|
27 #include "gc/z/zForwarding.hpp" |
|
28 #include "gc/z/zGlobals.hpp" |
|
29 #include "gc/z/zHash.inline.hpp" |
|
30 #include "runtime/atomic.hpp" |
|
31 #include "utilities/debug.hpp" |
|
32 |
|
33 inline uintptr_t ZForwarding::start() const { |
|
34 return _start; |
|
35 } |
|
36 |
|
37 inline size_t ZForwarding::object_alignment_shift() const { |
|
38 return _object_alignment_shift; |
|
39 } |
|
40 |
|
41 inline bool ZForwarding::inc_refcount() { |
|
42 uint32_t refcount = Atomic::load(&_refcount); |
|
43 |
|
44 while (refcount > 0) { |
|
45 const uint32_t old_refcount = refcount; |
|
46 const uint32_t new_refcount = old_refcount + 1; |
|
47 const uint32_t prev_refcount = Atomic::cmpxchg(new_refcount, &_refcount, old_refcount); |
|
48 if (prev_refcount == old_refcount) { |
|
49 return true; |
|
50 } |
|
51 |
|
52 refcount = prev_refcount; |
|
53 } |
|
54 |
|
55 return false; |
|
56 } |
|
57 |
|
58 inline bool ZForwarding::dec_refcount() { |
|
59 assert(_refcount > 0, "Invalid state"); |
|
60 return Atomic::sub(1u, &_refcount) == 0u; |
|
61 } |
|
62 |
|
63 inline bool ZForwarding::is_pinned() const { |
|
64 return Atomic::load(&_pinned); |
|
65 } |
|
66 |
|
67 inline void ZForwarding::set_pinned() { |
|
68 Atomic::store(true, &_pinned); |
|
69 } |
|
70 |
|
71 inline ZForwardingEntry* ZForwarding::entries() const { |
|
72 return reinterpret_cast<ZForwardingEntry*>(reinterpret_cast<uintptr_t>(this) + sizeof(*this)); |
|
73 } |
|
74 |
|
75 inline ZForwardingEntry ZForwarding::at(ZForwardingCursor* cursor) const { |
|
76 return Atomic::load(entries() + *cursor); |
|
77 } |
|
78 |
|
79 inline ZForwardingEntry ZForwarding::first(uintptr_t from_index, ZForwardingCursor* cursor) const { |
|
80 const uint32_t mask = _nentries - 1; |
|
81 const uint32_t hash = ZHash::uint32_to_uint32((uint32_t)from_index); |
|
82 *cursor = hash & mask; |
|
83 return at(cursor); |
|
84 } |
|
85 |
|
86 inline ZForwardingEntry ZForwarding::next(ZForwardingCursor* cursor) const { |
|
87 const uint32_t mask = _nentries - 1; |
|
88 *cursor = (*cursor + 1) & mask; |
|
89 return at(cursor); |
|
90 } |
|
91 |
|
92 inline ZForwardingEntry ZForwarding::find(uintptr_t from_index) const { |
|
93 ZForwardingCursor dummy; |
|
94 return find(from_index, &dummy); |
|
95 } |
|
96 |
|
97 inline ZForwardingEntry ZForwarding::find(uintptr_t from_index, ZForwardingCursor* cursor) const { |
|
98 // Reading entries in the table races with the atomic CAS done for |
|
99 // insertion into the table. This is safe because each entry is at |
|
100 // most updated once (from -1 to something else). |
|
101 ZForwardingEntry entry = first(from_index, cursor); |
|
102 while (!entry.is_empty()) { |
|
103 if (entry.from_index() == from_index) { |
|
104 // Match found, return matching entry |
|
105 return entry; |
|
106 } |
|
107 |
|
108 entry = next(cursor); |
|
109 } |
|
110 |
|
111 // Match not found, return empty entry |
|
112 return entry; |
|
113 } |
|
114 |
|
115 inline uintptr_t ZForwarding::insert(uintptr_t from_index, uintptr_t to_offset, ZForwardingCursor* cursor) { |
|
116 const ZForwardingEntry new_entry(from_index, to_offset); |
|
117 const ZForwardingEntry old_entry; // empty |
|
118 |
|
119 for (;;) { |
|
120 const ZForwardingEntry prev_entry = Atomic::cmpxchg(new_entry, entries() + *cursor, old_entry); |
|
121 if (prev_entry.is_empty()) { |
|
122 // Success |
|
123 return to_offset; |
|
124 } |
|
125 |
|
126 // Find next empty or matching entry |
|
127 ZForwardingEntry entry = at(cursor); |
|
128 while (!entry.is_empty()) { |
|
129 if (entry.from_index() == from_index) { |
|
130 // Match found, return already inserted address |
|
131 return entry.to_offset(); |
|
132 } |
|
133 |
|
134 entry = next(cursor); |
|
135 } |
|
136 } |
|
137 } |
|
138 |
|
139 #endif // SHARE_GC_Z_ZFORWARDING_INLINE_HPP |