JDK14/Java14源码在线阅读

JDK14/Java14源码在线阅读 / hotspot / os / windows / gc / z / zBackingFile_windows.cpp
/*
 * Copyright (c) 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/zBackingFile_windows.hpp"
#include "gc/z/zGlobals.hpp"
#include "gc/z/zGranuleMap.inline.hpp"
#include "gc/z/zMapper_windows.hpp"
#include "logging/log.hpp"
#include "runtime/globals.hpp"
#include "utilities/debug.hpp"

// The backing file commits and uncommits physical memory, that can be
// multi-mapped into the virtual address space. To support fine-graned
// committing and uncommitting, each ZGranuleSize chunked is mapped to
// a separate paging file mapping.

ZBackingFile::ZBackingFile() :
    _handles(MaxHeapSize),
    _size(0) {}

size_t ZBackingFile::size() const {
  return _size;
}

HANDLE ZBackingFile::get_handle(uintptr_t offset) const {
  HANDLE const handle = _handles.get(offset);
  assert(handle != 0, "Should be set");
  return handle;
}

void ZBackingFile::put_handle(uintptr_t offset, HANDLE handle) {
  assert(handle != INVALID_HANDLE_VALUE, "Invalid handle");
  assert(_handles.get(offset) == 0, "Should be cleared");
  _handles.put(offset, handle);
}

void ZBackingFile::clear_handle(uintptr_t offset) {
  assert(_handles.get(offset) != 0, "Should be set");
  _handles.put(offset, 0);
}

size_t ZBackingFile::commit_from_paging_file(size_t offset, size_t size) {
  for (size_t i = 0; i < size; i += ZGranuleSize) {
    HANDLE const handle = ZMapper::create_and_commit_paging_file_mapping(ZGranuleSize);
    if (handle == 0) {
      return i;
    }

    put_handle(offset + i, handle);
  }

  return size;
}

size_t ZBackingFile::uncommit_from_paging_file(size_t offset, size_t size) {
  for (size_t i = 0; i < size; i += ZGranuleSize) {
    HANDLE const handle = get_handle(offset + i);
    clear_handle(offset + i);
    ZMapper::close_paging_file_mapping(handle);
  }

  return size;
}

size_t ZBackingFile::commit(size_t offset, size_t length) {
  log_trace(gc, heap)("Committing memory: " SIZE_FORMAT "M-" SIZE_FORMAT "M (" SIZE_FORMAT "M)",
                      offset / M, (offset + length) / M, length / M);

  const size_t committed = commit_from_paging_file(offset, length);

  const size_t end = offset + committed;
  if (end > _size) {
    // Update size
    _size = end;
  }

  return committed;
}

size_t ZBackingFile::uncommit(size_t offset, size_t length) {
  log_trace(gc, heap)("Uncommitting memory: " SIZE_FORMAT "M-" SIZE_FORMAT "M (" SIZE_FORMAT "M)",
                      offset / M, (offset + length) / M, length / M);

  return uncommit_from_paging_file(offset, length);
}

void ZBackingFile::map(uintptr_t addr, size_t size, size_t offset) const {
  assert(is_aligned(offset, ZGranuleSize), "Misaligned");
  assert(is_aligned(addr, ZGranuleSize), "Misaligned");
  assert(is_aligned(size, ZGranuleSize), "Misaligned");

  for (size_t i = 0; i < size; i += ZGranuleSize) {

/**代码未完, 请加载全部代码(NowJava.com).**/
展开阅读全文

关注时代Java

关注时代Java