1 /*
 2  * Copyright (c) 2017, 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 #ifndef SHARE_OOPS_OOPHANDLE_HPP
26 #define SHARE_OOPS_OOPHANDLE_HPP
27 
28 #include "metaprogramming/primitiveConversions.hpp"
29 #include "oops/oopsHierarchy.hpp"
30 
31 class OopStorage;
32 
33 // Simple classes for wrapping oop and atomically accessed oop pointers
34 // stored in OopStorage, or stored in the ClassLoaderData handles area.
35 // These classes help with allocation, release, and NativeAccess loads and
36 // stores with the appropriate barriers.
37 
38 class OopHandle {
39   friend class VMStructs;
40 private:
41   oop* _obj;
42 
43 public:
44   OopHandle() : _obj(NULL) {}
45   explicit OopHandle(oop* w) : _obj(w) {}
46   OopHandle(OopStorage* storage, oop obj);
47 
48   OopHandle(const OopHandle& copy) : _obj(copy._obj) {}
49 
50   OopHandle& operator=(const OopHandle& copy) {
51     // Allow "this" to be junk if copy is empty; needed by initialization of
52     // raw memory in hashtables.
53     assert(is_empty() || copy.is_empty(), "can only copy if empty");
54     _obj = copy._obj;
55     return *this;
56   }
57 
58   inline oop resolve() const;
59   inline oop peek() const;
60 
61   bool is_empty() const { return _obj == NULL; }
62 
63   inline void release(OopStorage* storage);
64 
65   inline void replace(oop obj);
66 
67   inline oop xchg(oop new_value);
68 
69   // Used only for removing handle.
70   oop* ptr_raw() const { return _obj; }
71 };
72 
73 // Convert OopHandle to oop*
74 
75 template<>
76 struct PrimitiveConversions::Translate<OopHandle> : public TrueType {
77   typedef OopHandle Value;
78   typedef oop* Decayed;
79 
80   static Decayed decay(Value x) { return x.ptr_raw(); }
81   static Value recover(Decayed x) { return OopHandle(x); }
82 };
83 
84 #endif // SHARE_OOPS_OOPHANDLE_HPP