Skip to content

Commit afdb539

Browse files
committed
Harden native buffer allocators against allocation and cleanup failures
1 parent f4dfc14 commit afdb539

4 files changed

Lines changed: 33 additions & 19 deletions

File tree

jme3-android/src/main/java/com/jme3/util/AndroidNativeBufferAllocator.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,27 @@ public void destroyDirectBuffer(Buffer toBeDestroyed) {
7777
@Override
7878
public ByteBuffer allocate(int size) {
7979
ByteBuffer buffer = createDirectByteBuffer(size);
80-
if (buffer != null) {
81-
long address = directBufferAddress(buffer);
82-
if (address != 0L) {
83-
DEALLOCATORS.put(address, new Deallocator(buffer, address));
84-
}
80+
if (buffer == null) {
81+
throw new OutOfMemoryError("Could not allocate " + size + " bytes through Android native allocator");
82+
}
83+
long address = directBufferAddress(buffer);
84+
if (address != 0L) {
85+
DEALLOCATORS.put(address, new Deallocator(buffer, address));
8586
}
8687
return buffer;
8788
}
8889

8990
private static void freeCollectedBuffers() {
90-
try {
91-
for (;;) {
91+
for (;;) {
92+
try {
9293
Deallocator deallocator = (Deallocator) REFERENCE_QUEUE.remove();
9394
deallocator.freeNow();
95+
} catch (InterruptedException exception) {
96+
Thread.currentThread().interrupt();
97+
break;
98+
} catch (Throwable throwable) {
99+
LOGGER.log(Level.SEVERE, "Error deallocating direct buffer", throwable);
94100
}
95-
} catch (InterruptedException exception) {
96-
Thread.currentThread().interrupt();
97101
}
98102
}
99103

jme3-ios/src/main/java/com/jme3/util/LibJGLIOSNativeBufferAllocator.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,16 @@ public ByteBuffer allocate(int size) {
5757
}
5858

5959
private static void freeCollectedBuffers() {
60-
try {
61-
for (;;) {
60+
for (;;) {
61+
try {
6262
Deallocator deallocator = (Deallocator) REFERENCE_QUEUE.remove();
6363
deallocator.freeNow();
64+
} catch (InterruptedException exception) {
65+
Thread.currentThread().interrupt();
66+
break;
67+
} catch (Throwable throwable) {
68+
LOGGER.log(Level.SEVERE, "Error deallocating direct buffer", throwable);
6469
}
65-
} catch (InterruptedException exception) {
66-
Thread.currentThread().interrupt();
6770
}
6871
}
6972

jme3-lwjgl3/src/main/java/com/jme3/util/LWJGLBufferAllocator.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,16 @@ void free() {
100100
* Free unnecessary LWJGL byte buffers.
101101
*/
102102
static void freeByteBuffers() {
103-
try {
104-
for (;;) {
103+
for (;;) {
104+
try {
105105
final Deallocator deallocator = (Deallocator) DUMMY_QUEUE.remove();
106106
deallocator.free();
107+
} catch (final InterruptedException e) {
108+
Thread.currentThread().interrupt();
109+
break;
110+
} catch (final Throwable throwable) {
111+
LOGGER.log(Level.SEVERE, "Error deallocating direct buffer", throwable);
107112
}
108-
} catch (final InterruptedException e) {
109-
e.printStackTrace();
110113
}
111114
}
112115

@@ -161,7 +164,7 @@ long getAddress(final Buffer buffer) {
161164

162165
@Override
163166
public ByteBuffer allocate(final int size) {
164-
final Long address = MemoryUtil.nmemCalloc(size, 1);
167+
final Long address = MemoryUtil.nmemCallocChecked(size, 1);
165168
final ByteBuffer byteBuffer = MemoryUtil.memByteBuffer(address, size);
166169
DEALLOCATORS.put(address, createDeallocator(address, byteBuffer));
167170
return byteBuffer;

jme3-saferallocator/src/main/java/com/jme3/util/SaferBufferAllocator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,11 @@ public static void alignedFree(long ptr) {
200200
@Override
201201
public ByteBuffer allocate(int size) {
202202
SaferAllocMemoryGuard.beforeAlloc(size);
203-
return register(SaferAlloc.calloc(1, size));
203+
ByteBuffer buffer = SaferAlloc.calloc(1, size);
204+
if (buffer == null) {
205+
throw new OutOfMemoryError("Could not allocate " + size + " bytes through SaferAlloc");
206+
}
207+
return register(buffer);
204208
}
205209

206210
@Override

0 commit comments

Comments
 (0)