1 | /* |
2 | * Copyright (c) 2017, 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_ZGRANULEMAP_INLINE_HPP |
25 | #define SHARE_GC_Z_ZGRANULEMAP_INLINE_HPP |
26 | |
27 | #include "gc/z/zAddress.inline.hpp" |
28 | #include "gc/z/zGlobals.hpp" |
29 | #include "gc/z/zGranuleMap.hpp" |
30 | #include "memory/allocation.inline.hpp" |
31 | |
32 | template <typename T> |
33 | inline ZGranuleMap<T>::ZGranuleMap() : |
34 | _size(ZAddressOffsetMax >> ZGranuleSizeShift), |
35 | _map(MmapArrayAllocator<T>::allocate(_size, mtGC)) {} |
36 | |
37 | template <typename T> |
38 | inline ZGranuleMap<T>::~ZGranuleMap() { |
39 | MmapArrayAllocator<T>::free(_map, _size); |
40 | } |
41 | |
42 | template <typename T> |
43 | inline size_t ZGranuleMap<T>::index_for_addr(uintptr_t addr) const { |
44 | assert(!ZAddress::is_null(addr), "Invalid address" ); |
45 | |
46 | const size_t index = ZAddress::offset(addr) >> ZGranuleSizeShift; |
47 | assert(index < _size, "Invalid index" ); |
48 | |
49 | return index; |
50 | } |
51 | |
52 | template <typename T> |
53 | inline T ZGranuleMap<T>::get(uintptr_t addr) const { |
54 | const size_t index = index_for_addr(addr); |
55 | return _map[index]; |
56 | } |
57 | |
58 | template <typename T> |
59 | inline void ZGranuleMap<T>::put(uintptr_t addr, T value) { |
60 | const size_t index = index_for_addr(addr); |
61 | _map[index] = value; |
62 | } |
63 | |
64 | template <typename T> |
65 | inline void ZGranuleMap<T>::put(uintptr_t addr, size_t size, T value) { |
66 | assert(is_aligned(size, ZGranuleSize), "Misaligned" ); |
67 | |
68 | const size_t start_index = index_for_addr(addr); |
69 | const size_t end_index = start_index + (size >> ZGranuleSizeShift); |
70 | for (size_t index = start_index; index < end_index; index++) { |
71 | _map[index] = value; |
72 | } |
73 | } |
74 | |
75 | template <typename T> |
76 | inline ZGranuleMapIterator<T>::ZGranuleMapIterator(const ZGranuleMap<T>* map) : |
77 | _map(map), |
78 | _next(0) {} |
79 | |
80 | template <typename T> |
81 | inline bool ZGranuleMapIterator<T>::next(T* value) { |
82 | if (_next < _map->_size) { |
83 | *value = _map->_map[_next++]; |
84 | return true; |
85 | } |
86 | |
87 | // End of map |
88 | return false; |
89 | } |
90 | |
91 | template <typename T> |
92 | inline bool ZGranuleMapIterator<T>::next(T** value) { |
93 | if (_next < _map->_size) { |
94 | *value = _map->_map + _next++; |
95 | return true; |
96 | } |
97 | |
98 | // End of map |
99 | return false; |
100 | } |
101 | |
102 | #endif // SHARE_GC_Z_ZGRANULEMAP_INLINE_HPP |
103 | |