Path: blob/master/src/java.desktop/share/classes/sun/font/FontStrikeDisposer.java
41154 views
/*1* Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.font;2627import java.lang.ref.Reference;28import java.util.concurrent.ConcurrentHashMap;29import sun.java2d.Disposer;30import sun.java2d.DisposerRecord;3132/*33* This keeps track of data that needs to be cleaned up once a34* strike is freed.35* a) The native memory that is the glyph image cache.36* b) removing the "desc" key from the strike's map.37* This is safe to do because this disposer is invoked only when the38* reference object has been cleared, which means the value indexed by39* this key is just an empty reference object.40* It is possible that a new FontStrike has been created that would41* be referenced by the same (equals) key. If it is placed in the map42* before this disposer is executed, then we do not want to remove that43* object. We should only remove an object where the value is null.44* So we first verify that the key still points to a cleared reference.45* Updates to the map thus need to be synchronized.46*47* A WeakHashmap will automatically clean up, but we might maintain a48* reference to the "desc" key in the FontStrike (value) which would49* prevent the keys from being discarded. And since the strike is the only50* place is likely we would maintain such a strong reference, then the map51* entries would be removed much more promptly than we need.52*/5354class FontStrikeDisposer55implements DisposerRecord, Disposer.PollDisposable {5657ConcurrentHashMap<FontStrikeDesc, Reference<FontStrike>> strikeCache;58FontStrikeDesc desc;59long[] longGlyphImages;60int [] intGlyphImages;61int [][] segIntGlyphImages;62long[][] segLongGlyphImages;63long pScalerContext = 0L;64boolean disposed = false;65boolean comp = false;6667public FontStrikeDisposer(Font2D font2D, FontStrikeDesc desc,68long pContext, int[] images) {69this.strikeCache = font2D.strikeCache;70this.desc = desc;71this.pScalerContext = pContext;72this.intGlyphImages = images;73}7475public FontStrikeDisposer(Font2D font2D, FontStrikeDesc desc,76long pContext, long[] images) {77this.strikeCache = font2D.strikeCache;78this.desc = desc;79this.pScalerContext = pContext;80this.longGlyphImages = images;81}8283public FontStrikeDisposer(Font2D font2D, FontStrikeDesc desc,84long pContext) {85this.strikeCache = font2D.strikeCache;86this.desc = desc;87this.pScalerContext = pContext;88}8990public FontStrikeDisposer(Font2D font2D, FontStrikeDesc desc) {91this.strikeCache = font2D.strikeCache;92this.desc = desc;93this.comp = true;94}9596public synchronized void dispose() {97if (!disposed) {98Reference<FontStrike> ref = strikeCache.get(desc);99if (ref != null) {100Object o = ref.get();101if (o == null) {102strikeCache.remove(desc);103}104}105StrikeCache.disposeStrike(this);106disposed = true;107}108}109}110111112