Skip to content

Commit 26aa935

Browse files
Simplifications and cleanups.
Pass a native memory tracking category to all relevant allocations of native memory.
1 parent 925a5a2 commit 26aa935

File tree

73 files changed

+1096
-1289
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1096
-1289
lines changed

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/UnmanagedMemory.java

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -62,8 +62,7 @@ private UnmanagedMemory() {
6262
/**
6363
* Allocates {@code size} bytes of unmanaged memory. The content of the memory is undefined.
6464
* <p>
65-
* If {@code size} is 0, the method is allowed but not required to return the null pointer. This
66-
* method never returns a the null pointer, but instead throws a {@link OutOfMemoryError} when
65+
* This method never returns a null pointer, but instead throws an {@link OutOfMemoryError} when
6766
* allocation fails.
6867
*
6968
* @since 19.0
@@ -76,19 +75,10 @@ public static <T extends PointerBase> T malloc(UnsignedWord size) {
7675
return result;
7776
}
7877

79-
public static <T extends PointerBase> T malloc(UnsignedWord size, int flag) {
80-
T result = ImageSingletons.lookup(UnmanagedMemorySupport.class).malloc(size, flag);
81-
if (result.isNull()) {
82-
throw new OutOfMemoryError("malloc of unmanaged memory");
83-
}
84-
return result;
85-
}
86-
8778
/**
8879
* Allocates {@code size} bytes of unmanaged memory. The content of the memory is undefined.
8980
* <p>
90-
* If {@code size} is 0, the method is allowed but not required to return the null pointer. This
91-
* method never returns a the null pointer, but instead throws a {@link OutOfMemoryError} when
81+
* This method never returns a null pointer, but instead throws an {@link OutOfMemoryError} when
9282
* allocation fails.
9383
*
9484
* @since 19.0
@@ -100,8 +90,7 @@ public static <T extends PointerBase> T malloc(int size) {
10090
/**
10191
* Allocates {@code size} bytes of unmanaged memory. The content of the memory is set to 0.
10292
* <p>
103-
* If {@code size} is 0, the method is allowed but not required to return the null pointer. This
104-
* method never returns a the null pointer, but instead throws a {@link OutOfMemoryError} when
93+
* This method never returns a null pointer, but instead throws an {@link OutOfMemoryError} when
10594
* allocation fails.
10695
*
10796
* @since 19.0
@@ -117,8 +106,7 @@ public static <T extends PointerBase> T calloc(UnsignedWord size) {
117106
/**
118107
* Allocates {@code size} bytes of unmanaged memory. The content of the memory is set to 0.
119108
* <p>
120-
* If {@code size} is 0, the method is allowed but not required to return the null pointer. This
121-
* method never returns a the null pointer, but instead throws a {@link OutOfMemoryError} when
109+
* This method never returns a null pointer, but instead throws an {@link OutOfMemoryError} when
122110
* allocation fails.
123111
*
124112
* @since 19.0
@@ -132,9 +120,8 @@ public static <T extends PointerBase> T calloc(int size) {
132120
* If the new size is larger than the old size, the content of the additional memory is
133121
* undefined.
134122
* <p>
135-
* If {@code size} is 0, the method is allowed but not required to return the null pointer. This
136-
* method never returns a the null pointer, but instead throws a {@link OutOfMemoryError} when
137-
* allocation fails.
123+
* This method never returns a null pointer, but instead throws an {@link OutOfMemoryError} when
124+
* allocation fails. In that case, the old data is not deallocated and remains unchanged.
138125
*
139126
* @since 19.0
140127
*/
@@ -146,29 +133,13 @@ public static <T extends PointerBase> T realloc(T ptr, UnsignedWord size) {
146133
return result;
147134
}
148135

149-
public static <T extends PointerBase> T realloc(T ptr, UnsignedWord size, int flag) {
150-
T result = ImageSingletons.lookup(UnmanagedMemorySupport.class).realloc(ptr, size, flag);
151-
if (result.isNull()) {
152-
throw new OutOfMemoryError("realloc of unmanaged memory");
153-
}
154-
return result;
155-
}
156-
157136
/**
158-
* Frees unmanaged memory that was previously allocated using methods of this class.
137+
* Frees unmanaged memory that was previously allocated using methods of this class. This method
138+
* is a no-op if the given pointer is {@code null}.
159139
*
160140
* @since 19.0
161141
*/
162142
public static void free(PointerBase ptr) {
163143
ImageSingletons.lookup(UnmanagedMemorySupport.class).free(ptr);
164144
}
165-
166-
/**
167-
* Will not attempt to perform any NMT operations. This is crucial for releasing memory
168-
* allocated by C libraries which will not have NMT "malloc headers". If
169-
* {@link UnmanagedMemory#free(PointerBase)} is used instead, a segfault will occur.
170-
*/
171-
public static void untrackedFree(PointerBase ptr) {
172-
ImageSingletons.lookup(UnmanagedMemorySupport.class).untrackedFree(ptr);
173-
}
174145
}

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/impl/UnmanagedMemorySupport.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -43,21 +43,13 @@
4343
import org.graalvm.word.PointerBase;
4444
import org.graalvm.word.UnsignedWord;
4545

46-
/** Implemented by operating-system specific code. */
46+
/** Implemented by platform or operating-system specific code. */
4747
public interface UnmanagedMemorySupport {
4848
<T extends PointerBase> T malloc(UnsignedWord size);
4949

50-
<T extends PointerBase> T malloc(UnsignedWord size, int flag);
51-
5250
<T extends PointerBase> T calloc(UnsignedWord size);
5351

54-
<T extends PointerBase> T calloc(UnsignedWord size, int flag);
55-
5652
<T extends PointerBase> T realloc(T ptr, UnsignedWord size);
5753

58-
<T extends PointerBase> T realloc(T ptr, UnsignedWord size, int flag);
59-
6054
void free(PointerBase ptr);
61-
62-
void untrackedFree(PointerBase ptr);
6355
}

substratevm/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This changelog summarizes major changes to GraalVM Native Image.
66
* (GR-51106) Fields that are accessed via a `VarHandle` or `MethodHandle` are no longer marked as "unsafe accessed" when the `VarHandle`/`MethodHandle` can be fully intrinsified.
77
* (GR-49996) Ensure explicitly set image name (e.g., via `-o imagename`) is not accidentally overwritten by `-jar jarfile` option.
88
* (GR-48683) Together with Red Hat, we added partial support for the JFR event `OldObjectSample`.
9+
* (GR-51851) Together with Red Hat, we added initial support for native memory tracking (`--enable-monitoring=nmt`).
910

1011
## GraalVM for JDK 22 (Internal Version 24.0.0)
1112
* (GR-48304) Red Hat added support for the JFR event ThreadAllocationStatistics.

substratevm/src/com.oracle.svm.agent/src/com/oracle/svm/agent/BreakpointInterceptor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@
6161
import java.util.concurrent.locks.ReentrantLock;
6262
import java.util.function.Supplier;
6363

64-
import com.oracle.svm.core.jni.headers.JNIMode;
65-
import jdk.graal.compiler.core.common.NumUtil;
66-
import jdk.graal.compiler.java.LambdaUtils;
6764
import org.graalvm.nativeimage.StackValue;
6865
import org.graalvm.nativeimage.UnmanagedMemory;
6966
import org.graalvm.nativeimage.c.function.CEntryPoint;
@@ -88,6 +85,7 @@
8885
import com.oracle.svm.core.jni.headers.JNIEnvironment;
8986
import com.oracle.svm.core.jni.headers.JNIFieldId;
9087
import com.oracle.svm.core.jni.headers.JNIMethodId;
88+
import com.oracle.svm.core.jni.headers.JNIMode;
9189
import com.oracle.svm.core.jni.headers.JNINativeMethod;
9290
import com.oracle.svm.core.jni.headers.JNIObjectHandle;
9391
import com.oracle.svm.core.jni.headers.JNIValue;
@@ -106,6 +104,9 @@
106104
import com.oracle.svm.jvmtiagentbase.jvmti.JvmtiInterface;
107105
import com.oracle.svm.jvmtiagentbase.jvmti.JvmtiLocationFormat;
108106

107+
import jdk.graal.compiler.core.common.NumUtil;
108+
import jdk.graal.compiler.java.LambdaUtils;
109+
109110
/**
110111
* Intercepts events of interest via breakpoints in Java code.
111112
* <p>

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixProcessPropertiesSupport.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@
3636
import org.graalvm.nativeimage.c.type.CCharPointer;
3737
import org.graalvm.nativeimage.c.type.CTypeConversion;
3838
import org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder;
39-
import org.graalvm.nativeimage.UnmanagedMemory;
4039
import org.graalvm.word.PointerBase;
4140
import org.graalvm.word.WordFactory;
4241

4342
import com.oracle.svm.core.BaseProcessPropertiesSupport;
4443
import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue;
44+
import com.oracle.svm.core.memory.UntrackedNullableNativeMemory;
4545
import com.oracle.svm.core.posix.headers.Dlfcn;
4646
import com.oracle.svm.core.posix.headers.Signal;
4747
import com.oracle.svm.core.posix.headers.Stdlib;
@@ -103,7 +103,7 @@ public String getObjectFile(PointerBase symbolAddress) {
103103
try {
104104
return CTypeConversion.toJavaString(realpath);
105105
} finally {
106-
UnmanagedMemory.untrackedFree(realpath);
106+
UntrackedNullableNativeMemory.free(realpath);
107107
}
108108
}
109109

@@ -157,14 +157,14 @@ protected static String realpath(String path) {
157157
* pointer to it, so I have to free it.
158158
*/
159159
try (CCharPointerHolder pathHolder = CTypeConversion.toCString(path)) {
160-
final CCharPointer realpathPointer = Stdlib.realpath(pathHolder.get(), WordFactory.nullPointer());
161-
if (realpathPointer.isNull()) {
160+
CCharPointer realpath = Stdlib.realpath(pathHolder.get(), WordFactory.nullPointer());
161+
if (realpath.isNull()) {
162162
/* Failure to find a real path. */
163163
return null;
164164
} else {
165165
/* Success */
166-
final String result = CTypeConversion.toJavaString(realpathPointer);
167-
UnmanagedMemory.untrackedFree(realpathPointer);
166+
String result = CTypeConversion.toJavaString(realpath);
167+
UntrackedNullableNativeMemory.free(realpath);
168168
return result;
169169
}
170170
}

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixRawFileOperationSupport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.graalvm.nativeimage.Platforms;
3333
import org.graalvm.nativeimage.c.type.CCharPointer;
3434
import org.graalvm.nativeimage.c.type.CTypeConversion;
35-
import org.graalvm.nativeimage.impl.UnmanagedMemorySupport;
3635
import org.graalvm.word.Pointer;
3736
import org.graalvm.word.SignedWord;
3837
import org.graalvm.word.UnsignedWord;
@@ -42,6 +41,7 @@
4241
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
4342
import com.oracle.svm.core.feature.InternalFeature;
4443
import com.oracle.svm.core.headers.LibC;
44+
import com.oracle.svm.core.memory.UntrackedNullableNativeMemory;
4545
import com.oracle.svm.core.os.AbstractRawFileOperationSupport;
4646
import com.oracle.svm.core.os.AbstractRawFileOperationSupport.RawFileOperationSupportHolder;
4747
import com.oracle.svm.core.posix.headers.Errno;
@@ -58,7 +58,7 @@ public PosixRawFileOperationSupport(boolean useNativeByteOrder) {
5858
@Override
5959
public CCharPointer allocateCPath(String path) {
6060
byte[] data = path.getBytes();
61-
CCharPointer filename = ImageSingletons.lookup(UnmanagedMemorySupport.class).malloc(WordFactory.unsigned(data.length + 1));
61+
CCharPointer filename = UntrackedNullableNativeMemory.malloc(WordFactory.unsigned(data.length + 1));
6262
if (filename.isNull()) {
6363
return WordFactory.nullPointer();
6464
}

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/UnmanagedMemorySupportImpl.java

Lines changed: 0 additions & 135 deletions
This file was deleted.

0 commit comments

Comments
 (0)