Skip to content

Commit 675e532

Browse files
committed
BridJ: simplify weak hash map nonsense (fixes issue #534)
1 parent 054df33 commit 675e532

3 files changed

Lines changed: 25 additions & 41 deletions

File tree

src/main/java/org/bridj/BridJ.java

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
import java.util.Iterator;
5555
import java.util.List;
5656
import java.util.Map;
57-
import java.util.WeakHashMap;
57+
import java.util.concurrent.ConcurrentHashMap;
5858
import java.util.logging.Level;
5959
import java.util.logging.Logger;
6060
import java.util.regex.*;
@@ -105,8 +105,7 @@ public class BridJ {
105105
static final Map<File, NativeLibrary> librariesByFile = new HashMap<File, NativeLibrary>();
106106
private static NativeEntities orphanEntities = new NativeEntities();
107107
static final Map<Class<?>, BridJRuntime> classRuntimes = new HashMap<Class<?>, BridJRuntime>();
108-
static final Map<Long, NativeObject> strongNativeObjects = new HashMap<Long, NativeObject>(),
109-
weakNativeObjects = new WeakHashMap<Long, NativeObject>();
108+
static final Map<Long, NativeObject> strongNativeObjects = new HashMap<Long, NativeObject>();
110109

111110
public static long sizeOf(Type type) {
112111
Class c = Utils.getClass(type);
@@ -140,25 +139,6 @@ public static long sizeOf(Type type) {
140139
throw new RuntimeException("Unable to compute size of type " + Utils.toString(type));
141140
}
142141

143-
static synchronized void registerNativeObject(NativeObject ob) {
144-
weakNativeObjects.put(Pointer.getAddress(ob, null), ob);
145-
}
146-
/// Caller should display message such as "target was GC'ed. You might need to add a BridJ.protectFromGC(NativeObject), BridJ.unprotectFromGC(NativeObject)
147-
148-
static synchronized NativeObject getNativeObject(long peer) {
149-
NativeObject ob = weakNativeObjects.get(peer);
150-
if (ob == null) {
151-
ob = strongNativeObjects.get(peer);
152-
}
153-
return ob;
154-
}
155-
156-
static synchronized void unregisterNativeObject(NativeObject ob) {
157-
long peer = Pointer.getAddress(ob, null);
158-
weakNativeObjects.remove(peer);
159-
strongNativeObjects.remove(peer);
160-
}
161-
162142
/**
163143
* Keep a hard reference to a native object to avoid its garbage
164144
* collection.<br>
@@ -167,7 +147,6 @@ static synchronized void unregisterNativeObject(NativeObject ob) {
167147
*/
168148
public static synchronized <T extends NativeObject> T protectFromGC(T ob) {
169149
long peer = Pointer.getAddress(ob, null);
170-
weakNativeObjects.remove(peer);
171150
strongNativeObjects.put(peer, ob);
172151
return ob;
173152
}
@@ -178,14 +157,15 @@ public static synchronized <T extends NativeObject> T protectFromGC(T ob) {
178157
*/
179158
public static synchronized <T extends NativeObject> T unprotectFromGC(T ob) {
180159
long peer = Pointer.getAddress(ob, null);
181-
if (strongNativeObjects.remove(peer) != null) {
182-
weakNativeObjects.put(peer, ob);
160+
NativeObject removed = strongNativeObjects.remove(peer);
161+
if (removed != ob) {
162+
throw new IllegalStateException("Unprotected object " + removed + " instead of " + ob + " for address " + peer);
183163
}
184164
return ob;
185165
}
186166

187167
public static void delete(NativeObject nativeObject) {
188-
unregisterNativeObject(nativeObject);
168+
BridJ.setJavaObjectFromNativePeer(Pointer.getAddress(nativeObject, null), null);
189169
Pointer.getPointer(nativeObject, null).release();
190170
}
191171

@@ -253,18 +233,21 @@ public static boolean isCastingNativeObjectInCurrentThread() {
253233
public static boolean isCastingNativeObjectReturnTypeInCurrentThread() {
254234
return currentlyCastingNativeObject.get().peek() == CastingType.CastingNativeObjectReturnType;
255235
}
256-
private static WeakHashMap<Long, NativeObject> knownNativeObjects = new WeakHashMap<Long, NativeObject>();
236+
237+
238+
private static final ConcurrentHashMap<Long, NativeObject> registeredObjects =
239+
new ConcurrentHashMap<Long, NativeObject>();
257240

258241
public static synchronized <O extends NativeObject> void setJavaObjectFromNativePeer(long peer, O object) {
259242
if (object == null) {
260-
knownNativeObjects.remove(peer);
243+
registeredObjects.remove(peer);
261244
} else {
262-
knownNativeObjects.put(peer, object);
245+
registeredObjects.put(peer, object);
263246
}
264247
}
265248

266249
public static synchronized Object getJavaObjectFromNativePeer(long peer) {
267-
return knownNativeObjects.get(peer);
250+
return registeredObjects.get(peer);
268251
}
269252

270253
private static <O extends NativeObject> O createNativeObjectFromPointer(Pointer<? super O> pointer, Type type, CastingType castingType) {
@@ -628,7 +611,6 @@ public static synchronized NativeLibrary getNativeLibrary(AnnotatedElement type)
628611
*/
629612
public synchronized static void releaseAll() {
630613
strongNativeObjects.clear();
631-
weakNativeObjects.clear();
632614
gc();
633615

634616
for (NativeLibrary lib : librariesByFile.values()) {
@@ -856,6 +838,9 @@ public static File getNativeLibraryFile(String libraryName) {
856838
try {
857839
synchronized (nativeLibraryFiles) {
858840
File nativeLibraryFile = nativeLibraryFiles.get(libraryName);
841+
if (debug) {
842+
info("Library named '" + libraryName + "' is associated to file '" + nativeLibraryFiles + "'", null);
843+
}
859844
if (nativeLibraryFile == null) {
860845
nativeLibraryFiles.put(libraryName, nativeLibraryFile = findNativeLibraryFile(libraryName));
861846
}
@@ -931,6 +916,10 @@ static File findNativeLibraryFile(String libraryName) throws IOException {
931916
}
932917
}
933918
List<String> possibleFileNames = getPossibleFileNames(name);
919+
if (debug) {
920+
info("Possible file names for library '" + libraryName + "' with name '" + name + "': " + possibleFileNames, null);
921+
}
922+
934923
for (String path : paths) {
935924
File pathFile = path == null ? null : new File(path);
936925
File f = new File(name);

src/main/java/org/bridj/NativeObject.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ protected NativeObject() {
5353
protected NativeObject(int constructorId, Object... args) {
5454
BridJ.initialize(this, constructorId, args);
5555
}
56-
/*
57-
@Override
58-
protected void finalize() throws Throwable {
59-
BridJ.deallocate(this);
60-
}*/
6156

6257
public NativeObject clone() throws CloneNotSupportedException {
6358
return BridJ.clone(this);

src/main/java/org/bridj/cpp/CPPObject.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ protected CPPObject(Pointer<? extends CPPObject> peer, Object... targs) {
6363
protected CPPObject(Void voidArg, int constructorId, Object... args) {
6464
super(voidArg, constructorId, args);
6565
}
66-
/*
67-
@Override
68-
protected void finalize() throws Throwable {
69-
BridJ.deallocate(this);
70-
}*/
66+
67+
@Override
68+
protected void finalize() throws Throwable {
69+
BridJ.setJavaObjectFromNativePeer(peer.getPeer(), null);
70+
}
7171
}

0 commit comments

Comments
 (0)