提交 c704b758 编写于 作者: C coffeys

7047325: Internal API to improve management of direct buffers

Reviewed-by: alanb, mduigou
上级 3633c027
# #
# 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. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
# #
# This code is free software; you can redistribute it and/or modify it # This code is free software; you can redistribute it and/or modify it
...@@ -27,8 +27,13 @@ BUILDDIR = ../.. ...@@ -27,8 +27,13 @@ BUILDDIR = ../..
PRODUCT = oracle PRODUCT = oracle
include $(BUILDDIR)/common/Defs.gmk include $(BUILDDIR)/common/Defs.gmk
SUBDIRS = net #
include $(BUILDDIR)/common/Subdirs.gmk # Files to compile
#
AUTO_FILES_JAVA_DIRS = com/oracle
#
# Rules
#
include $(BUILDDIR)/common/Classes.gmk
all build clean clobber::
$(SUBDIRS-loop)
#
# 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
...@@ -60,7 +60,8 @@ EXCLUDE_PROPWARN_PKGS = com.sun.java.swing.plaf.windows \ ...@@ -60,7 +60,8 @@ EXCLUDE_PROPWARN_PKGS = com.sun.java.swing.plaf.windows \
# with a new module system (being discussed for JDK 8). # with a new module system (being discussed for JDK 8).
# #
EXPORTED_PRIVATE_PKGS = com.sun.servicetag \ 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 # 64-bit solaris has a few special cases. We define the variable
# SOLARIS64 for use in this Makefile to easily test those cases # SOLARIS64 for use in this Makefile to easily test those cases
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -699,6 +699,14 @@ class Bits { // package-private ...@@ -699,6 +699,14 @@ class Bits { // package-private
} }
}; };
} }
@Override
public ByteBuffer newDirectByteBuffer(long addr, int cap, Object ob) {
return new DirectByteBuffer(addr, cap, ob);
}
@Override
public void truncate(Buffer buf) {
buf.truncate();
}
}); });
} }
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -543,6 +543,13 @@ public abstract class Buffer { ...@@ -543,6 +543,13 @@ public abstract class Buffer {
return mark; return mark;
} }
final void truncate() { // package-private
mark = -1;
position = 0;
limit = 0;
capacity = 0;
}
final void discardMark() { // package-private final void discardMark() { // package-private
mark = -1; mark = -1;
} }
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -58,12 +58,13 @@ class Direct$Type$Buffer$RW$$BO$ ...@@ -58,12 +58,13 @@ class Direct$Type$Buffer$RW$$BO$
// NOTE: moved up to Buffer.java for speed in JNI GetDirectBufferAddress // NOTE: moved up to Buffer.java for speed in JNI GetDirectBufferAddress
// protected long address; // protected long address;
// If this buffer is a view of another buffer then we keep a reference to // An object attached to this buffer. If this buffer is a view of another
// that buffer so that its memory isn't freed before we're done with it // buffer then we use this field to keep a reference to that buffer to
protected Object viewedBuffer = null; // ensure that its memory isn't freed before we are done with it.
private final Object att;
public Object viewedBuffer() { public Object attachment() {
return viewedBuffer; return att;
} }
#if[byte] #if[byte]
...@@ -136,6 +137,7 @@ class Direct$Type$Buffer$RW$$BO$ ...@@ -136,6 +137,7 @@ class Direct$Type$Buffer$RW$$BO$
address = base; address = base;
} }
cleaner = Cleaner.create(this, new Deallocator(base, size, cap)); cleaner = Cleaner.create(this, new Deallocator(base, size, cap));
att = null;
#else[rw] #else[rw]
super(cap); super(cap);
#end[rw] #end[rw]
...@@ -143,12 +145,24 @@ class Direct$Type$Buffer$RW$$BO$ ...@@ -143,12 +145,24 @@ class Direct$Type$Buffer$RW$$BO$
#if[rw] #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) // Invoked only by JNI: NewDirectByteBuffer(void*, long)
// //
private Direct$Type$Buffer(long addr, int cap) { private Direct$Type$Buffer(long addr, int cap) {
super(-1, 0, cap, cap); super(-1, 0, cap, cap);
address = addr; address = addr;
cleaner = null; cleaner = null;
att = null;
} }
#end[rw] #end[rw]
...@@ -162,8 +176,8 @@ class Direct$Type$Buffer$RW$$BO$ ...@@ -162,8 +176,8 @@ class Direct$Type$Buffer$RW$$BO$
#if[rw] #if[rw]
super(-1, 0, cap, cap, fd); super(-1, 0, cap, cap, fd);
address = addr; address = addr;
viewedBuffer = null;
cleaner = Cleaner.create(this, unmapper); cleaner = Cleaner.create(this, unmapper);
att = null;
#else[rw] #else[rw]
super(cap, addr, fd, unmapper); super(cap, addr, fd, unmapper);
#end[rw] #end[rw]
...@@ -180,10 +194,10 @@ class Direct$Type$Buffer$RW$$BO$ ...@@ -180,10 +194,10 @@ class Direct$Type$Buffer$RW$$BO$
#if[rw] #if[rw]
super(mark, pos, lim, cap); super(mark, pos, lim, cap);
address = db.address() + off; address = db.address() + off;
viewedBuffer = db;
#if[byte] #if[byte]
cleaner = null; cleaner = null;
#end[byte] #end[byte]
att = db;
#else[rw] #else[rw]
super(db, mark, pos, lim, cap, off); super(db, mark, pos, lim, cap, off);
#end[rw] #end[rw]
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -25,6 +25,9 @@ ...@@ -25,6 +25,9 @@
package sun.misc; package sun.misc;
import java.nio.Buffer;
import java.nio.ByteBuffer;
public interface JavaNioAccess { public interface JavaNioAccess {
/** /**
* Provides access to information on buffer usage. * Provides access to information on buffer usage.
...@@ -36,4 +39,18 @@ public interface JavaNioAccess { ...@@ -36,4 +39,18 @@ public interface JavaNioAccess {
long getMemoryUsed(); long getMemoryUsed();
} }
BufferPool getDirectBufferPool(); 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);
} }
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -32,7 +32,7 @@ public interface DirectBuffer { ...@@ -32,7 +32,7 @@ public interface DirectBuffer {
public long address(); public long address();
public Object viewedBuffer(); public Object attachment();
public Cleaner cleaner(); public Cleaner cleaner();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册