2019-03-18 11:50:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
|
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SHARE_GC_Z_ZFORWARDING_INLINE_HPP
|
|
|
|
#define SHARE_GC_Z_ZFORWARDING_INLINE_HPP
|
|
|
|
|
2019-03-18 11:50:40 +01:00
|
|
|
#include "gc/z/zAttachedArray.inline.hpp"
|
2019-03-18 11:50:39 +01:00
|
|
|
#include "gc/z/zForwarding.hpp"
|
|
|
|
#include "gc/z/zGlobals.hpp"
|
|
|
|
#include "gc/z/zHash.inline.hpp"
|
2019-03-18 11:50:39 +01:00
|
|
|
#include "gc/z/zHeap.hpp"
|
|
|
|
#include "gc/z/zVirtualMemory.inline.hpp"
|
2019-03-18 11:50:39 +01:00
|
|
|
#include "runtime/atomic.hpp"
|
|
|
|
#include "utilities/debug.hpp"
|
|
|
|
|
|
|
|
inline uintptr_t ZForwarding::start() const {
|
2019-03-18 11:50:39 +01:00
|
|
|
return _virtual.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t ZForwarding::size() const {
|
|
|
|
return _virtual.size();
|
2019-03-18 11:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t ZForwarding::object_alignment_shift() const {
|
|
|
|
return _object_alignment_shift;
|
|
|
|
}
|
|
|
|
|
2019-03-18 11:50:39 +01:00
|
|
|
inline ZPage* ZForwarding::page() const {
|
|
|
|
return _page;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool ZForwarding::is_pinned() const {
|
|
|
|
return Atomic::load(&_pinned);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void ZForwarding::set_pinned() {
|
2019-11-25 12:30:24 +01:00
|
|
|
Atomic::store(&_pinned, true);
|
2019-03-18 11:50:39 +01:00
|
|
|
}
|
|
|
|
|
2019-03-18 11:50:39 +01:00
|
|
|
inline bool ZForwarding::inc_refcount() {
|
|
|
|
uint32_t refcount = Atomic::load(&_refcount);
|
|
|
|
|
|
|
|
while (refcount > 0) {
|
|
|
|
const uint32_t old_refcount = refcount;
|
|
|
|
const uint32_t new_refcount = old_refcount + 1;
|
|
|
|
const uint32_t prev_refcount = Atomic::cmpxchg(new_refcount, &_refcount, old_refcount);
|
|
|
|
if (prev_refcount == old_refcount) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
refcount = prev_refcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool ZForwarding::dec_refcount() {
|
|
|
|
assert(_refcount > 0, "Invalid state");
|
2019-11-25 12:32:07 +01:00
|
|
|
return Atomic::sub(&_refcount, 1u) == 0u;
|
2019-03-18 11:50:39 +01:00
|
|
|
}
|
|
|
|
|
2019-03-18 11:50:39 +01:00
|
|
|
inline bool ZForwarding::retain_page() {
|
|
|
|
return inc_refcount();
|
2019-03-18 11:50:39 +01:00
|
|
|
}
|
|
|
|
|
2019-03-18 11:50:39 +01:00
|
|
|
inline void ZForwarding::release_page() {
|
|
|
|
if (dec_refcount()) {
|
|
|
|
ZHeap::heap()->free_page(_page, true /* reclaimed */);
|
|
|
|
_page = NULL;
|
|
|
|
}
|
2019-03-18 11:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ZForwardingEntry* ZForwarding::entries() const {
|
2019-03-18 11:50:40 +01:00
|
|
|
return _entries(this);
|
2019-03-18 11:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ZForwardingEntry ZForwarding::at(ZForwardingCursor* cursor) const {
|
|
|
|
return Atomic::load(entries() + *cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ZForwardingEntry ZForwarding::first(uintptr_t from_index, ZForwardingCursor* cursor) const {
|
2019-09-11 09:47:42 +02:00
|
|
|
const size_t mask = _entries.length() - 1;
|
|
|
|
const size_t hash = ZHash::uint32_to_uint32((uint32_t)from_index);
|
2019-03-18 11:50:39 +01:00
|
|
|
*cursor = hash & mask;
|
|
|
|
return at(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ZForwardingEntry ZForwarding::next(ZForwardingCursor* cursor) const {
|
2019-09-11 09:47:42 +02:00
|
|
|
const size_t mask = _entries.length() - 1;
|
2019-03-18 11:50:39 +01:00
|
|
|
*cursor = (*cursor + 1) & mask;
|
|
|
|
return at(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ZForwardingEntry ZForwarding::find(uintptr_t from_index) const {
|
|
|
|
ZForwardingCursor dummy;
|
|
|
|
return find(from_index, &dummy);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ZForwardingEntry ZForwarding::find(uintptr_t from_index, ZForwardingCursor* cursor) const {
|
|
|
|
// Reading entries in the table races with the atomic CAS done for
|
|
|
|
// insertion into the table. This is safe because each entry is at
|
2019-03-28 19:43:59 +01:00
|
|
|
// most updated once (from zero to something else).
|
2019-03-18 11:50:39 +01:00
|
|
|
ZForwardingEntry entry = first(from_index, cursor);
|
2019-03-28 19:43:59 +01:00
|
|
|
while (entry.populated()) {
|
2019-03-18 11:50:39 +01:00
|
|
|
if (entry.from_index() == from_index) {
|
|
|
|
// Match found, return matching entry
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = next(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match not found, return empty entry
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uintptr_t ZForwarding::insert(uintptr_t from_index, uintptr_t to_offset, ZForwardingCursor* cursor) {
|
|
|
|
const ZForwardingEntry new_entry(from_index, to_offset);
|
2019-03-18 11:50:40 +01:00
|
|
|
const ZForwardingEntry old_entry; // Empty
|
2019-03-18 11:50:39 +01:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
const ZForwardingEntry prev_entry = Atomic::cmpxchg(new_entry, entries() + *cursor, old_entry);
|
2019-03-28 19:43:59 +01:00
|
|
|
if (!prev_entry.populated()) {
|
2019-03-18 11:50:39 +01:00
|
|
|
// Success
|
|
|
|
return to_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find next empty or matching entry
|
|
|
|
ZForwardingEntry entry = at(cursor);
|
2019-03-28 19:43:59 +01:00
|
|
|
while (entry.populated()) {
|
2019-03-18 11:50:39 +01:00
|
|
|
if (entry.from_index() == from_index) {
|
|
|
|
// Match found, return already inserted address
|
|
|
|
return entry.to_offset();
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = next(cursor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // SHARE_GC_Z_ZFORWARDING_INLINE_HPP
|