Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/marlin/FloatArrayCache.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 FloatArrayCache implements MarlinConst {3334private final int arraySize;35private final ArrayDeque<float[]> floatArrays;36// stats37private int getOp = 0;38private int createOp = 0;39private int returnOp = 0;4041void dumpStats() {42if (getOp > 0) {43logInfo("FloatArrayCache[" + arraySize + "]: get: " + getOp44+ " created: " + createOp + " - returned: " + returnOp45+ " :: cache size: " + floatArrays.size());46}47}4849FloatArrayCache(final int arraySize) {50this.arraySize = arraySize;51// small but enough: almost 1 cache line52this.floatArrays = new ArrayDeque<float[]>(6);53}5455float[] getArray() {56if (doStats) {57getOp++;58}5960// use cache61final float[] array = floatArrays.pollLast();6263if (array != null) {64return array;65}6667if (doStats) {68createOp++;69}7071return new float[arraySize];72}7374void putDirtyArray(final float[] array, final int length) {75if (length != arraySize) {76if (doChecks) {77MarlinUtils.logInfo("ArrayCache: bad length = " + length);78}79return;80}81if (doStats) {82returnOp++;83}8485// NO clean-up of array data = DIRTY ARRAY8687if (doCleanDirty) {88// Force zero-fill dirty arrays:89Arrays.fill(array, 0, array.length, 0f);90}9192// fill cache:93floatArrays.addLast(array);94}9596void putArray(final float[] array, final int length,97final int fromIndex, final int toIndex)98{99if (length != arraySize) {100if (doChecks) {101MarlinUtils.logInfo("ArrayCache: bad length = " + length);102}103return;104}105if (doStats) {106returnOp++;107}108109// clean-up array of dirty part[fromIndex; toIndex[110fill(array, fromIndex, toIndex, 0f);111112// fill cache:113floatArrays.addLast(array);114}115116static void fill(final float[] array, final int fromIndex,117final int toIndex, final float value)118{119// clear array data:120/*121* Arrays.fill is faster than System.arraycopy(empty array)122* or Unsafe.setMemory(byte 0)123*/124if (toIndex != 0) {125Arrays.fill(array, fromIndex, toIndex, value);126}127128if (doChecks) {129check(array, fromIndex, toIndex, value);130}131}132133static void check(final float[] array, final int fromIndex,134final int toIndex, final float value)135{136if (doChecks) {137// check zero on full array:138for (int i = 0; i < array.length; i++) {139if (array[i] != value) {140logException("Invalid value at: " + i + " = " + array[i]141+ " from: " + fromIndex + " to: " + toIndex + "\n"142+ Arrays.toString(array), new Throwable());143144// ensure array is correctly filled:145Arrays.fill(array, value);146147return;148}149}150}151}152}153154155