Skip to content

Commit

Permalink
fix(core): closure allocation tracking
Browse files Browse the repository at this point in the history
Also now logging which closure registry implementation is being used. On
Linux, libffi switched to static trampolines and the executable address
is now different from the closure address, so the ConcurrentHashMap
implementation is necessary.
  • Loading branch information
Spasi committed Mar 25, 2021
1 parent f489247 commit e4a6cc8
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.lang.reflect.*;
import java.util.concurrent.*;

import static org.lwjgl.system.APIUtil.*;
import static org.lwjgl.system.Checks.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;
Expand Down Expand Up @@ -45,6 +46,8 @@ private interface ClosureRegistry {
}

if (code.get(0) == closure.address()) {
apiLog("Closure Registry: simple");

// When the closure address matches the executable address, we don't have to maintain any mappings.
// We can simply cast the executable address to ffi_closure. This is true on many platforms.
CLOSURE_REGISTRY = new ClosureRegistry() {
Expand All @@ -62,6 +65,8 @@ public FFIClosure remove(long executableAddress) {
}
};
} else {
apiLog("Closure Registry: ConcurrentHashMap");

CLOSURE_REGISTRY = new ClosureRegistry() {
private final ConcurrentHashMap<Long, FFIClosure> map = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -153,10 +158,10 @@ static long create(FFICIF cif, Object instance) {
if (closure == null) {
throw new OutOfMemoryError();
}
executableAddress = code.get(0);
if (DEBUG_ALLOCATOR) {
MemoryManage.DebugAllocator.track(closure.address(), FFIClosure.SIZEOF);
MemoryManage.DebugAllocator.track(executableAddress, FFIClosure.SIZEOF);
}
executableAddress = code.get(0);
}

long user_data = NewGlobalRef(instance);
Expand Down

0 comments on commit e4a6cc8

Please sign in to comment.