1 /* Verify that libffi closures aren't deallocated too early.
3 Copyright (C) 2007 Free Software Foundation, Inc
4 Contributed by Alexandre Oliva <aoliva@redhat.com>
6 If libffi closures are released too early, we lose.
9 import java
.util
.HashSet
;
11 public class TestClosureGC
{
12 public static String
objId (Object obj
) {
14 + Integer
.toHexString(obj
.getClass().getClassLoader().hashCode());
16 public static class cld
extends java
.net
.URLClassLoader
{
17 static final Object obj
= new cl0();
18 public cld () throws Exception
{
19 super(new java
.net
.URL
[] { });
20 /* System.out.println (objId (this) + " created"); */
22 public void finalize () {
23 /* System.out.println (objId (this) + " finalized"); */
25 public String
toString () {
26 return this.getClass().getName() + "@"
27 + Integer
.toHexString (hashCode ());
29 public Class
loadClass (String name
) throws ClassNotFoundException
{
31 java
.io
.InputStream IS
= getSystemResourceAsStream
33 int maxsz
= 1024, readsz
= 0;
34 byte buf
[] = new byte[maxsz
];
36 int readnow
= IS
.read (buf
, readsz
, maxsz
- readsz
);
40 if (readsz
== maxsz
) {
41 byte newbuf
[] = new byte[maxsz
*= 2];
42 System
.arraycopy (buf
, 0, newbuf
, 0, readsz
);
46 return defineClass (name
, buf
, 0, readsz
);
47 } catch (Exception e
) {
48 return super.loadClass (name
);
52 public static class cl0
{
54 /* System.out.println (objId (this) + " created"); */
56 public void finalize () {
57 /* System.out.println (objId (this) + " finalized"); */
60 public static class cl1
{
62 static final Object obj
= new cl0();
63 public cl1 (final HashSet hs
) {
65 /* System.out.println (objId (this) + " created"); */
67 public void finalize () {
68 /* System.out.println (objId (this) + " finalized"); */
71 public static class cl2
{
73 static final Object obj
= new cl0();
74 public cl2 (final HashSet hs
) {
76 /* System.out.println (objId (this) + " created"); */
78 public void finalize () {
79 /* System.out.println (objId (this) + " finalized"); */
84 static final HashSet hs
= new HashSet();
85 static final Object obj
= new cl0();
86 public static void main(String
[] argv
) throws Exception
{
88 Class
[] hscs
= { HashSet
.class };
89 Object
[] hsos
= { hs
};
90 new cld().loadClass ("TestClosureGC$cl1").
91 getConstructor (hscs
).newInstance (hsos
);
92 new cld().loadClass ("TestClosureGC$cl2").
93 getConstructor (hscs
).newInstance (hsos
);
94 new cld().loadClass ("TestClosureGC$cl1").
95 getConstructor (hscs
).newInstance (hsos
);
96 new cld().loadClass ("TestClosureGC$cl1").
97 getConstructor (hscs
).newInstance (hsos
);
99 for (int i
= 1; i
<= 5; i
++) {
100 /* System.out.println ("Will run GC and finalization " + i); */
103 System
.runFinalization ();
107 java
.util
.Iterator it
= hs
.iterator ();
108 while (it
.hasNext ()) {
109 Object obj
= it
.next();
110 /* System.out.println (objId (obj) + " in ht, removing"); */
114 System
.out
.println ("ok");