< prev index next >

src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template

Print this page

        

@@ -68,10 +68,40 @@
         return att;
     }
 
 #if[byte]
 
+    private static class Allocator
+        implements Supplier<Long>
+    {
+        private static final Unsafe unsafe = Unsafe.getUnsafe();
+
+        private final long size;
+        private final int capacity;
+
+        Allocator(long size, int capacity) {
+            this.size = size;
+            this.capacity = capacity;
+        }
+
+        @Override
+        public Long get() {
+            if (!Bits.tryReserveMemory(size, capacity)) {
+                return null;
+            }
+            long address;
+            try {
+                address = unsafe.allocateMemory(size);
+            } catch (OutOfMemoryError x) {
+                Bits.unreserveMemory(size, capacity);
+                return null;
+            }
+            unsafe.setMemory(address, size, (byte) 0);
+            return address;
+        }
+    }
+
     private static class Deallocator
         implements Runnable
     {
 
         private static Unsafe unsafe = Unsafe.getUnsafe();

@@ -119,20 +149,16 @@
 #if[rw]
         super(-1, 0, cap, cap);
         boolean pa = VM.isDirectMemoryPageAligned();
         int ps = Bits.pageSize();
         long size = Math.max(1L, (long)cap + (pa ? ps : 0));
-        Bits.reserveMemory(size, cap);
 
-        long base = 0;
-        try {
-            base = unsafe.allocateMemory(size);
-        } catch (OutOfMemoryError x) {
-            Bits.unreserveMemory(size, cap);
-            throw x;
+        Long base = CleanerFactory.dbbCleaner().retryWhileHelpingClean(new Allocator(size, cap));
+        if (base == null) {
+            throw new OutOfMemoryError("Direct buffer memory");
         }
-        unsafe.setMemory(base, size, (byte) 0);
+
         if (pa && (base % ps != 0)) {
             // Round up to page boundary
             address = base + ps - (base & (ps - 1));
         } else {
             address = base;
< prev index next >