mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8323159: Consider adding some text re. memory zeroing in Arena::allocate
Reviewed-by: mcimadamore, jvernee
This commit is contained in:
parent
1f4474f677
commit
f5b757ced6
2 changed files with 51 additions and 2 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -219,6 +219,9 @@ public interface Arena extends SegmentAllocator, AutoCloseable {
|
||||||
* Segments allocated with the returned arena can be
|
* Segments allocated with the returned arena can be
|
||||||
* {@linkplain MemorySegment#isAccessibleBy(Thread) accessed} by any thread.
|
* {@linkplain MemorySegment#isAccessibleBy(Thread) accessed} by any thread.
|
||||||
* Calling {@link #close()} on the returned arena will result in an {@link UnsupportedOperationException}.
|
* Calling {@link #close()} on the returned arena will result in an {@link UnsupportedOperationException}.
|
||||||
|
* <p>
|
||||||
|
* Memory segments {@linkplain #allocate(long, long) allocated} by the returned arena
|
||||||
|
* are zero-initialized.
|
||||||
*
|
*
|
||||||
* @return a new arena that is managed, automatically, by the garbage collector
|
* @return a new arena that is managed, automatically, by the garbage collector
|
||||||
*/
|
*/
|
||||||
|
@ -231,6 +234,9 @@ public interface Arena extends SegmentAllocator, AutoCloseable {
|
||||||
* {@linkplain MemorySegment#isAccessibleBy(Thread) accessed} by any thread.
|
* {@linkplain MemorySegment#isAccessibleBy(Thread) accessed} by any thread.
|
||||||
* Calling {@link #close()} on the returned arena will result in
|
* Calling {@link #close()} on the returned arena will result in
|
||||||
* an {@link UnsupportedOperationException}.
|
* an {@link UnsupportedOperationException}.
|
||||||
|
* <p>
|
||||||
|
* Memory segments {@linkplain #allocate(long, long) allocated} by the returned arena
|
||||||
|
* are zero-initialized.
|
||||||
*/
|
*/
|
||||||
static Arena global() {
|
static Arena global() {
|
||||||
class Holder {
|
class Holder {
|
||||||
|
@ -243,6 +249,9 @@ public interface Arena extends SegmentAllocator, AutoCloseable {
|
||||||
* {@return a new confined arena} Segments allocated with the confined arena can be
|
* {@return a new confined arena} Segments allocated with the confined arena can be
|
||||||
* {@linkplain MemorySegment#isAccessibleBy(Thread) accessed} by the thread
|
* {@linkplain MemorySegment#isAccessibleBy(Thread) accessed} by the thread
|
||||||
* that created the arena, the arena's <em>owner thread</em>.
|
* that created the arena, the arena's <em>owner thread</em>.
|
||||||
|
* <p>
|
||||||
|
* Memory segments {@linkplain #allocate(long, long) allocated} by the returned arena
|
||||||
|
* are zero-initialized.
|
||||||
*/
|
*/
|
||||||
static Arena ofConfined() {
|
static Arena ofConfined() {
|
||||||
return MemorySessionImpl.createConfined(Thread.currentThread()).asArena();
|
return MemorySessionImpl.createConfined(Thread.currentThread()).asArena();
|
||||||
|
@ -251,6 +260,9 @@ public interface Arena extends SegmentAllocator, AutoCloseable {
|
||||||
/**
|
/**
|
||||||
* {@return a new shared arena} Segments allocated with the global arena can be
|
* {@return a new shared arena} Segments allocated with the global arena can be
|
||||||
* {@linkplain MemorySegment#isAccessibleBy(Thread) accessed} by any thread.
|
* {@linkplain MemorySegment#isAccessibleBy(Thread) accessed} by any thread.
|
||||||
|
* <p>
|
||||||
|
* Memory segments {@linkplain #allocate(long, long) allocated} by the returned arena
|
||||||
|
* are zero-initialized.
|
||||||
*/
|
*/
|
||||||
static Arena ofShared() {
|
static Arena ofShared() {
|
||||||
return MemorySessionImpl.createShared().asArena();
|
return MemorySessionImpl.createShared().asArena();
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2023, 2024 Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -31,8 +31,11 @@ import org.testng.annotations.*;
|
||||||
import java.lang.foreign.Arena;
|
import java.lang.foreign.Arena;
|
||||||
import java.lang.foreign.MemorySegment;
|
import java.lang.foreign.MemorySegment;
|
||||||
import java.lang.foreign.SymbolLookup;
|
import java.lang.foreign.SymbolLookup;
|
||||||
|
import java.lang.foreign.ValueLayout;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.IntBuffer;
|
import java.nio.IntBuffer;
|
||||||
|
import java.util.HexFormat;
|
||||||
|
import java.util.stream.LongStream;
|
||||||
|
|
||||||
import static org.testng.Assert.*;
|
import static org.testng.Assert.*;
|
||||||
|
|
||||||
|
@ -108,6 +111,30 @@ public class TestScope {
|
||||||
testDerivedBufferScope(segment1.reinterpret(10));
|
testDerivedBufferScope(segment1.reinterpret(10));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testZeroedOfAuto() {
|
||||||
|
testZeroed(Arena.ofAuto());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testZeroedGlobal() {
|
||||||
|
testZeroed(Arena.global());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testZeroedOfConfined() {
|
||||||
|
try (Arena arena = Arena.ofConfined()) {
|
||||||
|
testZeroed(arena);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testZeroedOfShared() {
|
||||||
|
try (Arena arena = Arena.ofShared()) {
|
||||||
|
testZeroed(arena);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void testDerivedBufferScope(MemorySegment segment) {
|
void testDerivedBufferScope(MemorySegment segment) {
|
||||||
ByteBuffer buffer = segment.asByteBuffer();
|
ByteBuffer buffer = segment.asByteBuffer();
|
||||||
MemorySegment.Scope expectedScope = segment.scope();
|
MemorySegment.Scope expectedScope = segment.scope();
|
||||||
|
@ -119,4 +146,14 @@ public class TestScope {
|
||||||
IntBuffer view = buffer.asIntBuffer();
|
IntBuffer view = buffer.asIntBuffer();
|
||||||
assertEquals(expectedScope, MemorySegment.ofBuffer(view).scope());
|
assertEquals(expectedScope, MemorySegment.ofBuffer(view).scope());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final MemorySegment ZEROED_MEMORY = MemorySegment.ofArray(new byte[8102]);
|
||||||
|
|
||||||
|
void testZeroed(Arena arena) {
|
||||||
|
long byteSize = ZEROED_MEMORY.byteSize();
|
||||||
|
var segment = arena.allocate(byteSize, Long.BYTES);
|
||||||
|
long mismatch = ZEROED_MEMORY.mismatch(segment);
|
||||||
|
assertEquals(mismatch, -1);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue