changeset 14416:7b9181011bf7

Scope.allocate() with count of elements.
author henryjen
date Tue, 03 May 2016 16:09:14 -0700
parents fffb1cc5d0df
children 3bbbf6ab10b7
files src/java.base/share/classes/java/nicl/NativeScope.java src/java.base/share/classes/java/nicl/Scope.java src/java.base/share/classes/java/nicl/types/Transformer.java
diffstat 3 files changed, 22 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/nicl/NativeScope.java	Tue May 03 16:22:58 2016 -0700
+++ b/src/java.base/share/classes/java/nicl/NativeScope.java	Tue May 03 16:09:14 2016 -0700
@@ -112,15 +112,21 @@
     }
 
     @Override
-    public <T> Pointer<T> allocate(LayoutType<T> type, long size) {
+    public <T> Pointer<T> allocate(LayoutType<T> type, long count) {
         // Sanity check for now, can be removed/loosened if needed
+        long size = type.getType().getSize();
+        if (size < 0) {
+            throw new UnsupportedOperationException("Unknown size for type " + type);
+        }
+
+        size *= count;
         if (size > Integer.MAX_VALUE) {
             throw new UnsupportedOperationException("allocate size to large");
         }
 
-        return new BoundedPointer<T>(type, allocateRegion(size), 0);
+        return new BoundedPointer<T>(type, allocateRegion(size));
     }
-
+/*
     @Override
     public <T> Pointer<T> allocate(LayoutType<T> type) {
         if (type.getCarrierType().isPrimitive()) {
@@ -133,7 +139,7 @@
 
         throw new IllegalArgumentException("Unhandled type: " + type.getCarrierType());
     }
-
+*/
     @Override
     public <T> Reference<T> allocateStruct(LayoutType<T> type) {
         Class<? extends Reference<T>> implClass = NativeLibrary.getStructImplClass(type.getCarrierType());
--- a/src/java.base/share/classes/java/nicl/Scope.java	Tue May 03 16:22:58 2016 -0700
+++ b/src/java.base/share/classes/java/nicl/Scope.java	Tue May 03 16:09:14 2016 -0700
@@ -39,8 +39,17 @@
     public void startAllocation();
     public void endAllocation();
 
-    public <T> Pointer<T> allocate(LayoutType<T> type);
-    public <T> Pointer<T> allocate(LayoutType<T> type, long size);
+    default public <T> Pointer<T> allocate(LayoutType<T> type) {
+        return allocate(type, 1);
+    }
+
+    /**
+     * Allocate storage for a number of element of specified type.
+     * @param type The LayoutType of the element
+     * @param count The numbder of elements to be allocated
+     * @return A pointer to the allocated storage points to the first element.
+     */
+    public <T> Pointer<T> allocate(LayoutType<T> type, long count);
 
     // FIXME: When .ptr().deref works as expected for structs (returns
     // an actual struct instance instead of a ReferenceImpl instance)
--- a/src/java.base/share/classes/java/nicl/types/Transformer.java	Tue May 03 16:22:58 2016 -0700
+++ b/src/java.base/share/classes/java/nicl/types/Transformer.java	Tue May 03 16:09:14 2016 -0700
@@ -79,11 +79,10 @@
             return null;
         }
         scope.startAllocation();
-        long size = UNSAFE.addressSize() * ar.length;
         @SuppressWarnings("unchecked")
         Pointer<Pointer<Byte>> ptr = scope.allocate(
             new LayoutTypeImpl<Pointer<Byte>>((Class)Pointer.class, Types.POINTER),
-            size);
+            ar.length);
         for (int i = 0; i < ar.length; i++) {
             Pointer<Byte> s = toCString(ar[i], scope);
             ptr.offset(i).deref().set(s);