Skip to content

Commit 84e469e

Browse files
Alexey BakhtinRealCLanger
authored andcommitted
8367463: Improved Arena allocations
Reviewed-by: andrew Backport-of: 21e3fb149f1514a1173f1ae2a17abb742c145255
1 parent f485a1a commit 84e469e

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

src/java.base/share/classes/jdk/internal/foreign/SegmentFactories.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,16 @@ private static long allocateNativeInternal(long byteSize, long byteAlignment, Me
190190
if (VM.isDirectMemoryPageAligned()) {
191191
byteAlignment = Math.max(byteAlignment, AbstractMemorySegmentImpl.NIO_ACCESS.pageSize());
192192
}
193+
// Always allocate at least some memory so that zero-length segments have distinct
194+
// non-zero addresses.
195+
byteSize = Math.max(1, byteSize);
196+
193197
// Align the allocation size up to a multiple of 8 so we can init the memory with longs
194198
long alignedSize = init ? Utils.alignUp(byteSize, Long.BYTES) : byteSize;
195199
// Check for wrap around
196200
if (alignedSize < 0) {
197201
throw new OutOfMemoryError();
198202
}
199-
// Always allocate at least some memory so that zero-length segments have distinct
200-
// non-zero addresses.
201-
alignedSize = Math.max(1, alignedSize);
202203

203204
long allocationSize;
204205
long allocationBase;
@@ -224,12 +225,13 @@ private static long allocateNativeInternal(long byteSize, long byteAlignment, Me
224225
if (init) {
225226
initNativeMemory(result, alignedSize);
226227
}
228+
final long cleanupByteSize = byteSize;
227229
sessionImpl.addOrCleanupIfFail(new MemorySessionImpl.ResourceList.ResourceCleanup() {
228230
@Override
229231
public void cleanup() {
230232
UNSAFE.freeMemory(allocationBase);
231233
if (shouldReserve) {
232-
AbstractMemorySegmentImpl.NIO_ACCESS.unreserveMemory(allocationSize, byteSize);
234+
AbstractMemorySegmentImpl.NIO_ACCESS.unreserveMemory(allocationSize, cleanupByteSize);
233235
}
234236
}
235237
});

0 commit comments

Comments
 (0)