[jffi-next] 10/49: Keep a reference to the MemoryIO used to allocate strings, so they can be de-allocated in a finalizer

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 8a2722f8d9572d3b5a29a7f38e9ca8963dab24d8
Author: Wayne Meissner <wmeissner at gmail.com>
Date:   Sat Jan 1 22:57:33 2011 +1000

    Keep a reference to the MemoryIO used to allocate strings, so they can be de-allocated in a finalizer
---
 src/com/kenai/jffi/NativeMethod.java  | 16 +++++++++-------
 src/com/kenai/jffi/NativeMethods.java | 10 ++++++----
 2 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/src/com/kenai/jffi/NativeMethod.java b/src/com/kenai/jffi/NativeMethod.java
index 310e10d..91d1566 100644
--- a/src/com/kenai/jffi/NativeMethod.java
+++ b/src/com/kenai/jffi/NativeMethod.java
@@ -22,6 +22,7 @@ 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;
@@ -35,19 +36,20 @@ public final class NativeMethod {
      */
     public NativeMethod(long address, String name, String signature) {
         this.function = address;
-        this.name = nativeString(name);
-        this.signature = nativeString(signature);
+        this.name = nativeString(io, name);
+        this.signature = nativeString(io, signature);
     }
 
-    private static final long nativeString(String s) {
+    private static final long nativeString(MemoryIO io, String s) {
         byte[] bytes = s.getBytes();
 
-        long memory = MemoryIO.getInstance().allocateMemory(bytes.length + 1, false);
+        long memory = io.allocateMemory(bytes.length + 1, false);
         if (memory == 0L) {
             throw new OutOfMemoryError("failed to allocate memory for string");
         }
 
-        MemoryIO.getInstance().putZeroTerminatedByteArray(memory, bytes, 0, bytes.length);
+        io.putZeroTerminatedByteArray(memory, bytes, 0, bytes.length);
+
         return memory;
     }
 
@@ -55,10 +57,10 @@ public final class NativeMethod {
     protected void finalize() throws Throwable {
         try {
             if (name != 0L) {
-                MemoryIO.getInstance().freeMemory(name);
+                io.freeMemory(name);
             }
             if (signature != 0L) {
-                MemoryIO.getInstance().freeMemory(signature);
+                io.freeMemory(signature);
             }
         } finally {
             super.finalize();
diff --git a/src/com/kenai/jffi/NativeMethods.java b/src/com/kenai/jffi/NativeMethods.java
index 842f6da..b773ef9 100644
--- a/src/com/kenai/jffi/NativeMethods.java
+++ b/src/com/kenai/jffi/NativeMethods.java
@@ -33,15 +33,17 @@ public final class NativeMethods {
      * hash map, so as long as the class remains alive, the native memory for the
      * structures remains alive.
      *
-     * This doesn't seem to be neccessary on sun's jvm, but best do it to be safe.
+     * This doesn't seem to be necessary on sun's jvm, but best do it to be safe.
      */
     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 NativeMethods(long memory, List<NativeMethod> methods) {
+    private NativeMethods(MemoryIO mm, long memory, List<NativeMethod> methods) {
+        this.mm = mm;
         this.memory = memory;
         this.methods = new ArrayList<NativeMethod>(methods);
     }
@@ -70,7 +72,7 @@ public final class NativeMethods {
             throw new OutOfMemoryError("could not allocate native memory");
         }
 
-        NativeMethods nm = new NativeMethods(memory, methods);
+        NativeMethods nm = new NativeMethods(mm, memory, methods);
 
         long off = 0;
         for (NativeMethod m : methods) {
@@ -106,7 +108,7 @@ public final class NativeMethods {
     @Override
     protected void finalize() throws Throwable {
         try {
-            MemoryIO.getInstance().freeMemory(memory);
+            mm.freeMemory(memory);
         } 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