Skip to content

Commit f5b757c

Browse files
committedJan 15, 2024
8323159: Consider adding some text re. memory zeroing in Arena::allocate
Reviewed-by: mcimadamore, jvernee
1 parent 1f4474f commit f5b757c

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed
 

‎src/java.base/share/classes/java/lang/foreign/Arena.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -219,6 +219,9 @@ public interface Arena extends SegmentAllocator, AutoCloseable {
219219
* Segments allocated with the returned arena can be
220220
* {@linkplain MemorySegment#isAccessibleBy(Thread) accessed} by any thread.
221221
* Calling {@link #close()} on the returned arena will result in an {@link UnsupportedOperationException}.
222+
* <p>
223+
* Memory segments {@linkplain #allocate(long, long) allocated} by the returned arena
224+
* are zero-initialized.
222225
*
223226
* @return a new arena that is managed, automatically, by the garbage collector
224227
*/
@@ -231,6 +234,9 @@ static Arena ofAuto() {
231234
* {@linkplain MemorySegment#isAccessibleBy(Thread) accessed} by any thread.
232235
* Calling {@link #close()} on the returned arena will result in
233236
* an {@link UnsupportedOperationException}.
237+
* <p>
238+
* Memory segments {@linkplain #allocate(long, long) allocated} by the returned arena
239+
* are zero-initialized.
234240
*/
235241
static Arena global() {
236242
class Holder {
@@ -243,6 +249,9 @@ class Holder {
243249
* {@return a new confined arena} Segments allocated with the confined arena can be
244250
* {@linkplain MemorySegment#isAccessibleBy(Thread) accessed} by the thread
245251
* that created the arena, the arena's <em>owner thread</em>.
252+
* <p>
253+
* Memory segments {@linkplain #allocate(long, long) allocated} by the returned arena
254+
* are zero-initialized.
246255
*/
247256
static Arena ofConfined() {
248257
return MemorySessionImpl.createConfined(Thread.currentThread()).asArena();
@@ -251,6 +260,9 @@ static Arena ofConfined() {
251260
/**
252261
* {@return a new shared arena} Segments allocated with the global arena can be
253262
* {@linkplain MemorySegment#isAccessibleBy(Thread) accessed} by any thread.
263+
* <p>
264+
* Memory segments {@linkplain #allocate(long, long) allocated} by the returned arena
265+
* are zero-initialized.
254266
*/
255267
static Arena ofShared() {
256268
return MemorySessionImpl.createShared().asArena();

‎test/jdk/java/foreign/TestScope.java

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2024 Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -31,8 +31,11 @@
3131
import java.lang.foreign.Arena;
3232
import java.lang.foreign.MemorySegment;
3333
import java.lang.foreign.SymbolLookup;
34+
import java.lang.foreign.ValueLayout;
3435
import java.nio.ByteBuffer;
3536
import java.nio.IntBuffer;
37+
import java.util.HexFormat;
38+
import java.util.stream.LongStream;
3639

3740
import static org.testng.Assert.*;
3841

@@ -108,6 +111,30 @@ public void testSameLookupScope() {
108111
testDerivedBufferScope(segment1.reinterpret(10));
109112
}
110113

114+
@Test
115+
public void testZeroedOfAuto() {
116+
testZeroed(Arena.ofAuto());
117+
}
118+
119+
@Test
120+
public void testZeroedGlobal() {
121+
testZeroed(Arena.global());
122+
}
123+
124+
@Test
125+
public void testZeroedOfConfined() {
126+
try (Arena arena = Arena.ofConfined()) {
127+
testZeroed(arena);
128+
}
129+
}
130+
131+
@Test
132+
public void testZeroedOfShared() {
133+
try (Arena arena = Arena.ofShared()) {
134+
testZeroed(arena);
135+
}
136+
}
137+
111138
void testDerivedBufferScope(MemorySegment segment) {
112139
ByteBuffer buffer = segment.asByteBuffer();
113140
MemorySegment.Scope expectedScope = segment.scope();
@@ -119,4 +146,14 @@ void testDerivedBufferScope(MemorySegment segment) {
119146
IntBuffer view = buffer.asIntBuffer();
120147
assertEquals(expectedScope, MemorySegment.ofBuffer(view).scope());
121148
}
149+
150+
private static final MemorySegment ZEROED_MEMORY = MemorySegment.ofArray(new byte[8102]);
151+
152+
void testZeroed(Arena arena) {
153+
long byteSize = ZEROED_MEMORY.byteSize();
154+
var segment = arena.allocate(byteSize, Long.BYTES);
155+
long mismatch = ZEROED_MEMORY.mismatch(segment);
156+
assertEquals(mismatch, -1);
157+
}
158+
122159
}

0 commit comments

Comments
 (0)
Please sign in to comment.