1 /*
   2  * Copyright (c) 2015, 2017, 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/zPhysicalMemory.inline.hpp"
  26 #include "logging/log.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 #include "utilities/debug.hpp"
  29 
  30 ZPhysicalMemory::ZPhysicalMemory() :
  31     _nsegments(0),
  32     _segments(NULL) {}
  33 
  34 ZPhysicalMemory::ZPhysicalMemory(size_t size) :
  35     _nsegments(0),
  36     _segments(NULL) {
  37   add_segment(ZPhysicalMemorySegment(0, size));
  38 }
  39 
  40 ZPhysicalMemory::ZPhysicalMemory(const ZPhysicalMemorySegment& segment) :
  41     _nsegments(0),
  42     _segments(NULL) {
  43   add_segment(segment);
  44 }
  45 
  46 size_t ZPhysicalMemory::size() const {
  47   size_t size = 0;
  48 
  49   for (size_t i = 0; i < _nsegments; i++) {
  50     size += _segments[i].size();
  51   }
  52 
  53   return size;
  54 }
  55 
  56 void ZPhysicalMemory::add_segment(ZPhysicalMemorySegment segment) {
  57   // Try merge with last segment
  58   if (_nsegments > 0) {
  59     ZPhysicalMemorySegment& last = _segments[_nsegments - 1];
  60     assert(last.end() <= segment.start(), "Segments added out of order");
  61     if (last.end() == segment.start()) {
  62       // Merge
  63       last.expand(segment.size());
  64       return;
  65     }
  66   }
  67 
  68   // Make room for a new segment
  69   const size_t size = sizeof(ZPhysicalMemorySegment) * (_nsegments + 1);
  70   _segments = (ZPhysicalMemorySegment*)ReallocateHeap((char*)_segments, size, mtGC);
  71 
  72   // Add new segment
  73   _segments[_nsegments] = segment;
  74   _nsegments++;
  75 }
  76 
  77 ZPhysicalMemory ZPhysicalMemory::split(size_t split_size) {
  78   // Only splitting of single-segment instances have been implemented.
  79   assert(nsegments() == 1, "Can only have one segment");
  80   assert(split_size <= size(), "Invalid size");
  81   return ZPhysicalMemory(_segments[0].split(split_size));
  82 }
  83 
  84 void ZPhysicalMemory::clear() {
  85   if (_segments != NULL) {
  86     FreeHeap(_segments);
  87     _segments = NULL;
  88     _nsegments = 0;
  89   }
  90 }
  91 
  92 ZPhysicalMemoryManager::ZPhysicalMemoryManager(size_t max_capacity, size_t granule_size) :
  93     _backing(max_capacity, granule_size),
  94     _max_capacity(max_capacity),
  95     _capacity(0),
  96     _used(0) {}
  97 
  98 bool ZPhysicalMemoryManager::is_initialized() const {
  99   return _backing.is_initialized();
 100 }
 101 
 102 bool ZPhysicalMemoryManager::ensure_available(size_t size) {
 103   const size_t unused_capacity = _capacity - _used;
 104   if (unused_capacity >= size) {
 105     // Enough unused capacity available
 106     return true;
 107   }
 108 
 109   const size_t expand_with = size - unused_capacity;
 110   const size_t new_capacity = _capacity + expand_with;
 111   if (new_capacity > _max_capacity) {
 112     // Can not expand beyond max capacity
 113     return false;
 114   }
 115 
 116   // Expand
 117   if (!_backing.expand(_capacity, new_capacity)) {
 118     log_error(gc)("Failed to expand Java heap with " SIZE_FORMAT "%s",
 119                   byte_size_in_proper_unit(expand_with),
 120                   proper_unit_for_byte_size(expand_with));
 121     return false;
 122   }
 123 
 124   _capacity = new_capacity;
 125 
 126   return true;
 127 }
 128 
 129 ZPhysicalMemory ZPhysicalMemoryManager::alloc(size_t size) {
 130   if (!ensure_available(size)) {
 131     // Not enough memory available
 132     return ZPhysicalMemory();
 133   }
 134 
 135   _used += size;
 136   return _backing.alloc(size);
 137 }
 138 
 139 void ZPhysicalMemoryManager::free(ZPhysicalMemory pmem) {
 140   _backing.free(pmem);
 141   _used -= pmem.size();
 142 }
 143 
 144 void ZPhysicalMemoryManager::map(ZPhysicalMemory pmem, uintptr_t offset) {
 145   _backing.map(pmem, offset);
 146 }
 147 
 148 void ZPhysicalMemoryManager::unmap(ZPhysicalMemory pmem, uintptr_t offset) {
 149   _backing.unmap(pmem, offset);
 150 }
 151 
 152 void ZPhysicalMemoryManager::flip(ZPhysicalMemory pmem, uintptr_t offset) {
 153   _backing.flip(pmem, offset);
 154 }