changeset 4352:449f7f1bb735 jdk7u2-b04

7047325: Internal API to improve management of direct buffers Reviewed-by: alanb, mduigou
author coffeys
date Fri, 12 Aug 2011 09:40:37 +0100
parents 56fc480ef969
children a15d2672aad0 775d67f1d144
files make/com/oracle/Makefile make/com/oracle/net/Makefile make/common/Release.gmk src/share/classes/java/nio/Bits.java src/share/classes/java/nio/Buffer.java src/share/classes/java/nio/Direct-X-Buffer.java.template src/share/classes/sun/misc/JavaNioAccess.java src/share/classes/sun/nio/ch/DirectBuffer.java
diffstat 8 files changed, 71 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/make/com/oracle/Makefile	Fri Aug 12 11:14:09 2011 +0800
+++ b/make/com/oracle/Makefile	Fri Aug 12 09:40:37 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/make/com/oracle/net/Makefile	Fri Aug 12 11:14:09 2011 +0800
+++ /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/make/common/Release.gmk	Fri Aug 12 11:14:09 2011 +0800
+++ b/make/common/Release.gmk	Fri Aug 12 09:40:37 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/src/share/classes/java/nio/Bits.java	Fri Aug 12 11:14:09 2011 +0800
+++ b/src/share/classes/java/nio/Bits.java	Fri Aug 12 09:40:37 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/src/share/classes/java/nio/Buffer.java	Fri Aug 12 11:14:09 2011 +0800
+++ b/src/share/classes/java/nio/Buffer.java	Fri Aug 12 09:40:37 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/src/share/classes/java/nio/Direct-X-Buffer.java.template	Fri Aug 12 11:14:09 2011 +0800
+++ b/src/share/classes/java/nio/Direct-X-Buffer.java.template	Fri Aug 12 09:40:37 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/src/share/classes/sun/misc/JavaNioAccess.java	Fri Aug 12 11:14:09 2011 +0800
+++ b/src/share/classes/sun/misc/JavaNioAccess.java	Fri Aug 12 09:40:37 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/src/share/classes/sun/nio/ch/DirectBuffer.java	Fri Aug 12 11:14:09 2011 +0800
+++ b/src/share/classes/sun/nio/ch/DirectBuffer.java	Fri Aug 12 09:40:37 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();