1 /*
   2  * Copyright (c) 2015, 2020, 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 #ifndef SHARE_GC_Z_ZPHYSICALMEMORY_HPP
  25 #define SHARE_GC_Z_ZPHYSICALMEMORY_HPP
  26 
  27 #include "gc/z/zMemory.hpp"
  28 #include "memory/allocation.hpp"
  29 #include OS_HEADER(gc/z/zPhysicalMemoryBacking)
  30 
  31 class ZPhysicalMemorySegment : public CHeapObj<mtGC> {
  32 private:
  33   uintptr_t _start;
  34   uintptr_t _end;
  35   bool      _committed;
  36 
  37 public:
  38   ZPhysicalMemorySegment();
  39   ZPhysicalMemorySegment(uintptr_t start, size_t size, bool committed);
  40 
  41   uintptr_t start() const;
  42   uintptr_t end() const;
  43   size_t size() const;
  44 
  45   bool is_committed() const;
  46   void set_committed(bool committed);
  47 };
  48 
  49 class ZPhysicalMemory {
  50 private:
  51   uint32_t                _nsegments_max;
  52   uint32_t                _nsegments;
  53   ZPhysicalMemorySegment* _segments;
  54 
  55   void insert_segment(uint32_t index, uintptr_t start, size_t size, bool committed);
  56   void replace_segment(uint32_t index, uintptr_t start, size_t size, bool committed);
  57   void remove_segment(uint32_t index);
  58 
  59 public:
  60   ZPhysicalMemory();
  61   ZPhysicalMemory(const ZPhysicalMemorySegment& segment);
  62   ZPhysicalMemory(const ZPhysicalMemory& pmem);
  63   const ZPhysicalMemory& operator=(const ZPhysicalMemory& pmem);
  64   ~ZPhysicalMemory();
  65 
  66   bool is_null() const;
  67   size_t size() const;
  68 
  69   uint32_t nsegments() const;
  70   const ZPhysicalMemorySegment& segment(uint32_t index) const;
  71 
  72   void add_segments(const ZPhysicalMemory& pmem);
  73   void transfer_segments(ZPhysicalMemory& pmem);
  74 
  75   void add_segment(const ZPhysicalMemorySegment& segment);
  76   bool commit_segment(uint32_t index, size_t size);
  77   bool uncommit_segment(uint32_t index, size_t size);
  78 
  79   ZPhysicalMemory split(size_t size);
  80   ZPhysicalMemory split_committed();
  81 };
  82 
  83 class ZPhysicalMemoryManager {
  84 private:
  85   ZPhysicalMemoryBacking _backing;
  86   ZMemoryManager         _manager;
  87 
  88   void nmt_commit(const ZPhysicalMemory& pmem, uintptr_t offset) const;
  89   void nmt_uncommit(const ZPhysicalMemory& pmem, uintptr_t offset) const;
  90 
  91   void pretouch_view(uintptr_t addr, size_t size) const;
  92   bool map_view(const ZPhysicalMemory& pmem, uintptr_t addr) const;
  93   void unmap_view(const ZPhysicalMemory& pmem, uintptr_t addr) const;
  94 
  95 public:
  96   ZPhysicalMemoryManager(size_t max_capacity);
  97 
  98   bool is_initialized() const;
  99 
 100   void warn_commit_limits(size_t max_capacity) const;
 101   bool should_enable_uncommit(size_t min_capacity, size_t max_capacity);
 102 
 103   void alloc(ZPhysicalMemory& pmem, size_t size);
 104   void free(const ZPhysicalMemory& pmem);
 105 
 106   bool commit(ZPhysicalMemory& pmem);
 107   bool uncommit(ZPhysicalMemory& pmem);
 108 
 109   void pretouch(uintptr_t offset, size_t size) const;
 110 
 111   bool map(const ZPhysicalMemory& pmem, uintptr_t offset) const;
 112   void unmap(const ZPhysicalMemory& pmem, uintptr_t offset) const;
 113 
 114   void debug_map(const ZPhysicalMemory& pmem, uintptr_t offset) const;
 115   void debug_unmap(const ZPhysicalMemory& pmem, uintptr_t offset) const;
 116 };
 117 
 118 #endif // SHARE_GC_Z_ZPHYSICALMEMORY_HPP