1a7c41be1f8f8190418e0412a3fd2a8951e43d06
[org.ibex.core.git] / src / org / xwt / plat / GCJ.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt.plat;
3
4 import org.xwt.*;
5 import org.xwt.util.*;
6 import java.io.*;
7
8 /** common superclass for all platforms that use GCJ to compile a native binary */
9 public abstract class GCJ extends Platform {
10
11     // static references to these classes ensure that the linker will include them
12     private static Class c1 = gnu.java.locale.Calendar.class;
13     private static Class c2 = java.util.GregorianCalendar.class;
14     private static Class c3 = gnu.gcj.convert.Input_ASCII.class;
15     private static Class c4 = gnu.gcj.convert.Input_UTF8.class;
16     private static Class c5 = gnu.gcj.convert.Input_8859_1.class;
17     private static Class c6 = gnu.java.locale.LocaleInformation.class;
18     private static Class c7 = gnu.gcj.convert.Output_ASCII.class;
19     private static Class c8 = gnu.java.locale.Calendar_en.class;
20     private static Class c9 = gnu.java.locale.Calendar_en_US.class;
21
22     protected native InputStream _getBuiltinInputStream();
23
24     // FIXME
25     protected native Picture _decodeJPEG(InputStream is, String name);
26
27     // FIXME: This could be optimized (a lot) by using a custom hashtable
28     public final static class Retainer {
29         private static Hash table = new Hash();
30         private final static class Key {
31             private Object o;
32             public Key(Object o) { this.o = o; }
33             public int hashCode() { return o == null ? 0 : o.hashCode(); }
34             public boolean equals(Object o2) { return (o2 instanceof Key) && ((Key)o2).o == o; }
35         }
36         private final static class Entry {
37             private int refCount;
38             public Entry() { inc(); }
39             public void inc() { refCount++; }
40             public boolean dec() { return --refCount == 0; }
41         }
42         public static synchronized void retain(Object o) {
43             Key k = new Key(o);
44             Entry r = (Entry) table.get(k);
45             if(r == null) table.put(k,new Entry());
46             else r.inc();
47         }
48         public static synchronized void release(Object o) {
49             Key k = new Key(o);
50             Entry e = (Entry) table.get(k);
51             if(e == null) throw new Error("Retainer::Release on unknown object");
52             if(e.dec()) { table.remove(k); }
53         }
54     }
55 }