synced back up; 126k
[org.ibex.core.git] / src / org / ibex / plat / GCJ.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.ibex.plat;
3
4 import org.ibex.*;
5 import org.ibex.util.*;
6 import java.io.*;
7 import java.security.*;
8 import java.security.cert.*;
9
10 /** common superclass for all platforms that use GCJ to compile a native binary */
11 public abstract class GCJ extends Platform {
12
13     protected native InputStream _getBuiltinInputStream();
14
15     protected native void _decodeJPEG(InputStream is, Picture p);
16
17     // FEATURE: This could be optimized (a lot) by using a custom hashtable
18     public final static class Retainer {
19         private static Hash table = new Hash();
20         private final static class Key {
21             private Object o;
22             public Key(Object o) { this.o = o; }
23             public int hashCode() { return o == null ? 0 : o.hashCode(); }
24             public boolean equals(Object o2) { return (o2 instanceof Key) && ((Key)o2).o == o; }
25         }
26         private final static class Entry {
27             private int refCount;
28             public Entry() { inc(); }
29             public void inc() { refCount++; }
30             public boolean dec() { return --refCount == 0; }
31         }
32         public static synchronized void retain(Object o) {
33             Key k = new Key(o);
34             Entry r = (Entry) table.get(k);
35             if(r == null) table.put(k,new Entry());
36             else r.inc();
37         }
38         public static synchronized void release(Object o) {
39             Key k = new Key(o);
40             Entry e = (Entry) table.get(k);
41             if(e == null) throw new Error("Retainer::Release on unknown object");
42             if(e.dec()) { table.remove(k); }
43         }
44     }
45 }