Tidy up Unsafe.copyMemory test

Test: art/test/testrunner/testrunner.py -t 2235-JdkUnsafeTest
Bug: 203534219
Change-Id: I4c24b235276ee8e5ca7ccdb7147ef33f1cb9224b
diff --git a/test/2235-JdkUnsafeTest/src/Main.java b/test/2235-JdkUnsafeTest/src/Main.java
index 1c644c7..ad6202a 100644
--- a/test/2235-JdkUnsafeTest/src/Main.java
+++ b/test/2235-JdkUnsafeTest/src/Main.java
@@ -386,9 +386,9 @@
   }
 
   private static void testCopyMemory(Unsafe unsafe) {
-    int size = 4 * 1024;
+    final int size = 4 * 1024;
 
-    int intSize = 4;
+    final int intSize = 4;
     int[] inputInts = new int[size / intSize];
     for (int i = 0; i != inputInts.length; ++i) {
       inputInts[i] = ((int)i) + 1;
@@ -401,7 +401,7 @@
       check(inputInts[i], outputInts[i], "unsafe.copyMemory/int");
     }
 
-    int longSize = 8;
+    final int longSize = 8;
     long[] inputLongs = new long[size / longSize];
     for (int i = 0; i != inputLongs.length; ++i) {
       inputLongs[i] = ((long)i) + 1L;
@@ -415,7 +415,7 @@
       check(inputLongs[i], outputLongs[i], "unsafe.copyMemory/long");
     }
 
-    int floatSize = 4;
+    final int floatSize = 4;
     float[] inputFloats = new float[size / floatSize];
     for (int i = 0; i != inputFloats.length; ++i) {
       inputFloats[i] = ((float)i) + 0.5f;
@@ -429,7 +429,7 @@
       check(inputFloats[i], outputFloats[i], "unsafe.copyMemory/float");
     }
 
-    int doubleSize = 8;
+    final int doubleSize = 8;
     double[] inputDoubles = new double[size / doubleSize];
     for (int i = 0; i != inputDoubles.length; ++i) {
       inputDoubles[i] = ((double)i) + 0.5;
@@ -443,23 +443,21 @@
     }
 
     // check the version that works with memory pointers
-    long srcMemory = jdkUnsafeTestMalloc(size);
-    // use the integer array to fill the source
-    unsafe.copyMemory(inputInts, Unsafe.ARRAY_INT_BASE_OFFSET,
-                      null, srcMemory,
-                      size);
-    long dstMemory = jdkUnsafeTestMalloc(size);
+    try (TestMemoryPtr srcPtr = new TestMemoryPtr(size);
+         TestMemoryPtr dstPtr = new TestMemoryPtr(size)) {
+        // use the integer array to fill the source
+        unsafe.copyMemory(inputInts, Unsafe.ARRAY_INT_BASE_OFFSET,
+                          null, srcPtr.get(),
+                          size);
 
-    unsafe.copyMemory(srcMemory, dstMemory, size);
-    for (int i = 0; i != size; ++i) {
-      check(unsafe.getByte(srcMemory + i),
-            unsafe.getByte(dstMemory + i),
-            "unsafe.copyMemory/memoryAddress");
+        unsafe.copyMemory(srcPtr.get(), dstPtr.get(), size);
+        for (int i = 0; i != size; ++i) {
+          check(unsafe.getByte(srcPtr.get() + i),
+                unsafe.getByte(dstPtr.get() + i),
+                "unsafe.copyMemory/memoryAddress");
+        }
     }
 
-    jdkUnsafeTestFree(dstMemory);
-    jdkUnsafeTestFree(srcMemory);
-
     try {
         TestClass srcObj = new TestClass();
         srcObj.intVar = 12345678;
@@ -468,7 +466,7 @@
                           dstArray, Unsafe.ARRAY_INT_BASE_OFFSET,
                           4);
         expectThrow(RuntimeException.class, "unsafe.copyMemory/non-array-src");
-    } catch (RuntimeException e) {
+    } catch (RuntimeException expected) {
     }
 
     try {
@@ -478,7 +476,7 @@
                           dstObj, unsafe.objectFieldOffset(TestClass.class, "intVar"),
                           4);
         expectThrow(RuntimeException.class, "unsafe.copyMemory/non-array-dst");
-    } catch (RuntimeException e) {
+    } catch (RuntimeException expected) {
     }
   }
 
@@ -494,6 +492,26 @@
     public volatile Object volatileObjectVar = null;
   }
 
+  private static class TestMemoryPtr implements AutoCloseable {
+      private long ptr = 0;
+
+      public TestMemoryPtr(int size) {
+          ptr = jdkUnsafeTestMalloc(size);
+      }
+
+      public long get() {
+          return ptr;
+      }
+
+      @Override
+      public void close() {
+          if (ptr != 0) {
+              jdkUnsafeTestFree(ptr);
+              ptr = 0;
+          }
+      }
+  }
+
   private static native int vmJdkArrayBaseOffset(Class<?> clazz);
   private static native int vmJdkArrayIndexScale(Class<?> clazz);
   private static native long jdkUnsafeTestMalloc(long size);