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