1 /*
 2  * Copyright (c) 2017, 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 #include "precompiled.hpp"
25 #include "gc/z/zLock.inline.hpp"
26 #include "gc/z/zNMethodAllocator.hpp"
27 #include "gc/z/zNMethodData.hpp"
28 #include "memory/allocation.hpp"
29 #include "runtime/atomic.hpp"
30 #include "runtime/orderAccess.hpp"
31 #include "utilities/align.hpp"
32 #include "utilities/debug.hpp"
33 #include "utilities/growableArray.hpp"
34 
35 size_t ZNMethodDataOops::header_size() {
36   const size_t size = sizeof(ZNMethodDataOops);
37   assert(is_aligned(size, sizeof(oop*)), "Header misaligned");
38   return size;
39 }
40 
41 ZNMethodDataOops* ZNMethodDataOops::create(const GrowableArray<oop*>& immediates, bool has_non_immediates) {
42   // Allocate memory for the ZNMethodDataOops object
43   // plus the immediate oop* array that follows right after.
44   const size_t size = ZNMethodDataOops::header_size() + (sizeof(oop*) * immediates.length());
45   void* const mem = ZNMethodAllocator::allocate(size);
46   return ::new (mem) ZNMethodDataOops(immediates, has_non_immediates);
47 }
48 
49 void ZNMethodDataOops::destroy(ZNMethodDataOops* oops) {
50   ZNMethodAllocator::free(oops);
51 }
52 
53 ZNMethodDataOops::ZNMethodDataOops(const GrowableArray<oop*>& immediates, bool has_non_immediates) :
54     _nimmediates(immediates.length()),
55     _has_non_immediates(has_non_immediates) {
56   // Save all immediate oops
57   for (size_t i = 0; i < _nimmediates; i++) {
58     immediates_begin()[i] = immediates.at(i);
59   }
60 }
61 
62 size_t ZNMethodDataOops::immediates_count() const {
63   return _nimmediates;
64 }
65 
66 oop** ZNMethodDataOops::immediates_begin() const {
67   // The immediate oop* array starts immediately after this object
68   return (oop**)((uintptr_t)this + header_size());
69 }
70 
71 oop** ZNMethodDataOops::immediates_end() const {
72   return immediates_begin() + immediates_count();
73 }
74 
75 bool ZNMethodDataOops::has_non_immediates() const {
76   return _has_non_immediates;
77 }
78 
79 ZNMethodData::ZNMethodData() :
80     _lock(),
81     _oops(NULL) {}
82 
83 ZNMethodData::~ZNMethodData() {
84   ZNMethodAllocator::free(_oops);
85 }
86 
87 ZReentrantLock* ZNMethodData::lock() {
88   return &_lock;
89 }
90 
91 ZNMethodDataOops* ZNMethodData::oops() const {
92   return OrderAccess::load_acquire(&_oops);
93 }
94 
95 ZNMethodDataOops* ZNMethodData::swap_oops(ZNMethodDataOops* new_oops) {
96   return Atomic::xchg(new_oops, &_oops);
97 }