author | alanb |
Thu, 29 Jul 2010 13:08:58 +0100 | |
changeset 6128 | f95e5fdc1a65 |
parent 5506 | 202f599c92aa |
child 7025 | 6e002f9a2899 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.nio; |
|
27 |
||
6128
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
28 |
import sun.misc.Unsafe; |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
29 |
|
2 | 30 |
|
31 |
/** |
|
32 |
* A direct byte buffer whose content is a memory-mapped region of a file. |
|
33 |
* |
|
34 |
* <p> Mapped byte buffers are created via the {@link |
|
35 |
* java.nio.channels.FileChannel#map FileChannel.map} method. This class |
|
36 |
* extends the {@link ByteBuffer} class with operations that are specific to |
|
37 |
* memory-mapped file regions. |
|
38 |
* |
|
39 |
* <p> A mapped byte buffer and the file mapping that it represents remain |
|
40 |
* valid until the buffer itself is garbage-collected. |
|
41 |
* |
|
42 |
* <p> The content of a mapped byte buffer can change at any time, for example |
|
43 |
* if the content of the corresponding region of the mapped file is changed by |
|
44 |
* this program or another. Whether or not such changes occur, and when they |
|
45 |
* occur, is operating-system dependent and therefore unspecified. |
|
46 |
* |
|
47 |
* <a name="inaccess"><p> All or part of a mapped byte buffer may become |
|
48 |
* inaccessible at any time, for example if the mapped file is truncated. An |
|
49 |
* attempt to access an inaccessible region of a mapped byte buffer will not |
|
50 |
* change the buffer's content and will cause an unspecified exception to be |
|
51 |
* thrown either at the time of the access or at some later time. It is |
|
52 |
* therefore strongly recommended that appropriate precautions be taken to |
|
53 |
* avoid the manipulation of a mapped file by this program, or by a |
|
54 |
* concurrently running program, except to read or write the file's content. |
|
55 |
* |
|
56 |
* <p> Mapped byte buffers otherwise behave no differently than ordinary direct |
|
57 |
* byte buffers. </p> |
|
58 |
* |
|
59 |
* |
|
60 |
* @author Mark Reinhold |
|
61 |
* @author JSR-51 Expert Group |
|
62 |
* @since 1.4 |
|
63 |
*/ |
|
64 |
||
65 |
public abstract class MappedByteBuffer |
|
66 |
extends ByteBuffer |
|
67 |
{ |
|
68 |
||
69 |
// This is a little bit backwards: By rights MappedByteBuffer should be a |
|
70 |
// subclass of DirectByteBuffer, but to keep the spec clear and simple, and |
|
71 |
// for optimization purposes, it's easier to do it the other way around. |
|
72 |
// This works because DirectByteBuffer is a package-private class. |
|
73 |
||
74 |
// Volatile to make sure that the finalization thread sees the current |
|
75 |
// value of this so that a region is not accidentally unmapped again later. |
|
76 |
volatile boolean isAMappedBuffer; // package-private |
|
77 |
||
78 |
// This should only be invoked by the DirectByteBuffer constructors |
|
79 |
// |
|
80 |
MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private |
|
81 |
boolean mapped) |
|
82 |
{ |
|
83 |
super(mark, pos, lim, cap); |
|
84 |
isAMappedBuffer = mapped; |
|
85 |
} |
|
86 |
||
87 |
MappedByteBuffer(int mark, int pos, int lim, int cap) { // package-private |
|
88 |
super(mark, pos, lim, cap); |
|
89 |
isAMappedBuffer = false; |
|
90 |
} |
|
91 |
||
92 |
private void checkMapped() { |
|
93 |
if (!isAMappedBuffer) |
|
94 |
// Can only happen if a luser explicitly casts a direct byte buffer |
|
95 |
throw new UnsupportedOperationException(); |
|
96 |
} |
|
97 |
||
6128
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
98 |
// Returns the distance (in bytes) of the buffer from the page aligned address |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
99 |
// of the mapping. Computed each time to avoid storing in every direct buffer. |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
100 |
private long mappingOffset() { |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
101 |
int ps = Bits.pageSize(); |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
102 |
long offset = address % ps; |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
103 |
return (offset >= 0) ? offset : (ps + offset); |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
104 |
} |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
105 |
|
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
106 |
private long mappingAddress(long mappingOffset) { |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
107 |
return address - mappingOffset; |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
108 |
} |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
109 |
|
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
110 |
private long mappingLength(long mappingOffset) { |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
111 |
return (long)capacity() + mappingOffset; |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
112 |
} |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
113 |
|
2 | 114 |
/** |
115 |
* Tells whether or not this buffer's content is resident in physical |
|
116 |
* memory. |
|
117 |
* |
|
118 |
* <p> A return value of <tt>true</tt> implies that it is highly likely |
|
119 |
* that all of the data in this buffer is resident in physical memory and |
|
120 |
* may therefore be accessed without incurring any virtual-memory page |
|
121 |
* faults or I/O operations. A return value of <tt>false</tt> does not |
|
122 |
* necessarily imply that the buffer's content is not resident in physical |
|
123 |
* memory. |
|
124 |
* |
|
125 |
* <p> The returned value is a hint, rather than a guarantee, because the |
|
126 |
* underlying operating system may have paged out some of the buffer's data |
|
127 |
* by the time that an invocation of this method returns. </p> |
|
128 |
* |
|
129 |
* @return <tt>true</tt> if it is likely that this buffer's content |
|
130 |
* is resident in physical memory |
|
131 |
*/ |
|
132 |
public final boolean isLoaded() { |
|
133 |
checkMapped(); |
|
134 |
if ((address == 0) || (capacity() == 0)) |
|
135 |
return true; |
|
6128
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
136 |
long offset = mappingOffset(); |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
137 |
long length = mappingLength(offset); |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
138 |
return isLoaded0(mappingAddress(offset), length, Bits.pageCount(length)); |
2 | 139 |
} |
140 |
||
141 |
/** |
|
142 |
* Loads this buffer's content into physical memory. |
|
143 |
* |
|
144 |
* <p> This method makes a best effort to ensure that, when it returns, |
|
145 |
* this buffer's content is resident in physical memory. Invoking this |
|
146 |
* method may cause some number of page faults and I/O operations to |
|
147 |
* occur. </p> |
|
148 |
* |
|
149 |
* @return This buffer |
|
150 |
*/ |
|
151 |
public final MappedByteBuffer load() { |
|
152 |
checkMapped(); |
|
153 |
if ((address == 0) || (capacity() == 0)) |
|
154 |
return this; |
|
6128
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
155 |
long offset = mappingOffset(); |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
156 |
long length = mappingLength(offset); |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
157 |
load0(mappingAddress(offset), length); |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
158 |
|
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
159 |
// touch each page |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
160 |
Unsafe unsafe = Unsafe.getUnsafe(); |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
161 |
int ps = Bits.pageSize(); |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
162 |
int count = Bits.pageCount(length); |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
163 |
long a = mappingAddress(offset); |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
164 |
for (int i=0; i<count; i++) { |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
165 |
unsafe.getByte(a); |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
166 |
a += ps; |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
167 |
} |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
168 |
|
2 | 169 |
return this; |
170 |
} |
|
171 |
||
172 |
/** |
|
173 |
* Forces any changes made to this buffer's content to be written to the |
|
174 |
* storage device containing the mapped file. |
|
175 |
* |
|
176 |
* <p> If the file mapped into this buffer resides on a local storage |
|
177 |
* device then when this method returns it is guaranteed that all changes |
|
178 |
* made to the buffer since it was created, or since this method was last |
|
179 |
* invoked, will have been written to that device. |
|
180 |
* |
|
181 |
* <p> If the file does not reside on a local device then no such guarantee |
|
182 |
* is made. |
|
183 |
* |
|
184 |
* <p> If this buffer was not mapped in read/write mode ({@link |
|
185 |
* java.nio.channels.FileChannel.MapMode#READ_WRITE}) then invoking this |
|
186 |
* method has no effect. </p> |
|
187 |
* |
|
188 |
* @return This buffer |
|
189 |
*/ |
|
190 |
public final MappedByteBuffer force() { |
|
191 |
checkMapped(); |
|
6128
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
192 |
if ((address != 0) && (capacity() != 0)) { |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
193 |
long offset = mappingOffset(); |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
194 |
force0(mappingAddress(offset), mappingLength(offset)); |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
195 |
} |
2 | 196 |
return this; |
197 |
} |
|
198 |
||
6128
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
199 |
private native boolean isLoaded0(long address, long length, int pageCount); |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5506
diff
changeset
|
200 |
private native void load0(long address, long length); |
2 | 201 |
private native void force0(long address, long length); |
202 |
||
203 |
} |