Skip to content

Commit 0a4d369

Browse files
committed
Merge branch 'main' into target_is_6.0.X
2 parents 8bab1da + 36b980a commit 0a4d369

File tree

14 files changed

+294
-1109
lines changed

14 files changed

+294
-1109
lines changed

pom.xml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ under the License.
2525

2626
<modelVersion>4.0.0</modelVersion>
2727

28-
<!--
29-
<parent>
30-
<groupId>org.apache</groupId>
31-
<artifactId>apache</artifactId>
32-
<version>32</version>
33-
</parent> -->
34-
3528
<groupId>org.apache.datasketches</groupId>
3629
<artifactId>datasketches-memory</artifactId>
3730
<version>6.0.0</version>
@@ -85,7 +78,7 @@ under the License.
8578

8679
<properties>
8780
<!-- Test -->
88-
<testng.version>7.10.2</testng.version>
81+
<testng.version>7.11.0</testng.version>
8982

9083
<!-- System-wide properties -->
9184
<maven.version>3.6.3</maven.version>
@@ -189,7 +182,7 @@ under the License.
189182
<configuration>
190183
<rules>
191184
<requireJavaVersion>
192-
<version>${java.version}</version>
185+
<version>${java.version},</version>
193186
</requireJavaVersion>
194187
<requireMavenVersion>
195188
<version>[${maven.version},4.0.0)</version>

src/main/java/org/apache/datasketches/memory/DefaultMemoryRequestServer.java

Lines changed: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,39 +27,121 @@
2727
* manage continuous requests for larger or smaller memory.
2828
* This capability is only available for writable, non-file-memory-mapping resources.
2929
*
30+
* <p>The operation of this implementation is controlled by three conditions:</p>
31+
* <ul>
32+
* <li><b><i>origOffHeap:</i></b> If <i>true</i>, the originally allocated WritableMemory is off-heap.</li>
33+
*
34+
* <li><b><i>oneArena:</i></b> If <i>true</i>, all subsequent off-heap allocations will use the same Arena
35+
* obtained from the original off-heap WritableMemory. Otherwise, subsequent off-heap allocations will
36+
* use a new confined Arena created by this implementation.</li>
37+
*
38+
* <li><b><i>offHeap:</i></b> If <i>true</i>, all subsequent allocations will be off-heap.
39+
* If the originally allocated WritableMemory is on-heap, this variable is ignored.</li>
40+
* </ul>
41+
*
42+
* <p>These three variables work together as follows:</p>
43+
*
44+
* <ul>
45+
* <li>If the original WritableMemory is on-heap, all subsequent allocations will also be on-heap.</li>
46+
*
47+
* <li>If <i>origOffHeap</i> = <i>true</i>, <i>oneArena</i> = <i>true</i>, and <i>offHeap</i> = <i>true</i>,
48+
* all subsequent allocations will also be off-heap and associated with the original Arena.
49+
* It is the responsibility of the user to close the original Arena using a Try-With-Resource block, or directly.</li>
50+
*
51+
* <li>If the original WritableMemory is off-heap, <i>oneArena</i> is true, and <i>offHeap</i> is false,
52+
* all subsequent allocations will be on-heap.
53+
* It is the responsibility of the user to close the original Arena using a Try-With-Resource block, or directly.</li>
54+
*
55+
* <li>If the original WritableMemory is off-heap, <i>oneArena</i> is false, and <i>offHeap</i> is true,
56+
* all subsequent allocations will also be off-heap and associated with a new confined Arena assigned by this implementation.
57+
* It is the responsibility of the user to close the original Arena using a Try-With-Resource block, or directly,
58+
* and close the last returned new WritableMemory directly.</li>
59+
* </ul>
60+
*
61+
* <p>In summary:</p>
62+
*
63+
* <table> <caption><b>Configuration Options</b></caption>
64+
* <tr><th>Original Off-Heap</th> <th>OneArena</th> <th>OffHeap</th> <th>Subsequent Allocations</th></tr>
65+
* <tr><td>false</td> <td>N/A</td> <td>N/A</td> <td>All on-heap</td></tr>
66+
* <tr><td>true</td> <td>N/A</td> <td>false</td> <td>All on-heap</td></tr>
67+
* <tr><td>true</td> <td>true</td> <td>true</td> <td>All off-heap in original Arena</td></tr>
68+
* <tr><td>true</td> <td>false</td> <td>true</td> <td>All off-heap in separate Arenas</td></tr>
69+
* </table>
70+
*
3071
* @author Lee Rhodes
3172
*/
73+
@SuppressWarnings("resource") //can't use TWRs here
3274
public final class DefaultMemoryRequestServer implements MemoryRequestServer {
75+
private final long alignmentBytes;
76+
private final ByteOrder byteOrder;
77+
private final boolean oneArena;
78+
private final boolean offHeap;
3379

3480
/**
3581
* Default constructor.
3682
*/
37-
public DefaultMemoryRequestServer() { }
83+
public DefaultMemoryRequestServer() {
84+
alignmentBytes = 8;
85+
byteOrder = ByteOrder.nativeOrder();
86+
oneArena = false;
87+
offHeap = false;
88+
}
3889

39-
@Override
40-
public WritableMemory request(
41-
final long newCapacityBytes,
90+
/**
91+
* Optional constructor 1.
92+
* @param oneArena if true, the original arena will be used for all requested allocations.
93+
* @param offHeap if true, new allocations will be off-heap.
94+
*/
95+
public DefaultMemoryRequestServer(
96+
final boolean oneArena,
97+
final boolean offHeap) {
98+
this.alignmentBytes = 8;
99+
this.byteOrder = ByteOrder.nativeOrder();
100+
this.oneArena = oneArena;
101+
this.offHeap = offHeap;
102+
}
103+
104+
/**
105+
* Optional constructor 2.
106+
* @param alignmentBytes requested segment alignment for all allocations. Typically 1, 2, 4 or 8.
107+
* @param byteOrder the given <i>ByteOrder</i>. It must be non-null.
108+
* @param oneArena if true, the same arena will be used for all requested allocations.
109+
* @param offHeap if true, new allocations will be off-heap.
110+
*/
111+
public DefaultMemoryRequestServer(
42112
final long alignmentBytes,
43113
final ByteOrder byteOrder,
44-
final Arena arena) {
45-
final WritableMemory newWmem;
114+
final boolean oneArena,
115+
final boolean offHeap) {
116+
this.alignmentBytes = alignmentBytes;
117+
this.byteOrder = byteOrder;
118+
this.oneArena = oneArena;
119+
this.offHeap = offHeap;
120+
}
46121

47-
if (arena != null) {
48-
newWmem = WritableMemory.allocateDirect(newCapacityBytes, alignmentBytes, byteOrder, this, arena);
49-
}
50-
else { //On-heap
122+
@Override
123+
public WritableMemory request(
124+
final WritableMemory oldWmem,
125+
final long newCapacityBytes) {
126+
127+
//On-heap
128+
if (oldWmem.getArena() == null || !offHeap) {
51129
if (newCapacityBytes > Integer.MAX_VALUE) {
52130
throw new IllegalArgumentException("Requested capacity exceeds Integer.MAX_VALUE.");
53131
}
54-
newWmem = WritableMemory.allocate((int)newCapacityBytes, byteOrder, this);
132+
return WritableMemory.allocate((int)newCapacityBytes, byteOrder, this);
55133
}
56134

57-
return newWmem;
135+
//Acquire Arena
136+
final Arena arena = (oneArena) ? oldWmem.getArena() : Arena.ofConfined();
137+
return WritableMemory.allocateDirect(newCapacityBytes, alignmentBytes, byteOrder, this, arena);
58138
}
59139

60140
@Override
61-
public void requestClose(final Arena arena) {
62-
if (arena.scope().isAlive()) { arena.close(); }
141+
public void requestClose(final WritableMemory wmemToClose) {
142+
final Arena arena = wmemToClose.getArena();
143+
if (oneArena || arena == null || !arena.scope().isAlive()) { return; } //can't close
144+
arena.close();
63145
}
64146

65147
}

src/main/java/org/apache/datasketches/memory/Memory.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ static Memory wrap(
222222
int lengthBytes,
223223
ByteOrder byteOrder) {
224224
final MemorySegment slice = MemorySegment.ofArray(array).asSlice(offsetBytes, lengthBytes).asReadOnly();
225-
return WritableMemoryImpl.wrapSegmentAsArray(slice, byteOrder, null);
225+
return WritableMemoryImpl.wrapSegment(slice, byteOrder);
226226
}
227227

228228
//intentionally removed wrap(boolean[])
@@ -234,7 +234,7 @@ static Memory wrap(
234234
*/
235235
static Memory wrap(char[] array) {
236236
final MemorySegment seg = MemorySegment.ofArray(array).asReadOnly();
237-
return WritableMemoryImpl.wrapSegmentAsArray(seg, ByteOrder.nativeOrder(), null);
237+
return WritableMemoryImpl.wrapSegment(seg, ByteOrder.nativeOrder());
238238
}
239239

240240
/**
@@ -244,7 +244,7 @@ static Memory wrap(char[] array) {
244244
*/
245245
static Memory wrap(short[] array) {
246246
final MemorySegment seg = MemorySegment.ofArray(array).asReadOnly();
247-
return WritableMemoryImpl.wrapSegmentAsArray(seg, ByteOrder.nativeOrder(), null);
247+
return WritableMemoryImpl.wrapSegment(seg, ByteOrder.nativeOrder());
248248
}
249249

250250
/**
@@ -254,7 +254,7 @@ static Memory wrap(short[] array) {
254254
*/
255255
static Memory wrap(int[] array) {
256256
final MemorySegment seg = MemorySegment.ofArray(array).asReadOnly();
257-
return WritableMemoryImpl.wrapSegmentAsArray(seg, ByteOrder.nativeOrder(), null);
257+
return WritableMemoryImpl.wrapSegment(seg, ByteOrder.nativeOrder());
258258
}
259259

260260
/**
@@ -264,7 +264,7 @@ static Memory wrap(int[] array) {
264264
*/
265265
static Memory wrap(long[] array) {
266266
final MemorySegment seg = MemorySegment.ofArray(array).asReadOnly();
267-
return WritableMemoryImpl.wrapSegmentAsArray(seg, ByteOrder.nativeOrder(), null);
267+
return WritableMemoryImpl.wrapSegment(seg, ByteOrder.nativeOrder());
268268
}
269269

270270
/**
@@ -274,7 +274,7 @@ static Memory wrap(long[] array) {
274274
*/
275275
static Memory wrap(float[] array) {
276276
final MemorySegment seg = MemorySegment.ofArray(array).asReadOnly();
277-
return WritableMemoryImpl.wrapSegmentAsArray(seg, ByteOrder.nativeOrder(), null);
277+
return WritableMemoryImpl.wrapSegment(seg, ByteOrder.nativeOrder());
278278
}
279279

280280
/**
@@ -284,7 +284,7 @@ static Memory wrap(float[] array) {
284284
*/
285285
static Memory wrap(double[] array) {
286286
final MemorySegment seg = MemorySegment.ofArray(array).asReadOnly();
287-
return WritableMemoryImpl.wrapSegmentAsArray(seg, ByteOrder.nativeOrder(), null);
287+
return WritableMemoryImpl.wrapSegment(seg, ByteOrder.nativeOrder());
288288
}
289289
//END OF CONSTRUCTOR-TYPE METHODS
290290

src/main/java/org/apache/datasketches/memory/MemoryRequestServer.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919

2020
package org.apache.datasketches.memory;
2121

22-
import java.lang.foreign.Arena;
23-
import java.nio.ByteOrder;
24-
2522
/**
2623
* The MemoryRequestServer is a callback interface to provide a means to request more or less memory
2724
* for heap and off-heap WritableMemory resources that are not file-memory-mapped backed resources.
@@ -33,26 +30,21 @@
3330
public interface MemoryRequestServer {
3431

3532
/**
36-
* Request new WritableMemory with the given newCapacityBytes.
33+
* Request a new WritableMemory with the given newCapacityBytes.
34+
* @param oldWmem the previous WritableMemory to be possibly closed and which provides an associated Arena
35+
* that may be used for allocating the new WritableMemory.
36+
* If the arena is null, the requested WritableMemory will be on-heap.
3737
* @param newCapacityBytes The capacity being requested.
38-
* @param alignmentBytes requested segment alignment. Typically 1, 2, 4 or 8.
39-
* @param byteOrder the given <i>ByteOrder</i>. It must be non-null.
40-
* @param arena the given arena to manage the new off-heap WritableMemory.
41-
* If arena is null, the requested WritableMemory will be on-heap.
42-
* Warning: This class is not thread-safe. Specifying an Arena that allows multiple threads is not recommended.
38+
*
4339
* @return new WritableMemory with the requested capacity.
4440
*/
45-
WritableMemory request(
46-
long newCapacityBytes,
47-
long alignmentBytes,
48-
ByteOrder byteOrder,
49-
Arena arena);
41+
WritableMemory request(WritableMemory oldWmem, long newCapacityBytes);
5042

5143
/**
52-
* Request to close the area managing all the related resources, if applicable.
53-
* Be careful when you request to close the given Arena, you may be closing other resources as well.
54-
* @param arena the given arena to use to close all its managed resources.
44+
* Request to close the given WritableMemory. If applicable, it will be closed by its associated Arena.
45+
* Be careful. Closing the associated Arena may be closing other resources as well.
46+
* @param wmemToClose the given WritableMemory to close.
5547
*/
56-
void requestClose( Arena arena);
48+
void requestClose(WritableMemory wmemToClose);
5749

5850
}

0 commit comments

Comments
 (0)