< prev index next >

openjfx9/modules/javafx.graphics/src/main/java/com/sun/marlin/IntArrayCache.java

Print this page




   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.java2d.marlin;
  26 
  27 import static sun.java2d.marlin.ArrayCacheConst.ARRAY_SIZES;
  28 import static sun.java2d.marlin.ArrayCacheConst.BUCKETS;
  29 import static sun.java2d.marlin.ArrayCacheConst.MAX_ARRAY_SIZE;
  30 import static sun.java2d.marlin.MarlinUtils.logInfo;
  31 import static sun.java2d.marlin.MarlinUtils.logException;


  32 
  33 import java.lang.ref.WeakReference;
  34 import java.util.Arrays;
  35 
  36 import sun.java2d.marlin.ArrayCacheConst.BucketStats;
  37 import sun.java2d.marlin.ArrayCacheConst.CacheStats;
  38 
  39 /*
  40  * Note that the [BYTE/INT/FLOAT]ArrayCache files are nearly identical except
  41  * for a few type and name differences. Typically, the [BYTE]ArrayCache.java file
  42  * is edited manually and then [INT]ArrayCache.java and [FLOAT]ArrayCache.java
  43  * files are generated with the following command lines:
  44  */
  45 // % sed -e 's/(b\yte)[ ]*//g' -e 's/b\yte/int/g' -e 's/B\yte/Int/g' < B\yteArrayCache.java > IntArrayCache.java
  46 // % sed -e 's/(b\yte)[ ]*/(float) /g' -e 's/b\yte/float/g' -e 's/B\yte/Float/g' < B\yteArrayCache.java > FloatArrayCache.java
  47 
  48 final class IntArrayCache implements MarlinConst {
  49 
  50     final boolean clean;
  51     private final int bucketCapacity;
  52     private WeakReference<Bucket[]> refBuckets = null;
  53     final CacheStats stats;
  54 
  55     IntArrayCache(final boolean clean, final int bucketCapacity) {
  56         this.clean = clean;
  57         this.bucketCapacity = bucketCapacity;
  58         this.stats = (DO_STATS) ?
  59             new CacheStats(getLogPrefix(clean) + "IntArrayCache") : null;
  60     }
  61 
  62     Bucket getCacheBucket(final int length) {
  63         final int bucket = ArrayCacheConst.getBucket(length);
  64         return getBuckets()[bucket];
  65     }
  66 
  67     private Bucket[] getBuckets() {
  68         // resolve reference:


 214                 stats.returnOp++;
 215             }
 216             // fill cache:
 217             if (arrays.length > tail) {
 218                 arrays[tail++] = array;
 219 
 220                 if (DO_STATS) {
 221                     stats.updateMaxSize(tail);
 222                 }
 223             } else if (DO_CHECKS) {
 224                 logInfo(getLogPrefix(clean) + "IntArrayCache: "
 225                         + "array capacity exceeded !");
 226             }
 227         }
 228     }
 229 
 230     static int[] createArray(final int length, final boolean clean) {
 231         if (clean) {
 232             return new int[length];
 233         }
 234        // use JDK9 Unsafe.allocateUninitializedArray(class, length):
 235        return (int[]) OffHeapArray.UNSAFE.allocateUninitializedArray(int.class, length);
 236     }
 237 
 238     static void fill(final int[] array, final int fromIndex,
 239                      final int toIndex, final int value)
 240     {
 241         // clear array data:
 242         Arrays.fill(array, fromIndex, toIndex, value);
 243         if (DO_CHECKS) {
 244             check(array, fromIndex, toIndex, value);
 245         }
 246     }
 247 
 248     static void check(final int[] array, final int fromIndex,
 249                       final int toIndex, final int value)
 250     {
 251         if (DO_CHECKS) {
 252             // check zero on full array:
 253             for (int i = 0; i < array.length; i++) {
 254                 if (array[i] != value) {
 255                     logException("Invalid value at: " + i + " = " + array[i]
 256                             + " from: " + fromIndex + " to: " + toIndex + "\n"
 257                             + Arrays.toString(array), new Throwable());
 258 
 259                     // ensure array is correctly filled:
 260                     Arrays.fill(array, value);
 261 
 262                     return;
 263                 }
 264             }
 265         }
 266     }
 267 
 268     static String getLogPrefix(final boolean clean) {
 269         return (clean) ? "Clean" : "Dirty";


   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 com.sun.marlin;
  27 
  28 import static com.sun.marlin.ArrayCacheConst.ARRAY_SIZES;
  29 import static com.sun.marlin.ArrayCacheConst.BUCKETS;
  30 import static com.sun.marlin.ArrayCacheConst.MAX_ARRAY_SIZE;
  31 import static com.sun.marlin.MarlinUtils.logInfo;
  32 import static com.sun.marlin.MarlinUtils.logException;
  33 
  34 import java.lang.ref.WeakReference;
  35 import java.util.Arrays;
  36 
  37 import com.sun.marlin.ArrayCacheConst.BucketStats;
  38 import com.sun.marlin.ArrayCacheConst.CacheStats;
  39 
  40 /*
  41  * Note that the [BYTE/INT/FLOAT]ArrayCache files are nearly identical except
  42  * for a few type and name differences. Typically, the [BYTE]ArrayCache.java file
  43  * is edited manually and then [INT]ArrayCache.java and [FLOAT]ArrayCache.java
  44  * files are generated with the following command lines:
  45  */
  46 // % sed -e 's/(b\yte)[ ]*//g' -e 's/b\yte/int/g' -e 's/B\yte/Int/g' < B\yteArrayCache.java > IntArrayCache.java
  47 // % sed -e 's/(b\yte)[ ]*/(float) /g' -e 's/b\yte/float/g' -e 's/B\yte/Float/g' < B\yteArrayCache.java > FloatArrayCache.java
  48 
  49 public final class IntArrayCache implements MarlinConst {
  50 
  51     final boolean clean;
  52     private final int bucketCapacity;
  53     private WeakReference<Bucket[]> refBuckets = null;
  54     final CacheStats stats;
  55 
  56     IntArrayCache(final boolean clean, final int bucketCapacity) {
  57         this.clean = clean;
  58         this.bucketCapacity = bucketCapacity;
  59         this.stats = (DO_STATS) ?
  60             new CacheStats(getLogPrefix(clean) + "IntArrayCache") : null;
  61     }
  62 
  63     Bucket getCacheBucket(final int length) {
  64         final int bucket = ArrayCacheConst.getBucket(length);
  65         return getBuckets()[bucket];
  66     }
  67 
  68     private Bucket[] getBuckets() {
  69         // resolve reference:


 215                 stats.returnOp++;
 216             }
 217             // fill cache:
 218             if (arrays.length > tail) {
 219                 arrays[tail++] = array;
 220 
 221                 if (DO_STATS) {
 222                     stats.updateMaxSize(tail);
 223                 }
 224             } else if (DO_CHECKS) {
 225                 logInfo(getLogPrefix(clean) + "IntArrayCache: "
 226                         + "array capacity exceeded !");
 227             }
 228         }
 229     }
 230 
 231     static int[] createArray(final int length, final boolean clean) {
 232         if (clean) {
 233             return new int[length];
 234         }
 235         // use JDK9 Unsafe.allocateUninitializedArray(class, length):
 236         return (int[]) OffHeapArray.UNSAFE.allocateUninitializedArray(int.class, length);
 237     }
 238 
 239     public static void fill(final int[] array, final int fromIndex,
 240                             final int toIndex, final int value)
 241     {
 242         // clear array data:
 243         Arrays.fill(array, fromIndex, toIndex, value);
 244         if (DO_CHECKS) {
 245             check(array, fromIndex, toIndex, value);
 246         }
 247     }
 248 
 249     public static void check(final int[] array, final int fromIndex,
 250                              final int toIndex, final int value)
 251     {
 252         if (DO_CHECKS) {
 253             // check zero on full array:
 254             for (int i = 0; i < array.length; i++) {
 255                 if (array[i] != value) {
 256                     logException("Invalid value at: " + i + " = " + array[i]
 257                             + " from: " + fromIndex + " to: " + toIndex + "\n"
 258                             + Arrays.toString(array), new Throwable());
 259 
 260                     // ensure array is correctly filled:
 261                     Arrays.fill(array, value);
 262 
 263                     return;
 264                 }
 265             }
 266         }
 267     }
 268 
 269     static String getLogPrefix(final boolean clean) {
 270         return (clean) ? "Clean" : "Dirty";
< prev index next >