1 /*
2  * Copyright (c) 2015, 2018, 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 {
39   friend class VMStructs;
40 
41 private:
42   ZLock                    _lock;
43   ZVirtualMemoryManager    _virtual;
44   ZPhysicalMemoryManager   _physical;
45   ZPageCache               _cache;
46   const size_t             _max_reserve;
47   ZPreMappedMemory         _pre_mapped;
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 max_available(bool no_reserve) const;
62   size_t try_ensure_unused(size_t size, bool no_reserve);
63   size_t try_ensure_unused_for_pre_mapped(size_t size);
64 
65   ZPage* create_page(uint8_t type, size_t size);
66   void map_page(ZPage* page);
67   void detach_page(ZPage* page);
68   void flush_pre_mapped();
69   void flush_cache(size_t size);
70 
71   void check_out_of_memory_during_initialization();
72 
73   ZPage* alloc_page_common_inner(uint8_t type, size_t size, ZAllocationFlags flags);
74   ZPage* alloc_page_common(uint8_t type, size_t size, ZAllocationFlags flags);
75   ZPage* alloc_page_blocking(uint8_t type, size_t size, ZAllocationFlags flags);
76   ZPage* alloc_page_nonblocking(uint8_t type, size_t size, ZAllocationFlags flags);
77 
78   void satisfy_alloc_queue();
79 
80   void detach_memory(const ZVirtualMemory& vmem, ZPhysicalMemory& pmem);
81 
82 public:
83   ZPageAllocator(size_t min_capacity, size_t max_capacity, size_t max_reserve);
84 
85   bool is_initialized() const;
86 
87   size_t max_capacity() const;
88   size_t current_max_capacity() const;
89   size_t capacity() const;
90   size_t max_reserve() const;
91   size_t used_high() const;
92   size_t used_low() const;
93   size_t used() const;
94   size_t allocated() const;
95   size_t reclaimed() const;
96 
97   void reset_statistics();
98 
99   ZPage* alloc_page(uint8_t type, size_t size, ZAllocationFlags flags);
100   void flip_page(ZPage* page);
101   void free_page(ZPage* page, bool reclaimed);
102   void destroy_page(ZPage* page);
103 
104   void flush_detached_pages(ZList<ZPage>* list);
105 
106   void flip_pre_mapped();
107 
108   bool is_alloc_stalled() const;
109   void check_out_of_memory();
110 };
111 
112 #endif // SHARE_GC_Z_ZPAGEALLOCATOR_HPP
113