changeset 14414:b6db50e8621b

Pointer.offset() now takes an element count (as opposed to number of bytes)
author mikael
date Tue, 03 May 2016 16:21:37 -0700
parents 5ed6b90d503f
children fffb1cc5d0df
files src/java.base/share/classes/java/nicl/NativeLibrary.java src/java.base/share/classes/java/nicl/types/Pointer.java src/java.base/share/classes/java/nicl/types/Transformer.java src/java.base/share/classes/jdk/internal/nicl/UpcallHandler.java src/java.base/share/classes/jdk/internal/nicl/types/BoundedPointer.java test/java/nicl/Upcall/CallbackSort.java
diffstat 6 files changed, 58 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/nicl/NativeLibrary.java	Tue May 03 15:42:05 2016 -0700
+++ b/src/java.base/share/classes/java/nicl/NativeLibrary.java	Tue May 03 16:21:37 2016 -0700
@@ -176,12 +176,20 @@
 
     @Deprecated
     public static long rawStructRead(Reference<?> struct, long offset) {
-        return struct.ptr().cast(createLayout(long.class)).offset(offset).deref().get();
+        LayoutType<Long> t = createLayout(long.class);
+        if ((offset % t.getType().getSize()) != 0) {
+            throw new IllegalArgumentException("Invalid offset: " + offset);
+        }
+        return struct.ptr().cast(t).offset(offset / t.getType().getSize()).deref().get();
     }
 
     @Deprecated
     public static void rawStructWrite(Reference<?> struct, long offset, long value) {
-        struct.ptr().cast(createLayout(long.class)).offset(offset).deref().set(value);
+        LayoutType<Long> t = createLayout(long.class);
+        if ((offset % t.getType().getSize()) != 0) {
+            throw new IllegalArgumentException("Invalid offset: " + offset);
+        }
+        struct.ptr().cast(t).offset(offset / t.getType().getSize()).deref().set(value);
     }
 
     public static int errno() {
--- a/src/java.base/share/classes/java/nicl/types/Pointer.java	Tue May 03 15:42:05 2016 -0700
+++ b/src/java.base/share/classes/java/nicl/types/Pointer.java	Tue May 03 16:21:37 2016 -0700
@@ -31,7 +31,7 @@
  * @param <T> The type of the pointee
  */
 public interface Pointer<T> {
-    Pointer<T> offset(long diff);
+    Pointer<T> offset(long nElements);
     <S> Pointer<S> cast(LayoutType<S> type);
 
     long addr(PointerToken token) throws IllegalAccessException;
--- a/src/java.base/share/classes/java/nicl/types/Transformer.java	Tue May 03 15:42:05 2016 -0700
+++ b/src/java.base/share/classes/java/nicl/types/Transformer.java	Tue May 03 16:21:37 2016 -0700
@@ -86,7 +86,7 @@
             size);
         for (int i = 0; i < ar.length; i++) {
             Pointer<Byte> s = toCString(ar[i], scope);
-            ptr.offset(UNSAFE.addressSize() * i).deref().set(s);
+            ptr.offset(i).deref().set(s);
         }
         scope.endAllocation();
         return ptr;
--- a/src/java.base/share/classes/jdk/internal/nicl/UpcallHandler.java	Tue May 03 15:42:05 2016 -0700
+++ b/src/java.base/share/classes/jdk/internal/nicl/UpcallHandler.java	Tue May 03 16:21:37 2016 -0700
@@ -50,6 +50,7 @@
 public class UpcallHandler {
 
     private static final boolean DEBUG = Boolean.getBoolean("jdk.internal.nicl.UpcallHandler.DEBUG");
+    private static final LayoutType<Long> LONG_LAYOUT_TYPE = new LayoutTypeImpl<Long>(long.class, Types.INT64);
 
     private static final Object HANDLERS_LOCK = new Object();
     private static final HashMap<Long, UpcallHandler> NEP2HANDLER = new HashMap<>();
@@ -59,6 +60,19 @@
 
     private final UpcallStub stub;
 
+    static {
+        long size = Util.sizeof(long.class);
+        if (Constants.STACK_SLOT_SIZE != size) {
+            throw new Error("Invalid size: " + Constants.STACK_SLOT_SIZE);
+        }
+        if (Constants.INTEGER_REGISTER_SIZE != size) {
+            throw new Error("Invalid size: " + Constants.INTEGER_REGISTER_SIZE);
+        }
+        if ((Constants.VECTOR_REGISTER_SIZE % size) != 0) {
+            throw new Error("Invalid size: " + Constants.VECTOR_REGISTER_SIZE);
+        }
+    }
+
     public static UpcallHandler make(MethodHandle mh) throws Throwable {
         synchronized (HANDLERS_LOCK) {
             int id = ID2HANDLER.size();
@@ -117,28 +131,26 @@
         private final Pointer<Long> vectorReturns;
 
         UpcallContext(long integers, long vectors, long stack, long integerReturn, long vectorReturn) {
-            LayoutType<Long> type = new LayoutTypeImpl<Long>(long.class, Types.INT64);
-
-            this.integers = new BoundedPointer<Long>(type, new BoundedMemoryRegion(integers, Constants.MAX_INTEGER_ARGUMENT_REGISTERS * Constants.INTEGER_REGISTER_SIZE), 0, MemoryRegion.MODE_R);
-            this.vectors = new BoundedPointer<Long>(type, new BoundedMemoryRegion(vectors, Constants.MAX_VECTOR_ARGUMENT_REGISTERS * Constants.VECTOR_REGISTER_SIZE), 0, MemoryRegion.MODE_R);
-            this.stack = new UncheckedPointer<Long>(type, stack, MemoryRegion.MODE_R);
-            this.integerReturns = new BoundedPointer<Long>(type, new BoundedMemoryRegion(integerReturn, Constants.MAX_INTEGER_RETURN_REGISTERS * Constants.INTEGER_REGISTER_SIZE), 0, MemoryRegion.MODE_W);
-            this.vectorReturns = new BoundedPointer<Long>(type, new BoundedMemoryRegion(vectorReturn, Constants.MAX_VECTOR_RETURN_REGISTERS * Constants.VECTOR_REGISTER_SIZE), 0, MemoryRegion.MODE_W);
+            this.integers = new BoundedPointer<Long>(LONG_LAYOUT_TYPE, new BoundedMemoryRegion(integers, Constants.MAX_INTEGER_ARGUMENT_REGISTERS * Constants.INTEGER_REGISTER_SIZE), 0, MemoryRegion.MODE_R);
+            this.vectors = new BoundedPointer<Long>(LONG_LAYOUT_TYPE, new BoundedMemoryRegion(vectors, Constants.MAX_VECTOR_ARGUMENT_REGISTERS * Constants.VECTOR_REGISTER_SIZE), 0, MemoryRegion.MODE_R);
+            this.stack = new UncheckedPointer<Long>(LONG_LAYOUT_TYPE, stack, MemoryRegion.MODE_R);
+            this.integerReturns = new BoundedPointer<Long>(LONG_LAYOUT_TYPE, new BoundedMemoryRegion(integerReturn, Constants.MAX_INTEGER_RETURN_REGISTERS * Constants.INTEGER_REGISTER_SIZE), 0, MemoryRegion.MODE_W);
+            this.vectorReturns = new BoundedPointer<Long>(LONG_LAYOUT_TYPE, new BoundedMemoryRegion(vectorReturn, Constants.MAX_VECTOR_RETURN_REGISTERS * Constants.VECTOR_REGISTER_SIZE), 0, MemoryRegion.MODE_W);
         }
 
         Pointer<Long> getPtr(Storage storage) {
             switch (storage.getStorageClass()) {
             case INTEGER_ARGUMENT_REGISTER:
-                return integers.offset(storage.getStorageIndex() * Constants.INTEGER_REGISTER_SIZE);
+                return integers.offset(storage.getStorageIndex());
             case VECTOR_ARGUMENT_REGISTER:
-                return vectors.offset(storage.getStorageIndex() * Constants.VECTOR_REGISTER_SIZE);
+                return vectors.offset(storage.getStorageIndex() * Constants.VECTOR_REGISTER_SIZE / Util.sizeof(long.class));
             case STACK_ARGUMENT_SLOT:
-                return stack.offset(storage.getStorageIndex() * Constants.STACK_SLOT_SIZE);
+                return stack.offset(storage.getStorageIndex());
 
             case INTEGER_RETURN_REGISTER:
-                return integerReturns.offset(storage.getStorageIndex() * Constants.INTEGER_REGISTER_SIZE);
+                return integerReturns.offset(storage.getStorageIndex());
             case VECTOR_RETURN_REGISTER:
-                return vectorReturns.offset(storage.getStorageIndex() * Constants.VECTOR_REGISTER_SIZE);
+                return vectorReturns.offset(storage.getStorageIndex() * Constants.VECTOR_REGISTER_SIZE / Util.sizeof(long.class));
             default:
                 throw new Error("Unhandled storage: " + storage);
             }
@@ -193,7 +205,10 @@
 
             // FIXME: Work-around for now - the method is called once per stack slot,
             // so we need to return the same vector value all the time
-            Pointer<Long> baseAddress = src.offset(-binding.getOffset());
+            if ((binding.getOffset() % LONG_LAYOUT_TYPE.getType().getSize()) != 0) {
+                throw new Error("Invalid offset: " + binding.getOffset());
+            }
+            Pointer<Long> baseAddress = src.offset(-(binding.getOffset() / LONG_LAYOUT_TYPE.getType().getSize()));
 
             Pointer<Long> arrPtr = NativeLibrary.createArrayElementsPointer(bits);
             NativeLibrary.copy(baseAddress, arrPtr, size);
@@ -222,7 +237,10 @@
                 System.out.println("Populating struct at arg index " + index + " at offset 0x" + Long.toHexString(binding.getOffset()));
             }
 
-            Pointer<Long> dst = r.ptr().cast(new LayoutTypeImpl<Long>(long.class, Types.INT64)).offset(binding.getOffset());
+            if ((binding.getOffset() % LONG_LAYOUT_TYPE.getType().getSize()) != 0) {
+                throw new Error("Invalid offset: " + binding.getOffset());
+            }
+            Pointer<Long> dst = r.ptr().cast(LONG_LAYOUT_TYPE).offset(binding.getOffset() / LONG_LAYOUT_TYPE.getType().getSize());
 
             NativeLibrary.copy(src, dst, binding.getStorage().getSize());
 
@@ -353,7 +371,7 @@
 
             Reference<?> struct = (Reference<?>) o;
 
-            Pointer<Long> src = struct.ptr().cast(new LayoutTypeImpl<Long>(long.class, Types.INT64));
+            Pointer<Long> src = struct.ptr().cast(LONG_LAYOUT_TYPE);
 
             if (returnsInMemory) {
                 // the first integer argument register contains a pointer to caller allocated struct
@@ -361,7 +379,7 @@
 
                 // FIXME: 32-bit support goes here
                 long size = Util.alignUp(Util.sizeof(c), 8);
-                Pointer<Long> dstStructPtr = new BoundedPointer<Long>(new LayoutTypeImpl<Long>(long.class, Types.INT64), new BoundedMemoryRegion(structAddr, size));
+                Pointer<Long> dstStructPtr = new BoundedPointer<Long>(LONG_LAYOUT_TYPE, new BoundedMemoryRegion(structAddr, size));
 
                 NativeLibrary.copy(src, dstStructPtr, size);
 
@@ -369,7 +387,10 @@
                 Pointer<Long> retRegPtr = context.getPtr(new Storage(StorageClass.INTEGER_RETURN_REGISTER, 0, Constants.INTEGER_REGISTER_SIZE));
                 retRegPtr.deref().set(structAddr);
             } else {
-                Pointer<Long> srcPtr = src.offset(binding.getOffset());
+                if ((binding.getOffset() % LONG_LAYOUT_TYPE.getType().getSize()) != 0) {
+                    throw new Error("Invalid offset: " + binding.getOffset());
+                }
+                Pointer<Long> srcPtr = src.offset(binding.getOffset() / LONG_LAYOUT_TYPE.getType().getSize());
 
                 NativeLibrary.copy(srcPtr, dst, binding.getStorage().getSize());
             }
--- a/src/java.base/share/classes/jdk/internal/nicl/types/BoundedPointer.java	Tue May 03 15:42:05 2016 -0700
+++ b/src/java.base/share/classes/jdk/internal/nicl/types/BoundedPointer.java	Tue May 03 16:21:37 2016 -0700
@@ -78,8 +78,13 @@
     }
 
     @Override
-    public BoundedPointer<T> offset(long offset) {
-        long newOffset = this.offset + offset;
+    public BoundedPointer<T> offset(long nElements) {
+        long elemSize = type.getType().getSize();
+        if (elemSize == 0) {
+            throw new IllegalArgumentException();
+        }
+
+        long newOffset = this.offset + nElements * elemSize;
         region.checkBounds(newOffset); // eager check for sanity, at least for now
         return new BoundedPointer<T>(type, region, newOffset);
     }
--- a/test/java/nicl/Upcall/CallbackSort.java	Tue May 03 15:42:05 2016 -0700
+++ b/test/java/nicl/Upcall/CallbackSort.java	Tue May 03 16:21:37 2016 -0700
@@ -150,7 +150,7 @@
                 throw new IndexOutOfBoundsException();
             }
 
-            return base.offset(index * ELEM_SIZE).deref();
+            return base.offset(index).deref();
         }
 
         public int getAt(int index) {