questionable patch: merge of a lot of stuff from the svg branch
[org.ibex.core.git] / src / org / ibex / plat / GCJ.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.plat;
6
7 import org.ibex.util.*;
8 import java.io.*;
9 import java.security.*;
10 import java.security.cert.*;
11 import org.ibex.graphics.*;
12 import org.ibex.core.*;
13 import org.ibex.net.*;
14
15 /** common superclass for all platforms that use GCJ to compile a native binary */
16 public abstract class GCJ extends Platform {
17
18     protected native InputStream _getBuiltinInputStream();
19
20     protected native void _decodeJPEG(InputStream is, Picture p);
21
22     // FEATURE: This could be optimized (a lot) by using a custom hashtable
23     public final static class Retainer {
24         private static Hash table = new Hash();
25         private final static class Key {
26             private Object o;
27             public Key(Object o) { this.o = o; }
28             public int hashCode() { return o == null ? 0 : o.hashCode(); }
29             public boolean equals(Object o2) { return (o2 instanceof Key) && ((Key)o2).o == o; }
30         }
31         private final static class Entry {
32             private int refCount;
33             public Entry() { inc(); }
34             public void inc() { refCount++; }
35             public boolean dec() { return --refCount == 0; }
36         }
37         public static synchronized void retain(Object o) {
38             Key k = new Key(o);
39             Entry r = (Entry) table.get(k);
40             if(r == null) table.put(k,new Entry());
41             else r.inc();
42         }
43         public static synchronized void release(Object o) {
44             Key k = new Key(o);
45             Entry e = (Entry) table.get(k);
46             if(e == null) throw new Error("Retainer::Release on unknown object");
47             if(e.dec()) { table.remove(k); }
48         }
49     }
50 }