From 5709e037ea55fc95786b5a05de596eaeef835654 Mon Sep 17 00:00:00 2001
From: Zbynek Slajchrt <zbynek.slajchrt@oracle.com>
Date: Fri, 16 Mar 2018 20:36:14 +0100
Subject: [PATCH] Obsolete native pointer table removed

---
 .../llvm/TruffleLLVM_DownCallNodeFactory.java | 12 +++---
 .../nfi/TruffleNFI_DownCallNodeFactory.java   | 12 +++---
 .../r/runtime/ffi/interop/NativePointer.java  | 41 ++-----------------
 3 files changed, 16 insertions(+), 49 deletions(-)

diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/llvm/TruffleLLVM_DownCallNodeFactory.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/llvm/TruffleLLVM_DownCallNodeFactory.java
index 8600debf0f..50f798fb1b 100644
--- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/llvm/TruffleLLVM_DownCallNodeFactory.java
+++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/llvm/TruffleLLVM_DownCallNodeFactory.java
@@ -53,23 +53,23 @@ final class TruffleLLVM_DownCallNodeFactory extends DownCallNodeFactory {
     public DownCallNode createDownCallNode(NativeFunction f) {
         return new DownCallNode(f) {
             @Override
-            protected TruffleObject getTarget(NativeFunction function) {
+            protected TruffleObject getTarget(NativeFunction fn) {
                 CompilerAsserts.neverPartOfCompilation();
-                String library = function.getLibrary();
+                String library = fn.getLibrary();
                 DLLInfo dllInfo = null;
                 if (library != anyLibrary()) {
                     dllInfo = DLL.findLibrary(library);
                 }
-                SymbolHandle result = DLL.findSymbol(function.getCallName(), dllInfo);
+                SymbolHandle result = DLL.findSymbol(fn.getCallName(), dllInfo);
                 if (result == DLL.SYMBOL_NOT_FOUND) {
-                    throw RInternalError.shouldNotReachHere("Could not find function " + function.getCallName() + " in library " + library);
+                    throw RInternalError.shouldNotReachHere("Could not find function " + fn.getCallName() + " in library " + library);
                 }
                 return result.asTruffleObject();
             }
 
             @Override
             @ExplodeLoop
-            protected long beforeCall(NativeFunction nativeFunction, TruffleObject function, Object[] args) {
+            protected long beforeCall(NativeFunction nativeFunction, TruffleObject fn, Object[] args) {
                 for (int i = 0; i < args.length; i++) {
                     Object obj = args[i];
                     if (obj instanceof double[]) {
@@ -92,7 +92,7 @@ final class TruffleLLVM_DownCallNodeFactory extends DownCallNodeFactory {
 
             @Override
             @ExplodeLoop
-            protected void afterCall(long before, NativeFunction function, TruffleObject target, Object[] args) {
+            protected void afterCall(long before, NativeFunction fn, TruffleObject target, Object[] args) {
                 for (Object obj : args) {
                     if (obj instanceof NativeNACheck<?>) {
                         ((NativeNACheck<?>) obj).close();
diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_DownCallNodeFactory.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_DownCallNodeFactory.java
index 2481ed6657..a4da40eb92 100644
--- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_DownCallNodeFactory.java
+++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_DownCallNodeFactory.java
@@ -42,14 +42,14 @@ public final class TruffleNFI_DownCallNodeFactory extends DownCallNodeFactory {
     public DownCallNode createDownCallNode(NativeFunction f) {
         return new DownCallNode(f) {
             @Override
-            protected TruffleObject getTarget(NativeFunction function) {
+            protected TruffleObject getTarget(NativeFunction fn) {
                 // TODO: this lookupNativeFunction function can exist in all FFI Contexts
-                return TruffleNFI_Context.getInstance().lookupNativeFunction(function);
+                return TruffleNFI_Context.getInstance().lookupNativeFunction(fn);
             }
 
             @Override
             @ExplodeLoop
-            protected long beforeCall(NativeFunction function, TruffleObject target, Object[] args) {
+            protected long beforeCall(NativeFunction fn, TruffleObject target, Object[] args) {
                 for (int i = 0; i < args.length; i++) {
                     Object obj = args[i];
                     if (obj instanceof double[]) {
@@ -72,7 +72,7 @@ public final class TruffleNFI_DownCallNodeFactory extends DownCallNodeFactory {
                     }
                 }
 
-                if (function.hasComplexInteraction()) {
+                if (fn.hasComplexInteraction()) {
                     return ((TruffleNFI_Context) RContext.getInstance().getRFFI()).beforeDowncall();
                 }
                 return 0;
@@ -88,8 +88,8 @@ public final class TruffleNFI_DownCallNodeFactory extends DownCallNodeFactory {
 
             @Override
             @ExplodeLoop
-            protected void afterCall(long before, NativeFunction function, TruffleObject target, Object[] args) {
-                if (function.hasComplexInteraction()) {
+            protected void afterCall(long before, NativeFunction fn, TruffleObject target, Object[] args) {
+                if (fn.hasComplexInteraction()) {
                     ((TruffleNFI_Context) RContext.getInstance().getRFFI()).afterDowncall(before);
                 }
 
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/interop/NativePointer.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/interop/NativePointer.java
index 0b113e2711..5a4807141e 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/interop/NativePointer.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/interop/NativePointer.java
@@ -32,7 +32,7 @@ import com.oracle.truffle.r.runtime.data.RTruffleObject;
  * Created when a {@link RTruffleObject} subclass has no meaningful native representation,
  * nevertheless a {@link Message#TO_NATIVE} message is sent to it.
  */
-public class NativePointer implements TruffleObject {
+public abstract class NativePointer implements TruffleObject {
 
     /**
      * This is used when an {@link RNull} is stored in memory (LLVM).
@@ -50,23 +50,9 @@ public class NativePointer implements TruffleObject {
 
     public static final NullNativePointer NULL_NATIVEPOINTER = new NullNativePointer();
 
-    private static Table[] table = new Table[16];
-    private static int tableHwm;
-
-    private static class Table {
-        // TODO: is this reference to object needed?
-        @SuppressWarnings("unused") private final RTruffleObject object;
-        private final long nativePointer;
-
-        Table(RTruffleObject object, long nativePointer) {
-            this.object = object;
-            this.nativePointer = nativePointer;
-        }
-    }
-
     final RTruffleObject object;
 
-    public NativePointer(RTruffleObject object) {
+    protected NativePointer(RTruffleObject object) {
         this.object = object;
     }
 
@@ -80,27 +66,8 @@ public class NativePointer implements TruffleObject {
     }
 
     final long asPointer() {
-        long result = asPointerImpl();
-        boolean newPointer = true;
-        for (int i = 0; i < tableHwm; i++) {
-            if (table[i].nativePointer == result) {
-                newPointer = false;
-                break;
-            }
-        }
-        if (newPointer) {
-            // System.out.printf("as_pointer: %x from %s\n", result, object.getClass().getName());
-            if (tableHwm >= table.length) {
-                Table[] newTable = new Table[table.length * 2];
-                System.arraycopy(table, 0, newTable, 0, table.length);
-                table = newTable;
-            }
-            table[tableHwm++] = new Table(object, result);
-        }
-        return result;
+        return asPointerImpl();
     }
 
-    protected long asPointerImpl() {
-        return System.identityHashCode(object);
-    }
+    protected abstract long asPointerImpl();
 }
-- 
GitLab