1 /*
   2  * Copyright (c) 2015, 2016, 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 
  26 package sun.java2d.marlin;
  27 
  28 import java.util.ArrayDeque;
  29 import java.util.Arrays;
  30 import static sun.java2d.marlin.MarlinUtils.logException;
  31 import static sun.java2d.marlin.MarlinUtils.logInfo;
  32 
  33 final class FloatArrayCache implements MarlinConst {
  34 
  35     private final int arraySize;
  36     private final ArrayDeque<float[]> floatArrays;
  37     // stats
  38     private int getOp = 0;
  39     private int createOp = 0;
  40     private int returnOp = 0;
  41 
  42     void dumpStats() {
  43         if (getOp > 0) {
  44             logInfo("FloatArrayCache[" + arraySize + "]: get: " + getOp
  45                     + " created: " + createOp + " - returned: " + returnOp
  46                     + " :: cache size: " + floatArrays.size());
  47         }
  48     }
  49 
  50     FloatArrayCache(final int arraySize) {
  51         this.arraySize = arraySize;
  52         // small but enough: almost 1 cache line
  53         this.floatArrays = new ArrayDeque<float[]>(6);
  54     }
  55 
  56     float[] getArray() {
  57         if (doStats) {
  58             getOp++;
  59         }
  60 
  61         // use cache
  62         final float[] array = floatArrays.pollLast();
  63 
  64         if (array != null) {
  65             return array;
  66         }
  67 
  68         if (doStats) {
  69             createOp++;
  70         }
  71 
  72         return new float[arraySize];
  73     }
  74 
  75     void putDirtyArray(final float[] array, final int length) {
  76         if (length != arraySize) {
  77             if (doChecks) {
  78                 MarlinUtils.logInfo("ArrayCache: bad length = " + length);
  79             }
  80             return;
  81         }
  82         if (doStats) {
  83             returnOp++;
  84         }
  85 
  86         // NO clean-up of array data = DIRTY ARRAY
  87 
  88         if (doCleanDirty) {
  89             // Force zero-fill dirty arrays:
  90             Arrays.fill(array, 0, array.length, 0f);
  91         }
  92 
  93         // fill cache:
  94         floatArrays.addLast(array);
  95     }
  96 
  97     void putArray(final float[] array, final int length,
  98                   final int fromIndex, final int toIndex)
  99     {
 100         if (length != arraySize) {
 101             if (doChecks) {
 102                 MarlinUtils.logInfo("ArrayCache: bad length = " + length);
 103             }
 104             return;
 105         }
 106         if (doStats) {
 107             returnOp++;
 108         }
 109 
 110         // clean-up array of dirty part[fromIndex; toIndex[
 111         fill(array, fromIndex, toIndex, 0f);
 112 
 113         // fill cache:
 114         floatArrays.addLast(array);
 115     }
 116 
 117     static void fill(final float[] array, final int fromIndex,
 118                      final int toIndex, final float value)
 119     {
 120         // clear array data:
 121         /*
 122          * Arrays.fill is faster than System.arraycopy(empty array)
 123          * or Unsafe.setMemory(byte 0)
 124          */
 125         if (toIndex != 0) {
 126             Arrays.fill(array, fromIndex, toIndex, value);
 127         }
 128 
 129         if (doChecks) {
 130             check(array, fromIndex, toIndex, value);
 131         }
 132     }
 133 
 134     static void check(final float[] array, final int fromIndex,
 135                       final int toIndex, final float value)
 136     {
 137         if (doChecks) {
 138             // check zero on full array:
 139             for (int i = 0; i < array.length; i++) {
 140                 if (array[i] != value) {
 141                     logException("Invalid value at: " + i + " = " + array[i]
 142                             + " from: " + fromIndex + " to: " + toIndex + "\n"
 143                             + Arrays.toString(array), new Throwable());
 144 
 145                     // ensure array is correctly filled:
 146                     Arrays.fill(array, value);
 147 
 148                     return;
 149                 }
 150             }
 151         }
 152     }
 153 }