1 /*
   2  * Copyright (c) 2015, 2016, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @run main/othervm CallbackSort
  27  */
  28 
  29 import java.lang.invoke.MethodHandles;
  30 import java.nicl.Libraries;
  31 import java.nicl.NativeTypes;
  32 import java.nicl.Scope;
  33 import java.nicl.metadata.NativeCallback;

  34 import java.nicl.metadata.NativeHeader;
  35 import java.nicl.metadata.NativeLocation;
  36 import java.nicl.metadata.NativeType;
  37 import java.nicl.types.Pointer;
  38 import java.util.Iterator;
  39 
  40 public class CallbackSort {
  41     private static final boolean DEBUG = Boolean.getBoolean("CallbackSort.DEBUG");
  42     private boolean upcallCalled = false;
  43 
  44     @NativeHeader
  45     public static interface stdlib {
  46         @NativeCallback("(p:Vp:V)i")
  47         @FunctionalInterface
  48         static interface compar {
  49             @NativeLocation(file="dummy", line=47, column=11, USR="c:@F@slowsort")


  50             public int fn(Pointer<Void> e1, Pointer<Void> e2);
  51         }
  52 
  53         @NativeLocation(file="dummy", line=47, column=11, USR="c:@F@slowsort")
  54         @NativeType(layout="(p:VLLp:(p:Vp:V)i)V", ctype="void (void *, size_t, size_t, int (*)(const void *, const void *))", name="slowsort")

  55         public abstract void slowsort(Pointer<?> base, long nmemb, long size, compar compar);
  56     }
  57 
  58     public class comparator implements stdlib.compar {
  59         @Override
  60         public int fn(Pointer<Void> e1, Pointer<Void> e2) {
  61             upcallCalled = true;
  62 
  63             Pointer<Integer> p1 = e1.cast(NativeTypes.INT32);
  64             Pointer<Integer> p2 = e2.cast(NativeTypes.INT32);
  65 
  66             int i1 = p1.get();
  67             int i2 = p2.get();
  68 
  69             if (DEBUG) {
  70                 System.out.println("fn(i1=" + i1 + ", i2=" + i2 + ")");
  71             }
  72 
  73             return i1 - i2;
  74         }
  75     }
  76 
  77     private void doSort(NativeIntArray elems) {
  78         stdlib i = Libraries.bind(stdlib.class, Libraries.loadLibrary(MethodHandles.lookup(), "Upcall"));
  79         Pointer<?> p = elems.getBasePointer().cast(NativeTypes.VOID);
  80         i.slowsort(p, elems.size(), elems.getElemSize(), new comparator());
  81     }
  82 
  83     private void printElems(NativeIntArray arr) {
  84         for (int i : arr) {
  85             System.out.print(i + " ");
  86         }
  87         System.out.println();
  88     }
  89 
  90     public void testSort() {
  91         int nelems = 10;
  92         NativeIntArray arr = new NativeIntArray(nelems);
  93 
  94         for (int i = 0; i < nelems; i++) {
  95             arr.setAt(i, nelems - 1 - i);
  96         }
  97 
  98         if (DEBUG) {
  99             printElems(arr);
 100         }
 101 
 102         doSort(arr);
 103 
 104         assertTrue(upcallCalled);
 105 
 106         if (DEBUG) {
 107             printElems(arr);
 108         }
 109 
 110         for (int i = 0; i < arr.size(); i++) {
 111             if (i > 0) {
 112                 assertTrue(arr.getAt(i - 1) < arr.getAt(i));
 113             }
 114         }
 115     }
 116 
 117     public static void main(String[] args) {
 118         new CallbackSort().testSort();
 119     }
 120 
 121     static void assertTrue(boolean actual) {
 122         if (!actual) {
 123             throw new RuntimeException("assertion failed, actual: " + actual);
 124         }
 125     }
 126 
 127     static class NativeIntArray implements Iterable<Integer> {
 128         private static final int ELEM_SIZE = 4;
 129 
 130         private final Scope scope = Scope.newNativeScope();
 131 
 132         private final int nelems;
 133         private final Pointer<Integer> base;
 134 
 135         public NativeIntArray(int nelems) {
 136             this.nelems = nelems;
 137             this.base = scope.allocate(NativeTypes.INT32, nelems * ELEM_SIZE);
 138         }
 139 
 140         public Pointer<Integer> getBasePointer() {
 141             return base;
 142         }
 143 
 144         public int size() {
 145             return nelems;
 146         }
 147 
 148         public int getElemSize() {
 149             return ELEM_SIZE;
 150         }
 151 
 152         private Pointer<Integer> at(int index) {
 153             if (index < 0 || index >= nelems) {
 154                 throw new IndexOutOfBoundsException();
 155             }
 156 
 157             return base.offset(index);
 158         }
 159 
 160         public int getAt(int index) {
 161             return at(index).get();
 162         }
 163 
 164         public void setAt(int index, int value) {
 165             at(index).set(value);
 166         }
 167 
 168         @Override
 169         public Iterator<Integer> iterator() {
 170             return new Iter();
 171         }
 172 
 173         class Iter implements Iterator<Integer> {
 174             private int curIndex;
 175 
 176             @Override
 177             public boolean hasNext() {
 178                 return curIndex < nelems;
 179             }
 180 
 181             @Override
 182             public Integer next() {
 183                 return getAt(curIndex++);
 184             }
 185         }
 186     }
 187 }
--- EOF ---