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 final class ByteArrayCache implements MarlinConst {
50
51 final boolean clean;
52 private final int bucketCapacity;
53 private WeakReference<Bucket[]> refBuckets = null;
54 final CacheStats stats;
55
56 ByteArrayCache(final boolean clean, final int bucketCapacity) {
57 this.clean = clean;
58 this.bucketCapacity = bucketCapacity;
59 this.stats = (DO_STATS) ?
60 new CacheStats(getLogPrefix(clean) + "ByteArrayCache") : 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:
229 }
230
231 static byte[] createArray(final int length, final boolean clean) {
232 if (clean) {
233 return new byte[length];
234 }
235 // use JDK9 Unsafe.allocateUninitializedArray(class, length):
236 return (byte[]) OffHeapArray.UNSAFE.allocateUninitializedArray(byte.class, length);
237 }
238
239 static void fill(final byte[] array, final int fromIndex,
240 final int toIndex, final byte 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 static void check(final byte[] array, final int fromIndex,
250 final int toIndex, final byte 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";
|
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 ByteArrayCache implements MarlinConst {
50
51 final boolean clean;
52 private final int bucketCapacity;
53 private WeakReference<Bucket[]> refBuckets = null;
54 final CacheStats stats;
55
56 ByteArrayCache(final boolean clean, final int bucketCapacity) {
57 this.clean = clean;
58 this.bucketCapacity = bucketCapacity;
59 this.stats = (DO_STATS) ?
60 new CacheStats(getLogPrefix(clean) + "ByteArrayCache") : 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:
229 }
230
231 static byte[] createArray(final int length, final boolean clean) {
232 if (clean) {
233 return new byte[length];
234 }
235 // use JDK9 Unsafe.allocateUninitializedArray(class, length):
236 return (byte[]) OffHeapArray.UNSAFE.allocateUninitializedArray(byte.class, length);
237 }
238
239 static void fill(final byte[] array, final int fromIndex,
240 final int toIndex, final byte 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 byte[] array, final int fromIndex,
250 final int toIndex, final byte 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";
|