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