1 /*
2 * Copyright (c) 2025, 2026, 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
25 #ifndef SHARE_CDS_AOTSTREAMEDHEAPWRITER_HPP
26 #define SHARE_CDS_AOTSTREAMEDHEAPWRITER_HPP
27
28 #include "cds/aotMapLogger.hpp"
29 #include "cds/heapShared.hpp"
30 #include "memory/allocation.hpp"
31 #include "memory/allStatic.hpp"
32 #include "oops/oopHandle.hpp"
33 #include "utilities/bitMap.hpp"
34 #include "utilities/exceptions.hpp"
35 #include "utilities/growableArray.hpp"
36 #include "utilities/macros.hpp"
37 #include "utilities/resizableHashTable.hpp"
38
39 class MemRegion;
40
41 #if INCLUDE_CDS_JAVA_HEAP
42 class AOTStreamedHeapWriter : AllStatic {
43 class EmbeddedOopMapper;
44 static GrowableArrayCHeap<u1, mtClassShared>* _buffer;
45
46 // The number of bytes that have written into _buffer (may be smaller than _buffer->length()).
47 static size_t _buffer_used;
48
49 // The bottom of the copy of Heap::roots() inside this->_buffer.
50 static size_t _roots_offset;
51
52 // Offset to the forwarding information
53 static size_t _forwarding_offset;
54
55 // Offset to dfs bounds information
56 static size_t _root_highest_object_index_table_offset;
57
58 static GrowableArrayCHeap<oop, mtClassShared>* _source_objs;
59
60 typedef ResizeableHashTable<size_t, OopHandle,
61 AnyObj::C_HEAP,
62 mtClassShared> BufferOffsetToSourceObjectTable;
63
64 static BufferOffsetToSourceObjectTable* _buffer_offset_to_source_obj_table;
65
66 typedef ResizeableHashTable<void*, int,
67 AnyObj::C_HEAP,
68 mtClassShared> SourceObjectToDFSOrderTable;
69 static SourceObjectToDFSOrderTable* _dfs_order_table;
70
71 static int* _roots_highest_dfs;
72 static size_t* _dfs_to_archive_object_table;
73
74 static int cmp_dfs_order(oop* o1, oop* o2);
75
76 static void allocate_buffer();
77 static void ensure_buffer_space(size_t min_bytes);
78
79 // Both Java bytearray and GrowableArraty use int indices and lengths. Do a safe typecast with range check
80 static int to_array_index(size_t i) {
81 assert(i <= (size_t)max_jint, "must be");
82 return (int)i;
83 }
84 static int to_array_length(size_t n) {
85 return to_array_index(n);
86 }
87
88 template <typename T> static T offset_to_buffered_address(size_t offset) {
89 return (T)(_buffer->adr_at(to_array_index(offset)));
90 }
91
92 static address buffer_bottom() {
93 return offset_to_buffered_address<address>(0);
94 }
95
96 // The exclusive end of the last object that was copied into the buffer.
97 static address buffer_top() {
98 return buffer_bottom() + _buffer_used;
99 }
100
101 static bool in_buffer(address buffered_addr) {
102 return (buffer_bottom() <= buffered_addr) && (buffered_addr < buffer_top());
103 }
104
105 static size_t buffered_address_to_offset(address buffered_addr) {
106 assert(in_buffer(buffered_addr), "sanity");
107 return buffered_addr - buffer_bottom();
108 }
109
110 static void order_source_objs(GrowableArrayCHeap<oop, mtClassShared>* roots);
111 static void copy_roots_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots);
112 static void copy_source_objs_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots);
113 static size_t copy_one_source_obj_to_buffer(oop src_obj);
114
115 template <typename T>
116 static void write(T value);
117 static void copy_forwarding_to_buffer();
118 static void copy_roots_max_dfs_to_buffer(int roots_length);
119
120 static void map_embedded_oops(ArchiveStreamedHeapInfo* info);
121 static bool is_in_requested_range(oop o);
122 static oop requested_obj_from_buffer_offset(size_t offset);
123
124 static oop load_oop_from_buffer(oop* buffered_addr);
125 static oop load_oop_from_buffer(narrowOop* buffered_addr);
126 inline static void store_oop_in_buffer(oop* buffered_addr, int dfs_index);
127 inline static void store_oop_in_buffer(narrowOop* buffered_addr, int dfs_index);
128
129 template <typename T> static void mark_oop_pointer(T* buffered_addr, CHeapBitMap* oopmap);
130 template <typename T> static void map_oop_field_in_buffer(oop obj, T* field_addr_in_buffer, CHeapBitMap* oopmap);
131
132 static void update_header_for_buffered_addr(address buffered_addr, oop src_obj, Klass* src_klass);
133
134 static void populate_archive_heap_info(ArchiveStreamedHeapInfo* info);
135
136 public:
137 static void init() NOT_CDS_JAVA_HEAP_RETURN;
138
139 static void delete_tables_with_raw_oops();
140 static void add_source_obj(oop src_obj);
141 static void write(GrowableArrayCHeap<oop, mtClassShared>*, ArchiveStreamedHeapInfo* heap_info);
142 static address buffered_heap_roots_addr() {
143 return offset_to_buffered_address<address>(_roots_offset);
144 }
145
146 static size_t buffered_addr_to_buffered_offset(address buffered_addr) {
147 assert(buffered_addr != nullptr, "should not be null");
148 return size_t(buffered_addr) - size_t(buffer_bottom());
149 }
150
151 static size_t source_obj_to_buffered_offset(oop src_obj);
152 static address source_obj_to_buffered_addr(oop src_obj);
153
154 static oop buffered_offset_to_source_obj(size_t buffered_offset);
155 static oop buffered_addr_to_source_obj(address buffered_addr);
156
157 static AOTMapLogger::OopDataIterator* oop_iterator(ArchiveStreamedHeapInfo* heap_info);
158 };
159 #endif // INCLUDE_CDS_JAVA_HEAP
160 #endif // SHARE_CDS_AOTSTREAMEDHEAPWRITER_HPP