1 /*
   2  * Copyright (c) 2015, 2019, 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_ZPAGEALLOCATOR_HPP
  25 #define SHARE_GC_Z_ZPAGEALLOCATOR_HPP
  26 
  27 #include "gc/z/zAllocationFlags.hpp"
  28 #include "gc/z/zList.hpp"
  29 #include "gc/z/zLock.hpp"
  30 #include "gc/z/zPageCache.hpp"
  31 #include "gc/z/zPhysicalMemory.hpp"
  32 #include "gc/z/zSafeDelete.hpp"
  33 #include "gc/z/zVirtualMemory.hpp"
  34 #include "memory/allocation.hpp"
  35 
  36 class ZPageAllocRequest;
  37 
  38 class ZPageAllocator {
  39   friend class VMStructs;
  40 
  41 private:
  42   ZLock                      _lock;
  43   ZVirtualMemoryManager      _virtual;
  44   ZPhysicalMemoryManager     _physical;
  45   ZPageCache                 _cache;
  46   const size_t               _min_capacity;
  47   const size_t               _max_capacity;
  48   const size_t               _max_reserve;
  49   size_t                     _current_max_capacity;
  50   size_t                     _capacity;
  51   size_t                     _used_high;
  52   size_t                     _used_low;
  53   size_t                     _used;
  54   size_t                     _allocated;
  55   ssize_t                    _reclaimed;
  56   ZList<ZPageAllocRequest>   _queue;
  57   mutable ZSafeDelete<ZPage> _safe_delete;
  58   bool                       _uncommit;
  59   bool                       _initialized;
  60 
  61   static ZPage* const      gc_marker;
  62 
  63   void prime_cache(size_t size);
  64 
  65   void increase_used(size_t size, bool relocation);
  66   void decrease_used(size_t size, bool reclaimed);
  67 
  68   ZPage* create_page(uint8_t type, size_t size);
  69   void destroy_page(ZPage* page);
  70 
  71   size_t max_available(bool no_reserve) const;
  72   bool ensure_available(size_t size, bool no_reserve);
  73   void ensure_uncached_available(size_t size);
  74 
  75   void check_out_of_memory_during_initialization();
  76 
  77   ZPage* alloc_page_common_inner(uint8_t type, size_t size, bool no_reserve);
  78   ZPage* alloc_page_common(uint8_t type, size_t size, ZAllocationFlags flags);
  79   ZPage* alloc_page_blocking(uint8_t type, size_t size, ZAllocationFlags flags);
  80   ZPage* alloc_page_nonblocking(uint8_t type, size_t size, ZAllocationFlags flags);
  81 
  82   size_t flush_cache(ZPageCacheFlushClosure* cl);
  83   void flush_cache_for_allocation(size_t requested);
  84 
  85   void satisfy_alloc_queue();
  86 
  87 public:
  88   ZPageAllocator(size_t min_capacity,
  89                  size_t initial_capacity,
  90                  size_t max_capacity,
  91                  size_t max_reserve);
  92 
  93   bool is_initialized() const;
  94 
  95   size_t min_capacity() const;
  96   size_t max_capacity() const;
  97   size_t current_max_capacity() const;
  98   size_t capacity() const;
  99   size_t max_reserve() const;
 100   size_t used_high() const;
 101   size_t used_low() const;
 102   size_t used() const;
 103   size_t unused() const;
 104   size_t allocated() const;
 105   size_t reclaimed() const;
 106 
 107   void reset_statistics();
 108 
 109   ZPage* alloc_page(uint8_t type, size_t size, ZAllocationFlags flags);
 110   void free_page(ZPage* page, bool reclaimed);
 111 
 112   uint64_t uncommit(uint64_t delay);
 113 
 114   void enable_deferred_delete() const;
 115   void disable_deferred_delete() const;
 116 
 117   void map_page(ZPage* page);
 118   void unmap_all_pages();
 119 
 120   bool is_alloc_stalled() const;
 121   void check_out_of_memory();
 122 };
 123 
 124 #endif // SHARE_GC_Z_ZPAGEALLOCATOR_HPP