Path: blob/master/src/hotspot/share/oops/access.hpp
40951 views
/*1* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_OOPS_ACCESS_HPP25#define SHARE_OOPS_ACCESS_HPP2627#include "memory/allocation.hpp"28#include "oops/accessBackend.hpp"29#include "oops/accessDecorators.hpp"30#include "oops/oopsHierarchy.hpp"31#include "utilities/debug.hpp"32#include "utilities/globalDefinitions.hpp"333435// = GENERAL =36// Access is an API for performing accesses with declarative semantics. Each access can have a number of "decorators".37// A decorator is an attribute or property that affects the way a memory access is performed in some way.38// There are different groups of decorators. Some have to do with memory ordering, others to do with,39// e.g. strength of references, strength of GC barriers, or whether compression should be applied or not.40// Some decorators are set at buildtime, such as whether primitives require GC barriers or not, others41// at callsites such as whether an access is in the heap or not, and others are resolved at runtime42// such as GC-specific barriers and encoding/decoding compressed oops. For more information about what43// decorators are available, cf. oops/accessDecorators.hpp.44// By pipelining handling of these decorators, the design of the Access API allows separation of concern45// over the different orthogonal concerns of decorators, while providing a powerful way of46// expressing these orthogonal semantic properties in a unified way.47//48// == OPERATIONS ==49// * load: Load a value from an address.50// * load_at: Load a value from an internal pointer relative to a base object.51// * store: Store a value at an address.52// * store_at: Store a value in an internal pointer relative to a base object.53// * atomic_cmpxchg: Atomically compare-and-swap a new value at an address if previous value matched the compared value.54// * atomic_cmpxchg_at: Atomically compare-and-swap a new value at an internal pointer address if previous value matched the compared value.55// * atomic_xchg: Atomically swap a new value at an address if previous value matched the compared value.56// * atomic_xchg_at: Atomically swap a new value at an internal pointer address if previous value matched the compared value.57// * arraycopy: Copy data from one heap array to another heap array. The ArrayAccess class has convenience functions for this.58// * clone: Clone the contents of an object to a newly allocated object.59// * resolve: Resolve a stable to-space invariant oop that is guaranteed not to relocate its payload until a subsequent thread transition.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 qualifiers65// and sets default decorators to sensible values.66// * Step 2: Reduce types. This step makes sure there is only a single T type and not67// multiple types. The P type of the address and T type of the value must68// match.69// * Step 3: Pre-runtime dispatch. This step checks whether a runtime call can be70// avoided, and in that case avoids it (calling raw accesses or71// 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 corresponding73// BarrierSet::AccessBarrier accessor that attaches GC-required barriers74// to the access.75// * Step 5.a: Barrier resolution. This step is invoked the first time a runtime-dispatch76// happens for an access. The appropriate BarrierSet::AccessBarrier accessor77// is resolved, then the function pointer is updated to that accessor for78// future invocations.79// * Step 5.b: Post-runtime dispatch. This step now casts previously unknown types such80// as the address type of an oop on the heap (is it oop* or narrowOop*) to81// the appropriate type. It also splits sufficiently orthogonal accesses into82// different functions, such as whether the access involves oops or primitives83// and whether the access is performed on the heap or outside. Then the84// appropriate BarrierSet::AccessBarrier is called to perform the access.85//86// The implementation of step 1-4 resides in in accessBackend.hpp, to allow selected87// accesses to be accessible from only access.hpp, as opposed to access.inline.hpp.88// Steps 5.a and 5.b require knowledge about the GC backends, and therefore needs to89// include the various GC backend .inline.hpp headers. Their implementation resides in90// access.inline.hpp. The accesses that are allowed through the access.hpp file91// must be instantiated in access.cpp using the INSTANTIATE_HPP_ACCESS macro.9293template <DecoratorSet decorators = DECORATORS_NONE>94class Access: public AllStatic {95// This function asserts that if an access gets passed in a decorator outside96// of the expected_decorators, then something is wrong. It additionally checks97// the consistency of the decorators so that supposedly disjoint decorators are indeed98// disjoint. For example, an access can not be both in heap and on root at the99// same time.100template <DecoratorSet expected_decorators>101static void verify_decorators();102103template <DecoratorSet expected_mo_decorators>104static void verify_primitive_decorators() {105const DecoratorSet primitive_decorators = (AS_DECORATOR_MASK ^ AS_NO_KEEPALIVE) |106IN_HEAP | IS_ARRAY;107verify_decorators<expected_mo_decorators | primitive_decorators>();108}109110template <DecoratorSet expected_mo_decorators>111static void verify_oop_decorators() {112const DecoratorSet oop_decorators = AS_DECORATOR_MASK | IN_DECORATOR_MASK |113(ON_DECORATOR_MASK ^ ON_UNKNOWN_OOP_REF) | // no unknown oop refs outside of the heap114IS_ARRAY | IS_NOT_NULL | IS_DEST_UNINITIALIZED;115verify_decorators<expected_mo_decorators | oop_decorators>();116}117118template <DecoratorSet expected_mo_decorators>119static void verify_heap_oop_decorators() {120const DecoratorSet heap_oop_decorators = AS_DECORATOR_MASK | ON_DECORATOR_MASK |121IN_HEAP | IS_ARRAY | IS_NOT_NULL;122verify_decorators<expected_mo_decorators | heap_oop_decorators>();123}124125static const DecoratorSet load_mo_decorators = MO_UNORDERED | MO_RELAXED | MO_ACQUIRE | MO_SEQ_CST;126static const DecoratorSet store_mo_decorators = MO_UNORDERED | MO_RELAXED | MO_RELEASE | MO_SEQ_CST;127static const DecoratorSet atomic_xchg_mo_decorators = MO_SEQ_CST;128static const DecoratorSet atomic_cmpxchg_mo_decorators = MO_RELAXED | MO_SEQ_CST;129130protected:131template <typename T>132static inline bool oop_arraycopy(arrayOop src_obj, size_t src_offset_in_bytes, const T* src_raw,133arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,134size_t length) {135verify_decorators<ARRAYCOPY_DECORATOR_MASK | IN_HEAP |136AS_DECORATOR_MASK | IS_ARRAY | IS_DEST_UNINITIALIZED>();137return AccessInternal::arraycopy<decorators | INTERNAL_VALUE_IS_OOP>(src_obj, src_offset_in_bytes, src_raw,138dst_obj, dst_offset_in_bytes, dst_raw,139length);140}141142template <typename T>143static inline void arraycopy(arrayOop src_obj, size_t src_offset_in_bytes, const T* src_raw,144arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,145size_t length) {146verify_decorators<ARRAYCOPY_DECORATOR_MASK | IN_HEAP |147AS_DECORATOR_MASK | IS_ARRAY>();148AccessInternal::arraycopy<decorators>(src_obj, src_offset_in_bytes, src_raw,149dst_obj, dst_offset_in_bytes, dst_raw,150length);151}152153public:154// Primitive heap accesses155static inline AccessInternal::LoadAtProxy<decorators> load_at(oop base, ptrdiff_t offset) {156verify_primitive_decorators<load_mo_decorators>();157return AccessInternal::LoadAtProxy<decorators>(base, offset);158}159160template <typename T>161static inline void store_at(oop base, ptrdiff_t offset, T value) {162verify_primitive_decorators<store_mo_decorators>();163AccessInternal::store_at<decorators>(base, offset, value);164}165166template <typename T>167static inline T atomic_cmpxchg_at(oop base, ptrdiff_t offset, T compare_value, T new_value) {168verify_primitive_decorators<atomic_cmpxchg_mo_decorators>();169return AccessInternal::atomic_cmpxchg_at<decorators>(base, offset, compare_value, new_value);170}171172template <typename T>173static inline T atomic_xchg_at(oop base, ptrdiff_t offset, T new_value) {174verify_primitive_decorators<atomic_xchg_mo_decorators>();175return AccessInternal::atomic_xchg_at<decorators>(base, offset, new_value);176}177178// Oop heap accesses179static inline AccessInternal::OopLoadAtProxy<decorators> oop_load_at(oop base, ptrdiff_t offset) {180verify_heap_oop_decorators<load_mo_decorators>();181return AccessInternal::OopLoadAtProxy<decorators>(base, offset);182}183184template <typename T>185static inline void oop_store_at(oop base, ptrdiff_t offset, T value) {186verify_heap_oop_decorators<store_mo_decorators>();187typedef typename AccessInternal::OopOrNarrowOop<T>::type OopType;188OopType oop_value = value;189AccessInternal::store_at<decorators | INTERNAL_VALUE_IS_OOP>(base, offset, oop_value);190}191192template <typename T>193static inline T oop_atomic_cmpxchg_at(oop base, ptrdiff_t offset, T compare_value, T new_value) {194verify_heap_oop_decorators<atomic_cmpxchg_mo_decorators>();195typedef typename AccessInternal::OopOrNarrowOop<T>::type OopType;196OopType new_oop_value = new_value;197OopType compare_oop_value = compare_value;198return AccessInternal::atomic_cmpxchg_at<decorators | INTERNAL_VALUE_IS_OOP>(base, offset, compare_oop_value, new_oop_value);199}200201template <typename T>202static inline T oop_atomic_xchg_at(oop base, ptrdiff_t offset, T new_value) {203verify_heap_oop_decorators<atomic_xchg_mo_decorators>();204typedef typename AccessInternal::OopOrNarrowOop<T>::type OopType;205OopType new_oop_value = new_value;206return AccessInternal::atomic_xchg_at<decorators | INTERNAL_VALUE_IS_OOP>(base, offset, new_oop_value);207}208209// Clone an object from src to dst210static inline void clone(oop src, oop dst, size_t size) {211verify_decorators<IN_HEAP>();212AccessInternal::clone<decorators>(src, dst, size);213}214215// Primitive accesses216template <typename P>217static inline P load(P* addr) {218verify_primitive_decorators<load_mo_decorators>();219return AccessInternal::load<decorators, P, P>(addr);220}221222template <typename P, typename T>223static inline void store(P* addr, T value) {224verify_primitive_decorators<store_mo_decorators>();225AccessInternal::store<decorators>(addr, value);226}227228template <typename P, typename T>229static inline T atomic_cmpxchg(P* addr, T compare_value, T new_value) {230verify_primitive_decorators<atomic_cmpxchg_mo_decorators>();231return AccessInternal::atomic_cmpxchg<decorators>(addr, compare_value, new_value);232}233234template <typename P, typename T>235static inline T atomic_xchg(P* addr, T new_value) {236verify_primitive_decorators<atomic_xchg_mo_decorators>();237return AccessInternal::atomic_xchg<decorators>(addr, new_value);238}239240// Oop accesses241template <typename P>242static inline AccessInternal::OopLoadProxy<P, decorators> oop_load(P* addr) {243verify_oop_decorators<load_mo_decorators>();244return AccessInternal::OopLoadProxy<P, decorators>(addr);245}246247template <typename P, typename T>248static inline void oop_store(P* addr, T value) {249verify_oop_decorators<store_mo_decorators>();250typedef typename AccessInternal::OopOrNarrowOop<T>::type OopType;251OopType oop_value = value;252AccessInternal::store<decorators | INTERNAL_VALUE_IS_OOP>(addr, oop_value);253}254255template <typename P, typename T>256static inline T oop_atomic_cmpxchg(P* addr, T compare_value, T new_value) {257verify_oop_decorators<atomic_cmpxchg_mo_decorators>();258typedef typename AccessInternal::OopOrNarrowOop<T>::type OopType;259OopType new_oop_value = new_value;260OopType compare_oop_value = compare_value;261return AccessInternal::atomic_cmpxchg<decorators | INTERNAL_VALUE_IS_OOP>(addr, compare_oop_value, new_oop_value);262}263264template <typename P, typename T>265static inline T oop_atomic_xchg(P* addr, T new_value) {266verify_oop_decorators<atomic_xchg_mo_decorators>();267typedef typename AccessInternal::OopOrNarrowOop<T>::type OopType;268OopType new_oop_value = new_value;269return AccessInternal::atomic_xchg<decorators | INTERNAL_VALUE_IS_OOP>(addr, new_oop_value);270}271};272273// Helper for performing raw accesses (knows only of memory ordering274// atomicity decorators as well as compressed oops)275template <DecoratorSet decorators = DECORATORS_NONE>276class RawAccess: public Access<AS_RAW | decorators> {};277278// Helper for performing normal accesses on the heap. These accesses279// may resolve an accessor on a GC barrier set280template <DecoratorSet decorators = DECORATORS_NONE>281class HeapAccess: public Access<IN_HEAP | decorators> {};282283// Helper for performing normal accesses in roots. These accesses284// may resolve an accessor on a GC barrier set285template <DecoratorSet decorators = DECORATORS_NONE>286class NativeAccess: public Access<IN_NATIVE | decorators> {};287288// Helper for array access.289template <DecoratorSet decorators = DECORATORS_NONE>290class ArrayAccess: public HeapAccess<IS_ARRAY | decorators> {291typedef HeapAccess<IS_ARRAY | decorators> AccessT;292public:293template <typename T>294static inline void arraycopy(arrayOop src_obj, size_t src_offset_in_bytes,295arrayOop dst_obj, size_t dst_offset_in_bytes,296size_t length) {297AccessT::arraycopy(src_obj, src_offset_in_bytes, reinterpret_cast<const T*>(NULL),298dst_obj, dst_offset_in_bytes, reinterpret_cast<T*>(NULL),299length);300}301302template <typename T>303static inline void arraycopy_to_native(arrayOop src_obj, size_t src_offset_in_bytes,304T* dst,305size_t length) {306AccessT::arraycopy(src_obj, src_offset_in_bytes, reinterpret_cast<const T*>(NULL),307NULL, 0, dst,308length);309}310311template <typename T>312static inline void arraycopy_from_native(const T* src,313arrayOop dst_obj, size_t dst_offset_in_bytes,314size_t length) {315AccessT::arraycopy(NULL, 0, src,316dst_obj, dst_offset_in_bytes, reinterpret_cast<T*>(NULL),317length);318}319320static inline bool oop_arraycopy(arrayOop src_obj, size_t src_offset_in_bytes,321arrayOop dst_obj, size_t dst_offset_in_bytes,322size_t length) {323return AccessT::oop_arraycopy(src_obj, src_offset_in_bytes, reinterpret_cast<const HeapWord*>(NULL),324dst_obj, dst_offset_in_bytes, reinterpret_cast<HeapWord*>(NULL),325length);326}327328template <typename T>329static inline bool oop_arraycopy_raw(T* src, T* dst, size_t length) {330return AccessT::oop_arraycopy(NULL, 0, src,331NULL, 0, dst,332length);333}334335};336337template <DecoratorSet decorators>338template <DecoratorSet expected_decorators>339void Access<decorators>::verify_decorators() {340STATIC_ASSERT((~expected_decorators & decorators) == 0); // unexpected decorator used341const DecoratorSet barrier_strength_decorators = decorators & AS_DECORATOR_MASK;342STATIC_ASSERT(barrier_strength_decorators == 0 || ( // make sure barrier strength decorators are disjoint if set343(barrier_strength_decorators ^ AS_NO_KEEPALIVE) == 0 ||344(barrier_strength_decorators ^ AS_RAW) == 0 ||345(barrier_strength_decorators ^ AS_NORMAL) == 0346));347const DecoratorSet ref_strength_decorators = decorators & ON_DECORATOR_MASK;348STATIC_ASSERT(ref_strength_decorators == 0 || ( // make sure ref strength decorators are disjoint if set349(ref_strength_decorators ^ ON_STRONG_OOP_REF) == 0 ||350(ref_strength_decorators ^ ON_WEAK_OOP_REF) == 0 ||351(ref_strength_decorators ^ ON_PHANTOM_OOP_REF) == 0 ||352(ref_strength_decorators ^ ON_UNKNOWN_OOP_REF) == 0353));354const DecoratorSet memory_ordering_decorators = decorators & MO_DECORATOR_MASK;355STATIC_ASSERT(memory_ordering_decorators == 0 || ( // make sure memory ordering decorators are disjoint if set356(memory_ordering_decorators ^ MO_UNORDERED) == 0 ||357(memory_ordering_decorators ^ MO_RELAXED) == 0 ||358(memory_ordering_decorators ^ MO_ACQUIRE) == 0 ||359(memory_ordering_decorators ^ MO_RELEASE) == 0 ||360(memory_ordering_decorators ^ MO_SEQ_CST) == 0361));362const DecoratorSet location_decorators = decorators & IN_DECORATOR_MASK;363STATIC_ASSERT(location_decorators == 0 || ( // make sure location decorators are disjoint if set364(location_decorators ^ IN_NATIVE) == 0 ||365(location_decorators ^ IN_HEAP) == 0366));367}368369#endif // SHARE_OOPS_ACCESS_HPP370371372