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.C;
  34 import java.nicl.metadata.CallingConvention;
  35 import java.nicl.metadata.NativeHeader;

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