1 /*
2  * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package sun.awt.X11;
26 
27 import sun.misc.Unsafe;
28 import sun.util.logging.PlatformLogger;
29 
30 class UnsafeXDisposerRecord implements sun.java2d.DisposerRecord {
31     private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.UnsafeXDisposerRecord");
32     private static Unsafe unsafe = XlibWrapper.unsafe;
33     final long[] unsafe_ptrs, x_ptrs;
34     final String name;
35     volatile boolean disposed;
36     final Throwable place;
UnsafeXDisposerRecord(String name, long[] unsafe_ptrs, long[] x_ptrs)37     public UnsafeXDisposerRecord(String name, long[] unsafe_ptrs, long[] x_ptrs) {
38         this.unsafe_ptrs = unsafe_ptrs;
39         this.x_ptrs = x_ptrs;
40         this.name = name;
41         if (XlibWrapper.isBuildInternal) {
42             place = new Throwable();
43         } else {
44             place = null;
45         }
46     }
UnsafeXDisposerRecord(String name, long ... unsafe_ptrs)47     public UnsafeXDisposerRecord(String name, long ... unsafe_ptrs) {
48         this.unsafe_ptrs = unsafe_ptrs;
49         this.x_ptrs = null;
50         this.name = name;
51         if (XlibWrapper.isBuildInternal) {
52             place = new Throwable();
53         } else {
54             place = null;
55         }
56     }
57 
dispose()58     public void dispose() {
59         XToolkit.awtLock();
60         try {
61             if (!disposed) {
62                 if (XlibWrapper.isBuildInternal && "Java2D Disposer".equals(Thread.currentThread().getName()) && log.isLoggable(PlatformLogger.Level.WARNING)) {
63                     if (place != null) {
64                         log.warning(name + " object was not disposed before finalization!", place);
65                     } else {
66                         log.warning(name + " object was not disposed before finalization!");
67                     }
68                 }
69 
70                 if (unsafe_ptrs != null) {
71                     for (long l : unsafe_ptrs) {
72                         if (l != 0) {
73                             unsafe.freeMemory(l);
74                         }
75                     }
76                 }
77                 if (x_ptrs != null) {
78                     for (long l : x_ptrs) {
79                         if (l != 0) {
80                             if (Native.getLong(l) != 0) {
81                                 XlibWrapper.XFree(Native.getLong(l));
82                             }
83                             unsafe.freeMemory(l);
84                         }
85                     }
86                 }
87                 disposed = true;
88             }
89         } finally {
90             XToolkit.awtUnlock();
91         }
92     }
93 }
94