Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
/ jdk22 Public archive

Commit

Permalink
8323159: Consider adding some text re. memory zeroing in Arena::allocate
Browse files Browse the repository at this point in the history
8323745: Missing comma in copyright header in TestScope

Reviewed-by: mcimadamore, alanb
Backport-of: f5b757ced6b672010ea10575d644d3f9d1728923
  • Loading branch information
minborg committed Jan 17, 2024
1 parent eb2c4b0 commit 887a93b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/java.base/share/classes/java/lang/foreign/Arena.java
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -219,6 +219,9 @@ public interface Arena extends SegmentAllocator, AutoCloseable {
* Segments allocated with the returned arena can be
* {@linkplain MemorySegment#isAccessibleBy(Thread) accessed} by any thread.
* 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
*/
Expand All @@ -231,6 +234,9 @@ static Arena ofAuto() {
* {@linkplain MemorySegment#isAccessibleBy(Thread) accessed} by any thread.
* 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.
*/
static Arena global() {
class Holder {
Expand All @@ -243,6 +249,9 @@ class Holder {
* {@return a new confined arena} Segments allocated with the confined arena can be
* {@linkplain MemorySegment#isAccessibleBy(Thread) accessed} by the thread
* 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() {
return MemorySessionImpl.createConfined(Thread.currentThread()).asArena();
Expand All @@ -251,6 +260,9 @@ static Arena ofConfined() {
/**
* {@return a new shared arena} Segments allocated with the global arena can be
* {@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() {
return MemorySessionImpl.createShared().asArena();
Expand Down
39 changes: 38 additions & 1 deletion test/jdk/java/foreign/TestScope.java
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -31,8 +31,11 @@
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.SymbolLookup;
import java.lang.foreign.ValueLayout;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.HexFormat;
import java.util.stream.LongStream;

import static org.testng.Assert.*;

Expand Down Expand Up @@ -108,6 +111,30 @@ public void testSameLookupScope() {
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) {
ByteBuffer buffer = segment.asByteBuffer();
MemorySegment.Scope expectedScope = segment.scope();
Expand All @@ -119,4 +146,14 @@ void testDerivedBufferScope(MemorySegment segment) {
IntBuffer view = buffer.asIntBuffer();
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);
}

}

1 comment on commit 887a93b

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.