8378115: (fs) sun.nio.fs.NativeBuffer no longer needs a cleaner

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2026-02-23 18:09:31 +00:00
parent acde30e0ab
commit 3b0160f50f
4 changed files with 24 additions and 23 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,9 +25,7 @@
package sun.nio.fs;
import java.lang.ref.Cleaner.Cleanable;
import jdk.internal.misc.Unsafe;
import jdk.internal.ref.CleanerFactory;
/**
* A light-weight buffer in native memory.
@ -38,27 +36,17 @@ class NativeBuffer implements AutoCloseable {
private final long address;
private final int size;
private final Cleanable cleanable;
// optional "owner" to avoid copying
// (only safe for use by thread-local caches)
private Object owner;
private static class Deallocator implements Runnable {
private final long address;
Deallocator(long address) {
this.address = address;
}
public void run() {
unsafe.freeMemory(address);
}
}
// owner thread ID
private long ownerTid;
NativeBuffer(int size) {
this.address = unsafe.allocateMemory(size);
this.size = size;
this.cleanable = CleanerFactory.cleaner()
.register(this, new Deallocator(address));
}
@Override
@ -79,16 +67,26 @@ class NativeBuffer implements AutoCloseable {
}
void free() {
cleanable.clean();
unsafe.freeMemory(address);
}
// not synchronized; only safe for use by thread-local caches
void setOwner(Object owner) {
Thread thread = Thread.currentThread();
assert !thread.isVirtual();
assert ownerTid == 0 || ownerTid == thread.threadId();
this.owner = owner;
this.ownerTid = (owner != null) ? thread.threadId() : 0;
}
// not synchronized; only safe for use by thread-local caches
Object owner() {
return owner;
long tid = Thread.currentThread().threadId();
assert ownerTid == 0 || ownerTid == tid;
if (ownerTid == tid) {
return owner;
} else {
return null;
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -92,7 +92,8 @@ class NativeBuffers {
static NativeBuffer getNativeBuffer(int size) {
NativeBuffer buffer = getNativeBufferFromCache(size);
if (buffer != null) {
buffer.setOwner(null);
if (!Thread.currentThread().isVirtual())
buffer.setOwner(null);
return buffer;
} else {
return allocNativeBuffer(size);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -47,7 +47,8 @@ class UnixNativeDispatcher {
return buffer;
}
NativeBuffers.copyCStringToNativeBuffer(cstr, buffer);
buffer.setOwner(path);
if (!Thread.currentThread().isVirtual())
buffer.setOwner(path);
return buffer;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -1085,7 +1085,8 @@ class WindowsNativeDispatcher {
unsafe.copyMemory(chars, Unsafe.ARRAY_CHAR_BASE_OFFSET, null,
buffer.address(), (long)stringLengthInBytes);
unsafe.putChar(buffer.address() + stringLengthInBytes, (char)0);
buffer.setOwner(s);
if (!Thread.currentThread().isVirtual())
buffer.setOwner(s);
return buffer;
}