src/hotspot/share/gc/z/zNMethodData.cpp
author eosterlund
Tue, 26 Feb 2019 11:38:07 +0100
changeset 53922 00fcc1ef31e8
parent 53921 a590b6107ab3
child 54171 07943af21b96
permissions -rw-r--r--
8219638: ZGC: Free ZNMethodDataOops under a lock Reviewed-by: pliden

/*
 * Copyright (c) 2017, 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.
 */

#include "precompiled.hpp"
#include "gc/z/zLock.inline.hpp"
#include "gc/z/zNMethodData.hpp"
#include "memory/allocation.hpp"
#include "runtime/atomic.hpp"
#include "runtime/orderAccess.hpp"
#include "utilities/align.hpp"
#include "utilities/debug.hpp"
#include "utilities/growableArray.hpp"

size_t ZNMethodDataOops::header_size() {
  const size_t size = sizeof(ZNMethodDataOops);
  assert(is_aligned(size, sizeof(oop*)), "Header misaligned");
  return size;
}

ZNMethodDataOops* ZNMethodDataOops::create(const GrowableArray<oop*>& immediates, bool has_non_immediates) {
  // Allocate memory for the ZNMethodDataOops object
  // plus the immediate oop* array that follows right after.
  const size_t size = ZNMethodDataOops::header_size() + (sizeof(oop*) * immediates.length());
  void* const mem = NEW_C_HEAP_ARRAY(uint8_t, size, mtGC);
  return ::new (mem) ZNMethodDataOops(immediates, has_non_immediates);
}

void ZNMethodDataOops::destroy(ZNMethodDataOops* oops) {
  FREE_C_HEAP_ARRAY(uint8_t, oops);
}

ZNMethodDataOops::ZNMethodDataOops(const GrowableArray<oop*>& immediates, bool has_non_immediates) :
    _nimmediates(immediates.length()),
    _has_non_immediates(has_non_immediates) {
  // Save all immediate oops
  for (size_t i = 0; i < _nimmediates; i++) {
    immediates_begin()[i] = immediates.at(i);
  }
}

size_t ZNMethodDataOops::immediates_count() const {
  return _nimmediates;
}

oop** ZNMethodDataOops::immediates_begin() const {
  // The immediate oop* array starts immediately after this object
  return (oop**)((uintptr_t)this + header_size());
}

oop** ZNMethodDataOops::immediates_end() const {
  return immediates_begin() + immediates_count();
}

bool ZNMethodDataOops::has_non_immediates() const {
  return _has_non_immediates;
}

ZNMethodData::ZNMethodData() :
    _lock(),
    _oops(NULL) {}

ZNMethodData::~ZNMethodData() {
  ZNMethodDataOops::destroy(_oops);
}

ZReentrantLock* ZNMethodData::lock() {
  return &_lock;
}

ZNMethodDataOops* ZNMethodData::oops() const {
  return OrderAccess::load_acquire(&_oops);
}

ZNMethodDataOops* ZNMethodData::swap_oops(ZNMethodDataOops* new_oops) {
  ZLocker<ZReentrantLock> locker(&_lock);
  ZNMethodDataOops* const old_oops = _oops;
  _oops = new_oops;
  return old_oops;
}