This is an automated email from the ASF dual-hosted git repository.

dcromberge pushed a commit to branch multi-module-experimental
in repository https://gitbox.apache.org/repos/asf/datasketches-memory.git


The following commit(s) were added to refs/heads/multi-module-experimental by 
this push:
     new 3c818a0  Add Cleaner wrapper class
3c818a0 is described below

commit 3c818a02d29cf2bdc72618cb3058fcbed9735698
Author: David Cromberge <[email protected]>
AuthorDate: Wed Mar 24 09:56:16 2021 +0000

    Add Cleaner wrapper class
    
    The package where this class resides is dependent on the Java
    version that is used.  The correct package will be used
    depending on the Java version, using the MR jar.
---
 .../apache/datasketches/memory/AllocateDirect.java |   4 +-
 .../{NioBitsFields.java => CleanerWrapper.java}    |  24 +++-
 .../apache/datasketches/memory/NioBitsFields.java  |   4 +-
 .../apache/datasketches/memory/AllocateDirect.java | 143 ---------------------
 .../apache/datasketches/memory/CleanerWrapper.java |  23 +++-
 .../apache/datasketches/memory/NioBitsFields.java  |   4 +-
 6 files changed, 41 insertions(+), 161 deletions(-)

diff --git 
a/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/AllocateDirect.java
 
b/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/AllocateDirect.java
index dbbcbfe..c85e66f 100644
--- 
a/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/AllocateDirect.java
+++ 
b/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/AllocateDirect.java
@@ -37,7 +37,7 @@ final class AllocateDirect implements AutoCloseable {
   private static final Logger LOG = 
LoggerFactory.getLogger(AllocateDirect.class);
 
   private final Deallocator deallocator;
-  private final Cleaner cleaner; //TODO-JDK9 import jdk.internal.ref.Cleaner;
+  private final CleanerWrapper cleaner; //TODO-JDK9 import 
jdk.internal.ref.Cleaner;
   private final long nativeBaseOffset;
 
   /**
@@ -68,7 +68,7 @@ final class AllocateDirect implements AutoCloseable {
       nativeBaseOffset = nativeAddress;
     }
     deallocator = new Deallocator(nativeAddress, allocationSize, 
capacityBytes);
-    cleaner = Cleaner.create(this, deallocator);
+    cleaner = new CleanerWrapper(this, deallocator);
   }
 
   @Override
diff --git 
a/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
 
b/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/CleanerWrapper.java
similarity index 63%
copy from 
datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
copy to 
datasketches-memory-base/src/main/java/org/apache/datasketches/memory/CleanerWrapper.java
index ef3fc62..cf2502c 100644
--- 
a/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
+++ 
b/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/CleanerWrapper.java
@@ -19,16 +19,26 @@
 
 package org.apache.datasketches.memory;
 
+import sun.misc.Cleaner;
+
 /**
- * Extracts version-dependent field names into standalone class.
- * The appropriate version will be loaded by the class loader.
+ * Extracts a version-dependent reference to the `jdk.internal.ref.Cleaner` 
into
+ * a standalone class. The package name for Cleaner has changed in
+ * later versions. The appropriate class will be loaded by the class loader
+ * depending on the Java version that is used.
  *
  * For more information, see:
  * https://openjdk.java.net/jeps/238
  */
-class NioBitsFields {
-    static String VM_CLASS_NAME = "sun.misc.VM";
-    static String COUNT_FIELD_NAME = "count";
-    static String RESERVED_MEMORY_FIELD_NAME = "reservedMemory";
-    static String TOTAL_CAPACITY_FIELD_NAME = "totalCapacity";
+class CleanerWrapper {
+    private final Cleaner cleaner;
+
+    CleanerWrapper(Object referent, Runnable deallocator) {
+        cleaner = Cleaner.create(this, deallocator);
+    }
+
+    void clean() {
+        cleaner.clean();
+    }
 }
+
diff --git 
a/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
 
b/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
index ef3fc62..a5a264c 100644
--- 
a/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
+++ 
b/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
@@ -21,7 +21,9 @@ package org.apache.datasketches.memory;
 
 /**
  * Extracts version-dependent field names into standalone class.
- * The appropriate version will be loaded by the class loader.
+ * Some field names in the VM internal class have changed in
+ * later versions. The appropriate class will be loaded by the class loader
+ * depending on the Java version that is used.
  *
  * For more information, see:
  * https://openjdk.java.net/jeps/238
diff --git 
a/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/AllocateDirect.java
 
b/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/AllocateDirect.java
deleted file mode 100644
index 7fcd287..0000000
--- 
a/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/AllocateDirect.java
+++ /dev/null
@@ -1,143 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.datasketches.memory;
-
-import static org.apache.datasketches.memory.UnsafeUtil.unsafe;
-
-import jdk.internal.ref.Cleaner;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Provides access to direct (native) memory.
- *
- * @author Roman Leventov
- * @author Lee Rhodes
- */
-@SuppressWarnings({"restriction","synthetic-access"})
-final class AllocateDirect implements AutoCloseable {
-  private static final Logger LOG = 
LoggerFactory.getLogger(AllocateDirect.class);
-
-  private final Deallocator deallocator;
-  private final Cleaner cleaner;
-  private final long nativeBaseOffset;
-
-  /**
-   * Base Constructor for allocate native memory.
-   *
-   * <p>Allocates and provides access to capacityBytes directly in native 
(off-heap) memory
-   * leveraging the Memory interface.
-   * The allocated memory will be 8-byte aligned, but may not be page aligned.
-   * @param capacityBytes the the requested capacity of off-heap memory. 
Cannot be zero.
-   */
-  AllocateDirect(final long capacityBytes) {
-    final boolean pageAligned = NioBits.isPageAligned();
-    final long pageSize = NioBits.pageSize();
-    final long allocationSize = capacityBytes + (pageAligned ? pageSize : 0);
-    NioBits.reserveMemory(allocationSize, capacityBytes);
-
-    final long nativeAddress;
-    try {
-      nativeAddress = unsafe.allocateMemory(allocationSize);
-    } catch (final OutOfMemoryError err) {
-      NioBits.unreserveMemory(allocationSize, capacityBytes);
-      throw err;
-    }
-    if (pageAligned && ((nativeAddress % pageSize) != 0)) {
-      //Round up to page boundary
-      nativeBaseOffset = (nativeAddress & ~(pageSize - 1L)) + pageSize;
-    } else {
-      nativeBaseOffset = nativeAddress;
-    }
-    deallocator = new Deallocator(nativeAddress, allocationSize, 
capacityBytes);
-    cleaner = Cleaner.create(this, deallocator);
-  }
-
-  @Override
-  public void close() {
-    doClose();
-  }
-
-  boolean doClose() {
-    try {
-      if (deallocator.deallocate(false)) {
-        // This Cleaner.clean() call effectively just removes the Cleaner from 
the internal linked
-        // list of all cleaners. It will delegate to Deallocator.deallocate() 
which will be a no-op
-        // because the valid state is already changed.
-        cleaner.clean();
-        return true;
-      }
-      return false;
-
-    } finally {
-      BaseState.reachabilityFence(this);
-    }
-  }
-
-  long getNativeBaseOffset() {
-    return nativeBaseOffset;
-  }
-
-  StepBoolean getValid() {
-    return deallocator.getValid();
-  }
-
-  private static final class Deallocator implements Runnable {
-    //This is the only place the actual native address is kept for use by 
unsafe.freeMemory();
-    private final long nativeAddress;
-    private final long allocationSize;
-    private final long capacity;
-    private final StepBoolean valid = new StepBoolean(true); //only place for 
this
-
-    private Deallocator(final long nativeAddress, final long allocationSize, 
final long capacity) {
-      BaseState.currentDirectMemoryAllocations_.incrementAndGet();
-      BaseState.currentDirectMemoryAllocated_.addAndGet(capacity);
-      this.nativeAddress = nativeAddress;
-      this.allocationSize = allocationSize;
-      this.capacity = capacity;
-      assert (nativeAddress != 0);
-    }
-
-    StepBoolean getValid() {
-      return valid;
-    }
-
-    @Override
-    public void run() {
-      deallocate(true);
-    }
-
-    boolean deallocate(final boolean calledFromCleaner) {
-      if (valid.change()) {
-        if (calledFromCleaner) {
-          // Warn about non-deterministic resource cleanup.
-          LOG.warn("A WritableDirectHandle was not closed manually");
-        }
-        unsafe.freeMemory(nativeAddress);
-        NioBits.unreserveMemory(allocationSize, capacity);
-        BaseState.currentDirectMemoryAllocations_.decrementAndGet();
-        BaseState.currentDirectMemoryAllocated_.addAndGet(-capacity);
-        return true;
-      } 
-      return false;
-    }
-  }
-
-}
diff --git 
a/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
 
b/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/CleanerWrapper.java
similarity index 63%
copy from 
datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
copy to 
datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/CleanerWrapper.java
index ef3fc62..71a7937 100644
--- 
a/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
+++ 
b/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/CleanerWrapper.java
@@ -19,16 +19,25 @@
 
 package org.apache.datasketches.memory;
 
+import jdk.internal.ref.Cleaner;
+
 /**
- * Extracts version-dependent field names into standalone class.
- * The appropriate version will be loaded by the class loader.
+ * Extracts a version-dependent reference to the `jdk.internal.ref.Cleaner` 
into
+ * a standalone class. The package name for Cleaner has changed in
+ * later versions. The appropriate class will be loaded by the class loader
+ * depending on the Java version that is used.
  *
  * For more information, see:
  * https://openjdk.java.net/jeps/238
  */
-class NioBitsFields {
-    static String VM_CLASS_NAME = "sun.misc.VM";
-    static String COUNT_FIELD_NAME = "count";
-    static String RESERVED_MEMORY_FIELD_NAME = "reservedMemory";
-    static String TOTAL_CAPACITY_FIELD_NAME = "totalCapacity";
+class CleanerWrapper {
+    private final Cleaner cleaner;
+
+    CleanerWrapper(Object referent, Runnable deallocator) {
+        cleaner = Cleaner.create(this, deallocator);
+    }
+
+    void clean() {
+        cleaner.clean();
+    }
 }
diff --git 
a/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
 
b/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
index 7e9ebbb..e49c9ee 100644
--- 
a/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
+++ 
b/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
@@ -21,7 +21,9 @@ package org.apache.datasketches.memory;
 
 /**
  * Extracts version-dependent field names into standalone class.
- * The appropriate version will be loaded by the class loader.
+ * Some field names in the VM internal class have changed in
+ * later versions. The appropriate class will be loaded by the class loader
+ * depending on the Java version that is used.
  *
  * For more information, see:
  * https://openjdk.java.net/jeps/238

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to