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