2003/09/19 05:26:47
[org.ibex.core.git] / src / org / xwt / plat / GCJ.java
index 6f545c0..9484439 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.xwt.plat;
 
 import org.xwt.*;
@@ -21,6 +21,9 @@ public abstract class GCJ extends Platform {
 
     protected org.xwt.Weak _getWeak(Object o) { return new Java2Weak(o); }
 
+    protected native InputStream _getBuiltinInputStream();
+    protected native InputStream _getFreetypeInputStream();
+
     private static class Java2Weak extends java.lang.ref.WeakReference implements org.xwt.Weak {
         public Java2Weak(Object o) { super(o); }
     }
@@ -49,6 +52,33 @@ public abstract class GCJ extends Platform {
         private JPEG(InputStream is) { this.is = is; nativeDecompress(); buffer = null; }
         private native void nativeDecompress();
     }
-
+    
+    // FIXME: This could be optimized (a lot) by using a custom hashtable
+    public final static class Retainer {
+        private static Hash table = new Hash();
+        private final static class Key {
+            private Object o;
+            public Key(Object o) { this.o = o; }
+            public int hashCode() { return o == null ? 0 : o.hashCode(); }
+            public boolean equals(Object o2) { return (o2 instanceof Key) && ((Key)o2).o == o; }
+        }
+        private final static class Entry {
+            private int refCount;
+            public Entry() { inc(); }
+            public void inc() { refCount++; }
+            public boolean dec() { return --refCount == 0; }
+        }
+        public static synchronized void retain(Object o) {
+            Key k = new Key(o);
+            Entry r = (Entry) table.get(k);
+            if(r == null) table.put(k,new Entry());
+            else r.inc();
+        }
+        public static synchronized void release(Object o) {
+            Key k = new Key(o);
+            Entry e = (Entry) table.get(k);
+            if(e == null) throw new Error("Retainer::Release on unknown object");
+            if(e.dec()) { table.remove(k); }
+        }
+    }
 }
-