|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
26 | 26 |
|
27 | 27 | import org.graalvm.nativeimage.Platform;
|
28 | 28 | import org.graalvm.nativeimage.Platforms;
|
| 29 | +import org.graalvm.word.LocationIdentity; |
29 | 30 | import org.graalvm.word.Pointer;
|
30 | 31 | import org.graalvm.word.WordFactory;
|
31 | 32 |
|
| 33 | +import com.oracle.svm.core.AlwaysInline; |
32 | 34 | import com.oracle.svm.core.Uninterruptible;
|
33 | 35 | import com.oracle.svm.core.config.ConfigurationValues;
|
34 | 36 | import com.oracle.svm.core.hub.DynamicHub;
|
35 | 37 | import com.oracle.svm.core.image.ImageHeapObject;
|
36 | 38 | import com.oracle.svm.core.snippets.KnownIntrinsics;
|
37 | 39 |
|
38 | 40 | import jdk.graal.compiler.api.replacements.Fold;
|
| 41 | +import jdk.graal.compiler.nodes.NamedLocationIdentity; |
39 | 42 | import jdk.graal.compiler.word.ObjectAccess;
|
40 | 43 | import jdk.graal.compiler.word.Word;
|
| 44 | +import jdk.graal.compiler.word.WordOperationPlugin; |
41 | 45 |
|
42 | 46 | /**
|
43 | 47 | * An object header is a reference-sized collection of bits in each object instance. The object
|
|
49 | 53 | * of this instance if the object has been moved by the collector.
|
50 | 54 | */
|
51 | 55 | public abstract class ObjectHeader {
|
| 56 | + protected static final String INLINE_INITIALIZE_HEADER_INIT_REASON = "Methods that write to INIT_LOCATION must be inlined into a caller that emits an ALLOCATION_INIT barrier."; |
| 57 | + |
52 | 58 | @Platforms(Platform.HOSTED_ONLY.class)
|
53 | 59 | protected ObjectHeader() {
|
54 | 60 | }
|
@@ -107,8 +113,37 @@ public Pointer readPotentialDynamicHubFromPointer(Pointer ptr) {
|
107 | 113 | @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
|
108 | 114 | public abstract Pointer extractPotentialDynamicHubFromHeader(Word header);
|
109 | 115 |
|
| 116 | + /** |
| 117 | + * Initializes the header of a newly allocated heap object (i.e. writing to |
| 118 | + * {@link LocationIdentity#INIT_LOCATION}) |
| 119 | + */ |
| 120 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 121 | + @AlwaysInline(INLINE_INITIALIZE_HEADER_INIT_REASON) |
| 122 | + public final void initializeHeaderOfNewObjectInit(Pointer ptr, Word header, boolean isArrayLike) { |
| 123 | + initializeObjectHeader(ptr, header, isArrayLike, InitLocationMemWriter.INSTANCE); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Initializes the header of a newly allocated object located anywhere in memory (i.e. writing |
| 128 | + * to {@link LocationIdentity#ANY_LOCATION}) |
| 129 | + */ |
110 | 130 | @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
|
111 |
| - public abstract void initializeHeaderOfNewObject(Pointer ptr, Word header, boolean isArrayLike); |
| 131 | + public final void initializeHeaderOfNewObjectAny(Pointer ptr, Word header, boolean isArrayLike) { |
| 132 | + initializeObjectHeader(ptr, header, isArrayLike, AnyLocationMemWriter.INSTANCE); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Initializes the header of a newly allocated object located off the heap (i.e. writing to |
| 137 | + * {@link NamedLocationIdentity#OFF_HEAP_LOCATION}) |
| 138 | + */ |
| 139 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 140 | + public final void initializeHeaderOfNewObjectOffHeap(Pointer ptr, Word header, boolean isArrayLike) { |
| 141 | + initializeObjectHeader(ptr, header, isArrayLike, OffHeapLocationMemWriter.INSTANCE); |
| 142 | + } |
| 143 | + |
| 144 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 145 | + @AlwaysInline(INLINE_INITIALIZE_HEADER_INIT_REASON) |
| 146 | + protected abstract void initializeObjectHeader(Pointer ptr, Word header, boolean isArrayLike, MemWriter writer); |
112 | 147 |
|
113 | 148 | @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
|
114 | 149 | public boolean pointsToObjectHeader(Pointer ptr) {
|
@@ -149,4 +184,94 @@ protected static int getReferenceSize() {
|
149 | 184 | protected static int getHubOffset() {
|
150 | 185 | return ConfigurationValues.getObjectLayout().getHubOffset();
|
151 | 186 | }
|
| 187 | + |
| 188 | + /** |
| 189 | + * Helper interface to write to memory at an overridable {@link LocationIdentity}. |
| 190 | + * <p> |
| 191 | + * {@link #initializeObjectHeader} is used in multiple contexts which write to different memory |
| 192 | + * locations. LocationIdentity arguments to {@link WordOperationPlugin}s are required to be |
| 193 | + * constant at the time of bytecode parsing, meaning it's not possible to {@link Fold} a |
| 194 | + * location identity in any way, it must be a compile-time constant. To avoid duplicating |
| 195 | + * implementations of {@link #initializeObjectHeader}, we get around this by delegating the |
| 196 | + * writing to the MemWriter, whose implementations use different (constant) location identities. |
| 197 | + */ |
| 198 | + protected interface MemWriter { |
| 199 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 200 | + void writeWord(Pointer ptr, int offset, Word word); |
| 201 | + |
| 202 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 203 | + void writeInt(Pointer ptr, int offset, int val); |
| 204 | + |
| 205 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 206 | + void writeLong(Pointer ptr, int offset, long val); |
| 207 | + } |
| 208 | + |
| 209 | + private static final class AnyLocationMemWriter implements MemWriter { |
| 210 | + private static final AnyLocationMemWriter INSTANCE = new AnyLocationMemWriter(); |
| 211 | + |
| 212 | + @Override |
| 213 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 214 | + public void writeWord(Pointer ptr, int offset, Word word) { |
| 215 | + ptr.writeWord(offset, word, LocationIdentity.ANY_LOCATION); |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 220 | + public void writeInt(Pointer ptr, int offset, int val) { |
| 221 | + ptr.writeInt(offset, val, LocationIdentity.ANY_LOCATION); |
| 222 | + } |
| 223 | + |
| 224 | + @Override |
| 225 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 226 | + public void writeLong(Pointer ptr, int offset, long val) { |
| 227 | + ptr.writeLong(offset, val, LocationIdentity.ANY_LOCATION); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + private static final class OffHeapLocationMemWriter implements MemWriter { |
| 232 | + private static final OffHeapLocationMemWriter INSTANCE = new OffHeapLocationMemWriter(); |
| 233 | + |
| 234 | + @Override |
| 235 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 236 | + public void writeWord(Pointer ptr, int offset, Word word) { |
| 237 | + ptr.writeWord(offset, word, NamedLocationIdentity.OFF_HEAP_LOCATION); |
| 238 | + } |
| 239 | + |
| 240 | + @Override |
| 241 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 242 | + public void writeInt(Pointer ptr, int offset, int val) { |
| 243 | + ptr.writeInt(offset, val, NamedLocationIdentity.OFF_HEAP_LOCATION); |
| 244 | + } |
| 245 | + |
| 246 | + @Override |
| 247 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 248 | + public void writeLong(Pointer ptr, int offset, long val) { |
| 249 | + ptr.writeLong(offset, val, NamedLocationIdentity.OFF_HEAP_LOCATION); |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + private static final class InitLocationMemWriter implements MemWriter { |
| 254 | + private static final InitLocationMemWriter INSTANCE = new InitLocationMemWriter(); |
| 255 | + |
| 256 | + @Override |
| 257 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 258 | + @AlwaysInline(INLINE_INITIALIZE_HEADER_INIT_REASON) |
| 259 | + public void writeWord(Pointer ptr, int offset, Word word) { |
| 260 | + ptr.writeWord(offset, word, LocationIdentity.INIT_LOCATION); |
| 261 | + } |
| 262 | + |
| 263 | + @Override |
| 264 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 265 | + @AlwaysInline(INLINE_INITIALIZE_HEADER_INIT_REASON) |
| 266 | + public void writeInt(Pointer ptr, int offset, int val) { |
| 267 | + ptr.writeInt(offset, val, LocationIdentity.INIT_LOCATION); |
| 268 | + } |
| 269 | + |
| 270 | + @Override |
| 271 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 272 | + @AlwaysInline(INLINE_INITIALIZE_HEADER_INIT_REASON) |
| 273 | + public void writeLong(Pointer ptr, int offset, long val) { |
| 274 | + ptr.writeLong(offset, val, LocationIdentity.INIT_LOCATION); |
| 275 | + } |
| 276 | + } |
152 | 277 | }
|
0 commit comments