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/zPreMappedMemory.hpp"
  33 #include "gc/z/zSafeDelete.hpp"
  34 #include "gc/z/zVirtualMemory.hpp"
  35 #include "memory/allocation.hpp"
  36 
  37 class ZPageAllocRequest;
  38 
  39 class ZPageAllocator {
  40   friend class VMStructs;
  41 
  42 private:
  43   ZLock                      _lock;
  44   ZVirtualMemoryManager      _virtual;
  45   ZPhysicalMemoryManager     _physical;
  46   ZPageCache                 _cache;
  47   const size_t               _max_reserve;
  48   ZPreMappedMemory           _pre_mapped;
  49   size_t                     _used_high;
  50   size_t                     _used_low;
  51   size_t                     _used;
  52   size_t                     _allocated;
  53   ssize_t                    _reclaimed;
  54   ZList<ZPageAllocRequest>   _queue;
  55   mutable ZSafeDelete<ZPage> _safe_delete;
  56 
  57   static ZPage* const      gc_marker;
  58 
  59   void increase_used(size_t size, bool relocation);
  60   void decrease_used(size_t size, bool reclaimed);
  61 
  62   size_t max_available(bool no_reserve) const;
  63   size_t try_ensure_unused(size_t size, bool no_reserve);
  64   size_t try_ensure_unused_for_pre_mapped(size_t size);
  65 
  66   ZPage* create_page(uint8_t type, size_t size);
  67   void destroy_page(ZPage* page);
  68 
  69   void flush_pre_mapped();
  70   void flush_cache(ZPageCacheFlushClosure* cl);
  71   void evict_cache(size_t requested);
  72 
  73   void check_out_of_memory_during_initialization();
  74 
  75   ZPage* alloc_page_common_inner(uint8_t type, size_t size, ZAllocationFlags flags);
  76   ZPage* alloc_page_common(uint8_t type, size_t size, ZAllocationFlags flags);
  77   ZPage* alloc_page_blocking(uint8_t type, size_t size, ZAllocationFlags flags);
  78   ZPage* alloc_page_nonblocking(uint8_t type, size_t size, ZAllocationFlags flags);
  79 
  80   void satisfy_alloc_queue();
  81 
  82   void detach_memory(const ZVirtualMemory& vmem, ZPhysicalMemory& pmem);
  83 
  84 public:
  85   ZPageAllocator(size_t min_capacity, size_t max_capacity, size_t max_reserve);
  86 
  87   bool is_initialized() const;
  88 
  89   size_t max_capacity() const;
  90   size_t current_max_capacity() const;
  91   size_t capacity() const;
  92   size_t max_reserve() const;
  93   size_t used_high() const;
  94   size_t used_low() const;
  95   size_t used() const;
  96   size_t unused() const;
  97   size_t allocated() const;
  98   size_t reclaimed() const;
  99 
 100   void reset_statistics();
 101 
 102   ZPage* alloc_page(uint8_t type, size_t size, ZAllocationFlags flags);
 103   void free_page(ZPage* page, bool reclaimed);
 104 
 105   void enable_deferred_delete() const;
 106   void disable_deferred_delete() const;
 107 
 108   void map_page(ZPage* page);
 109   void unmap_all_pages();
 110 
 111   bool is_alloc_stalled() const;
 112   void check_out_of_memory();
 113 };
 114 
 115 #endif // SHARE_GC_Z_ZPAGEALLOCATOR_HPP