[jffi-next] 11/49: Allocate native method struct memory out of one chunk of memory

Tim Potter tpot-guest at moszumanska.debian.org
Wed Mar 4 04:51:09 UTC 2015


This is an automated email from the git hooks/post-receive script.

tpot-guest pushed a commit to annotated tag upstream/1.0.10
in repository jffi-next.

commit 65e8ac15ad8a922a357361d975153652b205ff5f
Author: Wayne Meissner <wmeissner at gmail.com>
Date:   Tue Jan 4 20:56:59 2011 +1000

    Allocate native method struct memory out of one chunk of memory
---
 src/com/kenai/jffi/NativeMethod.java  | 36 ++----------------
 src/com/kenai/jffi/NativeMethods.java | 71 +++++++++++++++++++++++++----------
 2 files changed, 55 insertions(+), 52 deletions(-)

diff --git a/src/com/kenai/jffi/NativeMethod.java b/src/com/kenai/jffi/NativeMethod.java
index 91d1566..65c2902 100644
--- a/src/com/kenai/jffi/NativeMethod.java
+++ b/src/com/kenai/jffi/NativeMethod.java
@@ -22,10 +22,9 @@ package com.kenai.jffi;
  * Represents a native implementation of a method for a class
  */
 public final class NativeMethod {
-    private final MemoryIO io = MemoryIO.getInstance();
     final long function;
-    final long name;
-    final long signature;
+    final String name;
+    final String signature;
 
     /**
      * Creates a new native method wrapper.
@@ -36,34 +35,7 @@ public final class NativeMethod {
      */
     public NativeMethod(long address, String name, String signature) {
         this.function = address;
-        this.name = nativeString(io, name);
-        this.signature = nativeString(io, signature);
-    }
-
-    private static final long nativeString(MemoryIO io, String s) {
-        byte[] bytes = s.getBytes();
-
-        long memory = io.allocateMemory(bytes.length + 1, false);
-        if (memory == 0L) {
-            throw new OutOfMemoryError("failed to allocate memory for string");
-        }
-
-        io.putZeroTerminatedByteArray(memory, bytes, 0, bytes.length);
-
-        return memory;
-    }
-
-    @Override
-    protected void finalize() throws Throwable {
-        try {
-            if (name != 0L) {
-                io.freeMemory(name);
-            }
-            if (signature != 0L) {
-                io.freeMemory(signature);
-            }
-        } finally {
-            super.finalize();
-        }
+        this.name = name;
+        this.signature = signature;
     }
 }
diff --git a/src/com/kenai/jffi/NativeMethods.java b/src/com/kenai/jffi/NativeMethods.java
index b773ef9..89eaa79 100644
--- a/src/com/kenai/jffi/NativeMethods.java
+++ b/src/com/kenai/jffi/NativeMethods.java
@@ -22,6 +22,8 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.WeakHashMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**
  * Utility class to register native methods on a class
@@ -38,14 +40,10 @@ public final class NativeMethods {
     private static final Map<Class, NativeMethods> registeredMethods
             = new WeakHashMap<Class, NativeMethods>();
 
-    private final long memory;
-    private final List<NativeMethod> methods;
-    private final MemoryIO mm;
+    private final ResourceHolder memory;
 
-    private NativeMethods(MemoryIO mm, long memory, List<NativeMethod> methods) {
-        this.mm = mm;
+    private NativeMethods(ResourceHolder memory) {
         this.memory = memory;
-        this.methods = new ArrayList<NativeMethod>(methods);
     }
 
     /**
@@ -55,7 +53,16 @@ public final class NativeMethods {
      * @param methods The list of methods to attach to the class.
      */
     public static synchronized final void register(Class clazz, List<NativeMethod> methods) {
-        final long ptrSize = Platform.getPlatform().addressSize() / 8;
+        //
+        // Calculate how much memory is needed for the function names + signatures
+        //
+        int stringSize = 0;
+        for (NativeMethod m : methods) {
+            stringSize += m.name.getBytes().length + 1;
+            stringSize += m.signature.getBytes().length + 1;
+        }
+
+        final int ptrSize = Platform.getPlatform().addressSize() / 8;
         final MemoryIO mm = MemoryIO.getInstance();
 
         //
@@ -66,18 +73,29 @@ public final class NativeMethods {
         //      char *signature;
         //      void *fnPtr;
         //   } JNINativeMethod;
-
-        long memory = mm.allocateMemory(methods.size() * 3 * ptrSize, true);
+        int structSize = methods.size() * 3 * ptrSize;
+        long memory = mm.allocateMemory(structSize + stringSize, true);
         if (memory == 0L) {
             throw new OutOfMemoryError("could not allocate native memory");
         }
 
-        NativeMethods nm = new NativeMethods(mm, memory, methods);
+        NativeMethods nm = new NativeMethods(new ResourceHolder(mm, memory));
 
-        long off = 0;
+        int off = 0;
+        int stringOff = structSize;
         for (NativeMethod m : methods) {
-            mm.putAddress(memory + off, m.name); off += ptrSize;
-            mm.putAddress(memory + off, m.signature); off += ptrSize;
+            byte[] name = m.name.getBytes();
+            long nameAddress = memory + stringOff;
+            stringOff += name.length + 1;
+            mm.putZeroTerminatedByteArray(nameAddress, name, 0, name.length);
+            
+            byte[] sig = m.signature.getBytes();
+            long sigAddress = memory + stringOff;
+            stringOff += sig.length + 1;
+            mm.putZeroTerminatedByteArray(sigAddress, sig, 0, sig.length);
+            
+            mm.putAddress(memory + off, nameAddress); off += ptrSize;
+            mm.putAddress(memory + off, sigAddress); off += ptrSize;
             mm.putAddress(memory + off, m.function); off += ptrSize;
         }
 
@@ -104,13 +122,26 @@ public final class NativeMethods {
         
         registeredMethods.remove(clazz);
     }
-
-    @Override
-    protected void finalize() throws Throwable {
-        try {
-            mm.freeMemory(memory);
-        } finally {
-            super.finalize();
+    
+    private static final class ResourceHolder {
+        private final MemoryIO mm;
+        private final long memory;
+
+        public ResourceHolder(MemoryIO mm, long memory) {
+            this.mm = mm;
+            this.memory = memory;
+        }
+        
+        @Override
+        protected void finalize() throws Throwable {
+            try {
+                mm.freeMemory(memory);
+            } catch (Throwable t) {
+                Logger.getLogger(getClass().getName()).log(Level.WARNING, 
+                    "Exception when freeing native method struct array: %s", t.getLocalizedMessage());
+            } finally {
+                super.finalize();
+            }
         }
     }
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/jffi-next.git



More information about the pkg-java-commits mailing list