Permalink
Browse files

Add a cache for ObjectParameterInfo

  • Loading branch information...
1 parent 769d534 commit e9d779413897a84216bd1e188937d7ea588332a3 Wayne Meissner committed Dec 16, 2012
@@ -1,27 +1,39 @@
package com.kenai.jffi;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
public final class ObjectParameterInfo {
+ private static final ConcurrentMap<Integer, ObjectParameterInfo> CACHE = new ConcurrentHashMap<Integer, ObjectParameterInfo>();
private final int parameterIndex;
private final int ioflags;
private final int objectInfo;
public static ObjectParameterInfo create(int parameterIndex, ObjectType objectType,
ComponentType componentType, int ioflags) {
-
- return new ObjectParameterInfo(parameterIndex, ioflags, objectType.value | componentType.value);
+ return getCachedInfo(ObjectBuffer.makeObjectFlags(ioflags, objectType.value | componentType.value, parameterIndex));
}
public static ObjectParameterInfo create(int parameterIndex, int ioflags) {
-
- return new ObjectParameterInfo(parameterIndex, ioflags, 0);
+ return getCachedInfo(ObjectBuffer.makeObjectFlags(ioflags, 0, parameterIndex));
}
- private ObjectParameterInfo(int parameterIndex, int ioflags, int typeInfo) {
+ private static ObjectParameterInfo getCachedInfo(int objectInfo) {
+ ObjectParameterInfo info = CACHE.get(objectInfo);
+ if (info != null) {
+ return info;
+ }
+
+ ObjectParameterInfo cachedInfo = CACHE.putIfAbsent(objectInfo, info = new ObjectParameterInfo(objectInfo));
+
+ return cachedInfo != null ? cachedInfo : info;
+ }
- this.parameterIndex = parameterIndex;
- this.ioflags = ioflags & ObjectBuffer.FLAGS_MASK;
- this.objectInfo = ObjectBuffer.makeObjectFlags(ioflags, typeInfo, parameterIndex);
+ private ObjectParameterInfo(int objectInfo) {
+ this.objectInfo = objectInfo;
+ this.ioflags = objectInfo & ObjectBuffer.FLAGS_MASK;
+ this.parameterIndex = (objectInfo & ObjectBuffer.INDEX_MASK) >> ObjectBuffer.INDEX_SHIFT;
}
/** Copy the array contents to native memory before calling the function */
@@ -99,11 +111,11 @@ public boolean equals(Object o) {
ObjectParameterInfo info = (ObjectParameterInfo) o;
- return objectInfo == info.objectInfo && parameterIndex == info.parameterIndex;
+ return objectInfo == info.objectInfo;
}
@Override
public int hashCode() {
- return 31 * parameterIndex + objectInfo;
+ return 31 * objectInfo;
}
}
@@ -0,0 +1,43 @@
+package com.kenai.jffi;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.util.EnumSet;
+
+import static org.junit.Assert.*;
+
+/**
+ *
+ */
+public class ObjectParameterInfoTest {
+ static int[] possibleFlags = {
+ ObjectParameterInfo.IN,
+ ObjectParameterInfo.OUT,
+ ObjectParameterInfo.NULTERMINATE,
+ ObjectParameterInfo.CLEAR,
+ };
+
+ @Test public void testCache() {
+ for (int i = 0; i < 256; i++) {
+ for (ObjectParameterInfo.ObjectType objectType : EnumSet.allOf(ObjectParameterInfo.ObjectType.class)) {
+ for (ObjectParameterInfo.ComponentType componentType : EnumSet.allOf(ObjectParameterInfo.ComponentType.class)) {
+ int ioflags = 0;
+ for (int f : possibleFlags) {
+ ioflags |= f;
+ ObjectParameterInfo info1 = ObjectParameterInfo.create(i, objectType, componentType, ioflags);
+ ObjectParameterInfo info2 = ObjectParameterInfo.create(i, objectType, componentType, ioflags);
+ assertEquals(i, info1.getParameterIndex());
+ assertEquals(i, info2.getParameterIndex());
+ assertEquals(ioflags, info1.ioflags());
+ assertEquals(ioflags, info2.ioflags());
+ assertSame(info1, info2);
+ }
+ }
+ }
+ }
+ }
+}

0 comments on commit e9d7794

Please sign in to comment.