Skip to content

Commit da14cbb

Browse files
committed
factor out cache clearing to Util helper
1 parent 85e99ee commit da14cbb

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

src/jvm/clojure/lang/DynamicClassLoader.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,9 @@ public DynamicClassLoader(ClassLoader parent){
4141
}
4242

4343
public Class defineClass(String name, byte[] bytes, Object srcForm){
44+
Util.clearCache(rq, classCache);
4445
Class c = defineClass(name, bytes, 0, bytes.length);
4546
classCache.put(name, new SoftReference(c,rq));
46-
//cleanup any dead entries
47-
if(rq.poll() != null)
48-
{
49-
while(rq.poll() != null)
50-
;
51-
for(Map.Entry<String,SoftReference<Class>> e : classCache.entrySet())
52-
{
53-
if(e.getValue().get() == null)
54-
classCache.remove(e.getKey(), e.getValue());
55-
}
56-
}
5747
return c;
5848
}
5949

src/jvm/clojure/lang/Util.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
package clojure.lang;
1414

1515
import java.math.BigInteger;
16+
import java.util.Map;
17+
import java.util.concurrent.ConcurrentHashMap;
18+
import java.lang.ref.SoftReference;
19+
import java.lang.ref.ReferenceQueue;
20+
import java.lang.ref.Reference;
1621

1722
public class Util{
1823
static public boolean equiv(Object k1, Object k2){
@@ -89,4 +94,17 @@ static public ISeq ret1(ISeq ret, Object nil){
8994
return ret;
9095
}
9196

97+
static public <K,V> void clearCache(ReferenceQueue rq, ConcurrentHashMap<K, SoftReference<V>> cache){
98+
//cleanup any dead entries
99+
if(rq.poll() != null)
100+
{
101+
while(rq.poll() != null)
102+
;
103+
for(Map.Entry<K, SoftReference<V>> e : cache.entrySet())
104+
{
105+
if(e.getValue().get() == null)
106+
cache.remove(e.getKey(), e.getValue());
107+
}
108+
}
109+
}
92110
}

0 commit comments

Comments
 (0)