7047325: Internal API to improve management of direct buffers
authorcoffeys
Thu, 11 Aug 2011 12:40:24 +0100
changeset 10325 b72c20cd583a
parent 10324 e28265130e4f
child 10326 9feeb910a1e2
7047325: Internal API to improve management of direct buffers Reviewed-by: alanb, mduigou
jdk/make/com/oracle/Makefile
jdk/make/com/oracle/net/Makefile
jdk/make/common/Release.gmk
jdk/src/share/classes/java/nio/Bits.java
jdk/src/share/classes/java/nio/Buffer.java
jdk/src/share/classes/java/nio/Direct-X-Buffer.java.template
jdk/src/share/classes/sun/misc/JavaNioAccess.java
jdk/src/share/classes/sun/nio/ch/DirectBuffer.java
--- a/jdk/make/com/oracle/Makefile	Wed Aug 10 13:44:58 2011 -0700
+++ b/jdk/make/com/oracle/Makefile	Thu Aug 11 12:40:24 2011 +0100
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 #
 # This code is free software; you can redistribute it and/or modify it
@@ -27,8 +27,13 @@
 PRODUCT = oracle
 include $(BUILDDIR)/common/Defs.gmk
 
-SUBDIRS = net
-include $(BUILDDIR)/common/Subdirs.gmk
+#
+# Files to compile
+#
+AUTO_FILES_JAVA_DIRS = com/oracle
 
-all build clean clobber::
-	$(SUBDIRS-loop)
+#
+# Rules
+#
+include $(BUILDDIR)/common/Classes.gmk
+
--- a/jdk/make/com/oracle/net/Makefile	Wed Aug 10 13:44:58 2011 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,39 +0,0 @@
-#
-# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
-# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
-#
-# This code is free software; you can redistribute it and/or modify it
-# under the terms of the GNU General Public License version 2 only, as
-# published by the Free Software Foundation.  Oracle designates this
-# particular file as subject to the "Classpath" exception as provided
-# by Oracle in the LICENSE file that accompanied this code.
-#
-# This code is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-# version 2 for more details (a copy is included in the LICENSE file that
-# accompanied this code).
-#
-# You should have received a copy of the GNU General Public License version
-# 2 along with this work; if not, write to the Free Software Foundation,
-# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
-#
-# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
-# or visit www.oracle.com if you need additional information or have any
-# questions.
-#
-
-BUILDDIR = ../../..
-PRODUCT = oracle
-include $(BUILDDIR)/common/Defs.gmk
-
-#
-# Files to compile
-#
-AUTO_FILES_JAVA_DIRS = com/oracle/net
-
-#
-# Rules
-#
-include $(BUILDDIR)/common/Classes.gmk
-
--- a/jdk/make/common/Release.gmk	Wed Aug 10 13:44:58 2011 -0700
+++ b/jdk/make/common/Release.gmk	Thu Aug 11 12:40:24 2011 +0100
@@ -60,7 +60,8 @@
 # with a new module system (being discussed for JDK 8).
 #
 EXPORTED_PRIVATE_PKGS = com.sun.servicetag \
-                        com.oracle.net
+                        com.oracle.net \
+                        com.oracle.nio
 
 # 64-bit solaris has a few special cases. We define the variable
 # SOLARIS64 for use in this Makefile to easily test those cases
--- a/jdk/src/share/classes/java/nio/Bits.java	Wed Aug 10 13:44:58 2011 -0700
+++ b/jdk/src/share/classes/java/nio/Bits.java	Thu Aug 11 12:40:24 2011 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -699,6 +699,14 @@
                         }
                     };
                 }
+                @Override
+                public ByteBuffer newDirectByteBuffer(long addr, int cap, Object ob) {
+                    return new DirectByteBuffer(addr, cap, ob);
+                }
+                @Override
+                public void truncate(Buffer buf) {
+                    buf.truncate();
+                }
         });
     }
 
--- a/jdk/src/share/classes/java/nio/Buffer.java	Wed Aug 10 13:44:58 2011 -0700
+++ b/jdk/src/share/classes/java/nio/Buffer.java	Thu Aug 11 12:40:24 2011 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -543,6 +543,13 @@
         return mark;
     }
 
+    final void truncate() {                             // package-private
+        mark = -1;
+        position = 0;
+        limit = 0;
+        capacity = 0;
+    }
+
     final void discardMark() {                          // package-private
         mark = -1;
     }
--- a/jdk/src/share/classes/java/nio/Direct-X-Buffer.java.template	Wed Aug 10 13:44:58 2011 -0700
+++ b/jdk/src/share/classes/java/nio/Direct-X-Buffer.java.template	Thu Aug 11 12:40:24 2011 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -58,12 +58,13 @@
     // NOTE: moved up to Buffer.java for speed in JNI GetDirectBufferAddress
     //    protected long address;
 
-    // If this buffer is a view of another buffer then we keep a reference to
-    // that buffer so that its memory isn't freed before we're done with it
-    protected Object viewedBuffer = null;
+    // An object attached to this buffer. If this buffer is a view of another
+    // buffer then we use this field to keep a reference to that buffer to
+    // ensure that its memory isn't freed before we are done with it.
+    private final Object att;
 
-    public Object viewedBuffer() {
-        return viewedBuffer;
+    public Object attachment() {
+        return att;
     }
 
 #if[byte]
@@ -136,6 +137,7 @@
             address = base;
         }
         cleaner = Cleaner.create(this, new Deallocator(base, size, cap));
+        att = null;
 #else[rw]
         super(cap);
 #end[rw]
@@ -143,12 +145,24 @@
 
 #if[rw]
 
+    // Invoked to construct a direct ByteBuffer referring to the block of
+    // memory. A given arbitrary object may also be attached to the buffer.
+    //
+    Direct$Type$Buffer(long addr, int cap, Object ob) {
+        super(-1, 0, cap, cap);
+        address = addr;
+        cleaner = null;
+        att = ob;
+    }
+
+
     // Invoked only by JNI: NewDirectByteBuffer(void*, long)
     //
     private Direct$Type$Buffer(long addr, int cap) {
         super(-1, 0, cap, cap);
         address = addr;
         cleaner = null;
+        att = null;
     }
 
 #end[rw]
@@ -162,8 +176,8 @@
 #if[rw]
         super(-1, 0, cap, cap, fd);
         address = addr;
-        viewedBuffer = null;
         cleaner = Cleaner.create(this, unmapper);
+        att = null;
 #else[rw]
         super(cap, addr, fd, unmapper);
 #end[rw]
@@ -180,10 +194,10 @@
 #if[rw]
         super(mark, pos, lim, cap);
         address = db.address() + off;
-        viewedBuffer = db;
 #if[byte]
         cleaner = null;
 #end[byte]
+        att = db;
 #else[rw]
         super(db, mark, pos, lim, cap, off);
 #end[rw]
--- a/jdk/src/share/classes/sun/misc/JavaNioAccess.java	Wed Aug 10 13:44:58 2011 -0700
+++ b/jdk/src/share/classes/sun/misc/JavaNioAccess.java	Thu Aug 11 12:40:24 2011 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,9 @@
 
 package sun.misc;
 
+import java.nio.Buffer;
+import java.nio.ByteBuffer;
+
 public interface JavaNioAccess {
     /**
      * Provides access to information on buffer usage.
@@ -36,4 +39,18 @@
         long getMemoryUsed();
     }
     BufferPool getDirectBufferPool();
+
+    /**
+     * Constructs a direct ByteBuffer referring to the block of memory starting
+     * at the given memory address and and extending {@code cap} bytes.
+     * The {@code ob} parameter is an arbitrary object that is attached
+     * to the resulting buffer.
+     */
+    ByteBuffer newDirectByteBuffer(long addr, int cap, Object ob);
+
+    /**
+     * Truncates a buffer by changing its capacity to 0.
+     */
+    void truncate(Buffer buf);
+
 }
--- a/jdk/src/share/classes/sun/nio/ch/DirectBuffer.java	Wed Aug 10 13:44:58 2011 -0700
+++ b/jdk/src/share/classes/sun/nio/ch/DirectBuffer.java	Thu Aug 11 12:40:24 2011 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -32,7 +32,7 @@
 
     public long address();
 
-    public Object viewedBuffer();
+    public Object attachment();
 
     public Cleaner cleaner();