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_OOPS_ACCESS_HPP
26 #define SHARE_OOPS_ACCESS_HPP
27
28 #include "memory/allStatic.hpp"
29 #include "oops/accessBackend.hpp"
30 #include "oops/accessDecorators.hpp"
31 #include "oops/oopsHierarchy.hpp"
32 #include "utilities/debug.hpp"
33 #include "utilities/globalDefinitions.hpp"
34
35
36 // = GENERAL =
37 // Access is an API for performing accesses with declarative semantics. Each access can have a number of "decorators".
38 // A decorator is an attribute or property that affects the way a memory access is performed in some way.
39 // There are different groups of decorators. Some have to do with memory ordering, others to do with,
40 // e.g. strength of references, strength of GC barriers, or whether compression should be applied or not.
41 // Some decorators are set at buildtime, such as whether primitives require GC barriers or not, others
42 // at callsites such as whether an access is in the heap or not, and others are resolved at runtime
43 // such as GC-specific barriers and encoding/decoding compressed oops. For more information about what
44 // decorators are available, cf. oops/accessDecorators.hpp.
45 // By pipelining handling of these decorators, the design of the Access API allows separation of concern
46 // over the different orthogonal concerns of decorators, while providing a powerful way of
47 // expressing these orthogonal semantic properties in a unified way.
48 //
49 // == OPERATIONS ==
50 // * load: Load a value from an address.
51 // * load_at: Load a value from an internal pointer relative to a base object.
52 // * store: Store a value at an address.
53 // * store_at: Store a value in an internal pointer relative to a base object.
54 // * atomic_cmpxchg: Atomically compare-and-swap a new value at an address if previous value matched the compared value.
55 // * atomic_cmpxchg_at: Atomically compare-and-swap a new value at an internal pointer address if previous value matched the compared value.
56 // * atomic_xchg: Atomically swap a new value at an address without checking the previous value.
57 // * atomic_xchg_at: Atomically swap a new value at an internal pointer address without checking the previous value.
58 // * arraycopy: Copy data from one heap array to another heap array. The ArrayAccess class has convenience functions for this.
59 // * clone: Clone the contents of an object to a newly allocated object.
60 //
61 // == IMPLEMENTATION ==
62 // Each access goes through the following steps in a template pipeline.
63 // There are essentially 5 steps for each access:
64 // * Step 1: Set default decorators and decay types. This step gets rid of CV qualifiers
65 // and sets default decorators to sensible values.
66 // * Step 2: Reduce types. This step makes sure there is only a single T type and not
67 // multiple types. The P type of the address and T type of the value must
68 // match.
69 // * Step 3: Pre-runtime dispatch. This step checks whether a runtime call can be
70 // avoided, and in that case avoids it (calling raw accesses or
71 // primitive accesses in a build that does not require primitive GC barriers)
72 // * Step 4: Runtime-dispatch. This step performs a runtime dispatch to the corresponding
73 // BarrierSet::AccessBarrier accessor that attaches GC-required barriers
74 // to the access.
75 // * Step 5.a: Barrier resolution. This step is invoked the first time a runtime-dispatch
76 // happens for an access. The appropriate BarrierSet::AccessBarrier accessor
77 // is resolved, then the function pointer is updated to that accessor for
78 // future invocations.
79 // * Step 5.b: Post-runtime dispatch. This step now casts previously unknown types such
104 const DecoratorSet primitive_decorators = (AS_DECORATOR_MASK ^ AS_NO_KEEPALIVE) |
105 IN_HEAP | IS_ARRAY;
106 verify_decorators<expected_mo_decorators | primitive_decorators>();
107 }
108
109 template <DecoratorSet expected_mo_decorators>
110 static void verify_oop_decorators() {
111 const DecoratorSet oop_decorators = AS_DECORATOR_MASK | IN_DECORATOR_MASK |
112 (ON_DECORATOR_MASK ^ ON_UNKNOWN_OOP_REF) | // no unknown oop refs outside of the heap
113 IS_ARRAY | IS_NOT_NULL | IS_DEST_UNINITIALIZED;
114 verify_decorators<expected_mo_decorators | oop_decorators>();
115 }
116
117 template <DecoratorSet expected_mo_decorators>
118 static void verify_heap_oop_decorators() {
119 const DecoratorSet heap_oop_decorators = AS_DECORATOR_MASK | ON_DECORATOR_MASK |
120 IN_HEAP | IS_ARRAY | IS_NOT_NULL | IS_DEST_UNINITIALIZED;
121 verify_decorators<expected_mo_decorators | heap_oop_decorators>();
122 }
123
124 static const DecoratorSet load_mo_decorators = MO_UNORDERED | MO_RELAXED | MO_ACQUIRE | MO_SEQ_CST;
125 static const DecoratorSet store_mo_decorators = MO_UNORDERED | MO_RELAXED | MO_RELEASE | MO_SEQ_CST;
126 static const DecoratorSet atomic_xchg_mo_decorators = MO_SEQ_CST;
127 static const DecoratorSet atomic_cmpxchg_mo_decorators = MO_RELAXED | MO_SEQ_CST;
128
129 protected:
130 template <typename T>
131 static inline bool oop_arraycopy(arrayOop src_obj, size_t src_offset_in_bytes, const T* src_raw,
132 arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
133 size_t length) {
134 verify_decorators<ARRAYCOPY_DECORATOR_MASK | IN_HEAP |
135 AS_DECORATOR_MASK | IS_ARRAY | IS_DEST_UNINITIALIZED>();
136 return AccessInternal::arraycopy<decorators | INTERNAL_VALUE_IS_OOP>(src_obj, src_offset_in_bytes, src_raw,
137 dst_obj, dst_offset_in_bytes, dst_raw,
138 length);
139 }
140
141 template <typename T>
142 static inline void arraycopy(arrayOop src_obj, size_t src_offset_in_bytes, const T* src_raw,
143 arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
144 size_t length) {
145 verify_decorators<ARRAYCOPY_DECORATOR_MASK | IN_HEAP |
146 AS_DECORATOR_MASK | IS_ARRAY>();
147 AccessInternal::arraycopy<decorators>(src_obj, src_offset_in_bytes, src_raw,
148 dst_obj, dst_offset_in_bytes, dst_raw,
149 length);
150 }
151
152 public:
153 // Primitive heap accesses
154 static inline AccessInternal::LoadAtProxy<decorators> load_at(oop base, ptrdiff_t offset) {
155 verify_primitive_decorators<load_mo_decorators>();
156 return AccessInternal::LoadAtProxy<decorators>(base, offset);
157 }
158
194 typedef typename AccessInternal::OopOrNarrowOop<T>::type OopType;
195 OopType new_oop_value = new_value;
196 OopType compare_oop_value = compare_value;
197 return AccessInternal::atomic_cmpxchg_at<decorators | INTERNAL_VALUE_IS_OOP>(base, offset, compare_oop_value, new_oop_value);
198 }
199
200 template <typename T>
201 static inline T oop_atomic_xchg_at(oop base, ptrdiff_t offset, T new_value) {
202 verify_heap_oop_decorators<atomic_xchg_mo_decorators>();
203 typedef typename AccessInternal::OopOrNarrowOop<T>::type OopType;
204 OopType new_oop_value = new_value;
205 return AccessInternal::atomic_xchg_at<decorators | INTERNAL_VALUE_IS_OOP>(base, offset, new_oop_value);
206 }
207
208 // Clone an object from src to dst
209 static inline void clone(oop src, oop dst, size_t size) {
210 verify_decorators<IN_HEAP>();
211 AccessInternal::clone<decorators>(src, dst, size);
212 }
213
214 // Primitive accesses
215 template <typename P>
216 static inline P load(P* addr) {
217 verify_primitive_decorators<load_mo_decorators>();
218 return AccessInternal::load<decorators, P, P>(addr);
219 }
220
221 template <typename P, typename T>
222 static inline void store(P* addr, T value) {
223 verify_primitive_decorators<store_mo_decorators>();
224 AccessInternal::store<decorators>(addr, value);
225 }
226
227 template <typename P, typename T>
228 static inline T atomic_cmpxchg(P* addr, T compare_value, T new_value) {
229 verify_primitive_decorators<atomic_cmpxchg_mo_decorators>();
230 return AccessInternal::atomic_cmpxchg<decorators>(addr, compare_value, new_value);
231 }
232
233 template <typename P, typename T>
304 }
305
306 template <typename T>
307 static inline void arraycopy_to_native(arrayOop src_obj, size_t src_offset_in_bytes,
308 T* dst,
309 size_t length) {
310 AccessT::arraycopy(src_obj, src_offset_in_bytes, static_cast<const T*>(nullptr),
311 nullptr, 0, dst,
312 length);
313 }
314
315 template <typename T>
316 static inline void arraycopy_from_native(const T* src,
317 arrayOop dst_obj, size_t dst_offset_in_bytes,
318 size_t length) {
319 AccessT::arraycopy(nullptr, 0, src,
320 dst_obj, dst_offset_in_bytes, static_cast<T*>(nullptr),
321 length);
322 }
323
324 static inline bool oop_arraycopy(arrayOop src_obj, size_t src_offset_in_bytes,
325 arrayOop dst_obj, size_t dst_offset_in_bytes,
326 size_t length) {
327 return AccessT::oop_arraycopy(src_obj, src_offset_in_bytes, static_cast<const HeapWord*>(nullptr),
328 dst_obj, dst_offset_in_bytes, static_cast<HeapWord*>(nullptr),
329 length);
330 }
331
332 template <typename T>
333 static inline bool oop_arraycopy_raw(T* src, T* dst, size_t length) {
334 return AccessT::oop_arraycopy(nullptr, 0, src,
335 nullptr, 0, dst,
336 length);
337 }
338
339 };
340
341 template <DecoratorSet decorators>
342 template <DecoratorSet expected_decorators>
343 void Access<decorators>::verify_decorators() {
344 STATIC_ASSERT((~expected_decorators & decorators) == 0); // unexpected decorator used
345 const DecoratorSet barrier_strength_decorators = decorators & AS_DECORATOR_MASK;
346 STATIC_ASSERT(barrier_strength_decorators == 0 || ( // make sure barrier strength decorators are disjoint if set
347 (barrier_strength_decorators ^ AS_NO_KEEPALIVE) == 0 ||
348 (barrier_strength_decorators ^ AS_RAW) == 0 ||
349 (barrier_strength_decorators ^ AS_NORMAL) == 0
350 ));
351 const DecoratorSet ref_strength_decorators = decorators & ON_DECORATOR_MASK;
352 STATIC_ASSERT(ref_strength_decorators == 0 || ( // make sure ref strength decorators are disjoint if set
353 (ref_strength_decorators ^ ON_STRONG_OOP_REF) == 0 ||
354 (ref_strength_decorators ^ ON_WEAK_OOP_REF) == 0 ||
355 (ref_strength_decorators ^ ON_PHANTOM_OOP_REF) == 0 ||
356 (ref_strength_decorators ^ ON_UNKNOWN_OOP_REF) == 0
|
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_OOPS_ACCESS_HPP
26 #define SHARE_OOPS_ACCESS_HPP
27
28 #include "memory/allStatic.hpp"
29 #include "oops/accessBackend.hpp"
30 #include "oops/accessDecorators.hpp"
31 #include "oops/inlineKlass.hpp"
32 #include "oops/oopsHierarchy.hpp"
33 #include "utilities/debug.hpp"
34 #include "utilities/globalDefinitions.hpp"
35
36
37 // = GENERAL =
38 // Access is an API for performing accesses with declarative semantics. Each access can have a number of "decorators".
39 // A decorator is an attribute or property that affects the way a memory access is performed in some way.
40 // There are different groups of decorators. Some have to do with memory ordering, others to do with,
41 // e.g. strength of references, strength of GC barriers, or whether compression should be applied or not.
42 // Some decorators are set at buildtime, such as whether primitives require GC barriers or not, others
43 // at callsites such as whether an access is in the heap or not, and others are resolved at runtime
44 // such as GC-specific barriers and encoding/decoding compressed oops. For more information about what
45 // decorators are available, cf. oops/accessDecorators.hpp.
46 // By pipelining handling of these decorators, the design of the Access API allows separation of concern
47 // over the different orthogonal concerns of decorators, while providing a powerful way of
48 // expressing these orthogonal semantic properties in a unified way.
49 //
50 // == OPERATIONS ==
51 // * load: Load a value from an address.
52 // * load_at: Load a value from an internal pointer relative to a base object.
53 // * store: Store a value at an address.
54 // * store_at: Store a value in an internal pointer relative to a base object.
55 // * atomic_cmpxchg: Atomically compare-and-swap a new value at an address if previous value matched the compared value.
56 // * atomic_cmpxchg_at: Atomically compare-and-swap a new value at an internal pointer address if previous value matched the compared value.
57 // * atomic_xchg: Atomically swap a new value at an address without checking the previous value.
58 // * atomic_xchg_at: Atomically swap a new value at an internal pointer address without checking the previous value.
59 // * arraycopy: Copy data from one heap array to another heap array. The ArrayAccess class has convenience functions for this.
60 // * clone: Clone the contents of an object to a newly allocated object.
61 // * value_copy: Copy the contents of a value type from one heap address to another
62 //
63 // == IMPLEMENTATION ==
64 // Each access goes through the following steps in a template pipeline.
65 // There are essentially 5 steps for each access:
66 // * Step 1: Set default decorators and decay types. This step gets rid of CV qualifiers
67 // and sets default decorators to sensible values.
68 // * Step 2: Reduce types. This step makes sure there is only a single T type and not
69 // multiple types. The P type of the address and T type of the value must
70 // match.
71 // * Step 3: Pre-runtime dispatch. This step checks whether a runtime call can be
72 // avoided, and in that case avoids it (calling raw accesses or
73 // primitive accesses in a build that does not require primitive GC barriers)
74 // * Step 4: Runtime-dispatch. This step performs a runtime dispatch to the corresponding
75 // BarrierSet::AccessBarrier accessor that attaches GC-required barriers
76 // to the access.
77 // * Step 5.a: Barrier resolution. This step is invoked the first time a runtime-dispatch
78 // happens for an access. The appropriate BarrierSet::AccessBarrier accessor
79 // is resolved, then the function pointer is updated to that accessor for
80 // future invocations.
81 // * Step 5.b: Post-runtime dispatch. This step now casts previously unknown types such
106 const DecoratorSet primitive_decorators = (AS_DECORATOR_MASK ^ AS_NO_KEEPALIVE) |
107 IN_HEAP | IS_ARRAY;
108 verify_decorators<expected_mo_decorators | primitive_decorators>();
109 }
110
111 template <DecoratorSet expected_mo_decorators>
112 static void verify_oop_decorators() {
113 const DecoratorSet oop_decorators = AS_DECORATOR_MASK | IN_DECORATOR_MASK |
114 (ON_DECORATOR_MASK ^ ON_UNKNOWN_OOP_REF) | // no unknown oop refs outside of the heap
115 IS_ARRAY | IS_NOT_NULL | IS_DEST_UNINITIALIZED;
116 verify_decorators<expected_mo_decorators | oop_decorators>();
117 }
118
119 template <DecoratorSet expected_mo_decorators>
120 static void verify_heap_oop_decorators() {
121 const DecoratorSet heap_oop_decorators = AS_DECORATOR_MASK | ON_DECORATOR_MASK |
122 IN_HEAP | IS_ARRAY | IS_NOT_NULL | IS_DEST_UNINITIALIZED;
123 verify_decorators<expected_mo_decorators | heap_oop_decorators>();
124 }
125
126 template <DecoratorSet expected_mo_decorators>
127 static void verify_heap_value_decorators() {
128 const DecoratorSet heap_value_decorators = IN_HEAP | IS_DEST_UNINITIALIZED;
129 verify_decorators<expected_mo_decorators | heap_value_decorators>();
130 }
131
132 static const DecoratorSet load_mo_decorators = MO_UNORDERED | MO_RELAXED | MO_ACQUIRE | MO_SEQ_CST;
133 static const DecoratorSet store_mo_decorators = MO_UNORDERED | MO_RELAXED | MO_RELEASE | MO_SEQ_CST;
134 static const DecoratorSet atomic_xchg_mo_decorators = MO_SEQ_CST;
135 static const DecoratorSet atomic_cmpxchg_mo_decorators = MO_RELAXED | MO_SEQ_CST;
136
137 protected:
138 template <typename T>
139 static inline void oop_arraycopy(arrayOop src_obj, size_t src_offset_in_bytes, const T* src_raw,
140 arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
141 size_t length) {
142 verify_decorators<ARRAYCOPY_DECORATOR_MASK | IN_HEAP |
143 AS_DECORATOR_MASK | IS_ARRAY | IS_DEST_UNINITIALIZED>();
144 AccessInternal::arraycopy<decorators | INTERNAL_VALUE_IS_OOP>(src_obj, src_offset_in_bytes, src_raw,
145 dst_obj, dst_offset_in_bytes, dst_raw,
146 length);
147 }
148
149 template <typename T>
150 static inline void arraycopy(arrayOop src_obj, size_t src_offset_in_bytes, const T* src_raw,
151 arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
152 size_t length) {
153 verify_decorators<ARRAYCOPY_DECORATOR_MASK | IN_HEAP |
154 AS_DECORATOR_MASK | IS_ARRAY>();
155 AccessInternal::arraycopy<decorators>(src_obj, src_offset_in_bytes, src_raw,
156 dst_obj, dst_offset_in_bytes, dst_raw,
157 length);
158 }
159
160 public:
161 // Primitive heap accesses
162 static inline AccessInternal::LoadAtProxy<decorators> load_at(oop base, ptrdiff_t offset) {
163 verify_primitive_decorators<load_mo_decorators>();
164 return AccessInternal::LoadAtProxy<decorators>(base, offset);
165 }
166
202 typedef typename AccessInternal::OopOrNarrowOop<T>::type OopType;
203 OopType new_oop_value = new_value;
204 OopType compare_oop_value = compare_value;
205 return AccessInternal::atomic_cmpxchg_at<decorators | INTERNAL_VALUE_IS_OOP>(base, offset, compare_oop_value, new_oop_value);
206 }
207
208 template <typename T>
209 static inline T oop_atomic_xchg_at(oop base, ptrdiff_t offset, T new_value) {
210 verify_heap_oop_decorators<atomic_xchg_mo_decorators>();
211 typedef typename AccessInternal::OopOrNarrowOop<T>::type OopType;
212 OopType new_oop_value = new_value;
213 return AccessInternal::atomic_xchg_at<decorators | INTERNAL_VALUE_IS_OOP>(base, offset, new_oop_value);
214 }
215
216 // Clone an object from src to dst
217 static inline void clone(oop src, oop dst, size_t size) {
218 verify_decorators<IN_HEAP>();
219 AccessInternal::clone<decorators>(src, dst, size);
220 }
221
222 // inline type heap access (when flat)...
223
224 // Copy value type data from src to dst
225 static inline void value_copy(void* src, void* dst, InlineKlass* md, LayoutKind lk) {
226 verify_heap_value_decorators<IN_HEAP>();
227 AccessInternal::value_copy<decorators>(src, dst, md, lk);
228 }
229
230 // Primitive accesses
231 template <typename P>
232 static inline P load(P* addr) {
233 verify_primitive_decorators<load_mo_decorators>();
234 return AccessInternal::load<decorators, P, P>(addr);
235 }
236
237 template <typename P, typename T>
238 static inline void store(P* addr, T value) {
239 verify_primitive_decorators<store_mo_decorators>();
240 AccessInternal::store<decorators>(addr, value);
241 }
242
243 template <typename P, typename T>
244 static inline T atomic_cmpxchg(P* addr, T compare_value, T new_value) {
245 verify_primitive_decorators<atomic_cmpxchg_mo_decorators>();
246 return AccessInternal::atomic_cmpxchg<decorators>(addr, compare_value, new_value);
247 }
248
249 template <typename P, typename T>
320 }
321
322 template <typename T>
323 static inline void arraycopy_to_native(arrayOop src_obj, size_t src_offset_in_bytes,
324 T* dst,
325 size_t length) {
326 AccessT::arraycopy(src_obj, src_offset_in_bytes, static_cast<const T*>(nullptr),
327 nullptr, 0, dst,
328 length);
329 }
330
331 template <typename T>
332 static inline void arraycopy_from_native(const T* src,
333 arrayOop dst_obj, size_t dst_offset_in_bytes,
334 size_t length) {
335 AccessT::arraycopy(nullptr, 0, src,
336 dst_obj, dst_offset_in_bytes, static_cast<T*>(nullptr),
337 length);
338 }
339
340 static inline void oop_arraycopy(arrayOop src_obj, size_t src_offset_in_bytes,
341 arrayOop dst_obj, size_t dst_offset_in_bytes,
342 size_t length) {
343 AccessT::oop_arraycopy(src_obj, src_offset_in_bytes, static_cast<const HeapWord*>(nullptr),
344 dst_obj, dst_offset_in_bytes, static_cast<HeapWord*>(nullptr),
345 length);
346 }
347
348 template <typename T>
349 static inline void oop_arraycopy_raw(T* src, T* dst, size_t length) {
350 AccessT::oop_arraycopy(nullptr, 0, src,
351 nullptr, 0, dst,
352 length);
353 }
354
355 };
356
357 template <DecoratorSet decorators>
358 template <DecoratorSet expected_decorators>
359 void Access<decorators>::verify_decorators() {
360 STATIC_ASSERT((~expected_decorators & decorators) == 0); // unexpected decorator used
361 const DecoratorSet barrier_strength_decorators = decorators & AS_DECORATOR_MASK;
362 STATIC_ASSERT(barrier_strength_decorators == 0 || ( // make sure barrier strength decorators are disjoint if set
363 (barrier_strength_decorators ^ AS_NO_KEEPALIVE) == 0 ||
364 (barrier_strength_decorators ^ AS_RAW) == 0 ||
365 (barrier_strength_decorators ^ AS_NORMAL) == 0
366 ));
367 const DecoratorSet ref_strength_decorators = decorators & ON_DECORATOR_MASK;
368 STATIC_ASSERT(ref_strength_decorators == 0 || ( // make sure ref strength decorators are disjoint if set
369 (ref_strength_decorators ^ ON_STRONG_OOP_REF) == 0 ||
370 (ref_strength_decorators ^ ON_WEAK_OOP_REF) == 0 ||
371 (ref_strength_decorators ^ ON_PHANTOM_OOP_REF) == 0 ||
372 (ref_strength_decorators ^ ON_UNKNOWN_OOP_REF) == 0
|