Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/marlin/ByteArrayCache.java
38918 views
/*1* Copyright (c) 2015, 2016, 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.java2d.marlin;2627import java.util.ArrayDeque;28import java.util.Arrays;29import static sun.java2d.marlin.MarlinUtils.logException;30import static sun.java2d.marlin.MarlinUtils.logInfo;3132final class ByteArrayCache implements MarlinConst {3334private final int arraySize;35private final ArrayDeque<byte[]> byteArrays;36// stats37private int getOp = 0;38private int createOp = 0;39private int returnOp = 0;4041void dumpStats() {42if (getOp > 0) {43logInfo("ByteArrayCache[" + arraySize + "]: get: " + getOp44+ " created: " + createOp + " - returned: " + returnOp45+ " :: cache size: " + byteArrays.size());46}47}4849ByteArrayCache(final int arraySize) {50this.arraySize = arraySize;51// small but enough: almost 1 cache line52this.byteArrays = new ArrayDeque<byte[]>(6);53}5455byte[] getArray() {56if (doStats) {57getOp++;58}5960// use cache:61final byte[] array = byteArrays.pollLast();62if (array != null) {63return array;64}6566if (doStats) {67createOp++;68}6970return new byte[arraySize];71}7273void putDirtyArray(final byte[] array, final int length) {74if (length != arraySize) {75if (doChecks) {76MarlinUtils.logInfo("ArrayCache: bad length = " + length);77}78return;79}80if (doStats) {81returnOp++;82}8384// NO clean-up of array data = DIRTY ARRAY8586if (doCleanDirty) {87// Force zero-fill dirty arrays:88Arrays.fill(array, 0, array.length, BYTE_0);89}9091// fill cache:92byteArrays.addLast(array);93}9495void putArray(final byte[] array, final int length,96final int fromIndex, final int toIndex)97{98if (length != arraySize) {99if (doChecks) {100MarlinUtils.logInfo("ArrayCache: bad length = " + length);101}102return;103}104if (doStats) {105returnOp++;106}107108// clean-up array of dirty part[fromIndex; toIndex[109fill(array, fromIndex, toIndex, BYTE_0);110111// fill cache:112byteArrays.addLast(array);113}114115static void fill(final byte[] array, final int fromIndex,116final int toIndex, final byte value)117{118// clear array data:119/*120* Arrays.fill is faster than System.arraycopy(empty array)121* or Unsafe.setMemory(byte 0)122*/123if (toIndex != 0) {124Arrays.fill(array, fromIndex, toIndex, value);125}126127if (doChecks) {128check(array, fromIndex, toIndex, value);129}130}131132static void check(final byte[] array, final int fromIndex,133final int toIndex, final byte value)134{135if (doChecks) {136// check zero on full array:137for (int i = 0; i < array.length; i++) {138if (array[i] != value) {139logException("Invalid value at: " + i + " = " + array[i]140+ " from: " + fromIndex + " to: " + toIndex + "\n"141+ Arrays.toString(array), new Throwable());142143// ensure array is correctly filled:144Arrays.fill(array, value);145146return;147}148}149}150}151}152153154