23 * questions.
24 */
25
26 package sun.nio.ch;
27
28 import java.lang.ref.SoftReference;
29 import java.lang.reflect.*;
30 import java.io.IOException;
31 import java.io.FileDescriptor;
32 import java.nio.ByteBuffer;
33 import java.nio.MappedByteBuffer;
34 import java.nio.channels.*;
35 import java.security.AccessController;
36 import java.security.PrivilegedAction;
37 import java.util.*;
38 import sun.misc.Unsafe;
39 import sun.misc.Cleaner;
40 import sun.security.action.GetPropertyAction;
41
42
43 class Util {
44
45 // -- Caches --
46
47 // The number of temp buffers in our pool
48 private static final int TEMP_BUF_POOL_SIZE = 8;
49
50 // Per-thread cache of temporary direct buffers
51 private static ThreadLocal<BufferCache> bufferCache =
52 new ThreadLocal<BufferCache>()
53 {
54 @Override
55 protected BufferCache initialValue() {
56 return new BufferCache();
57 }
58 };
59
60 /**
61 * A simple cache of direct buffers.
62 */
63 private static class BufferCache {
141 }
142 }
143
144 boolean isEmpty() {
145 return count == 0;
146 }
147
148 ByteBuffer removeFirst() {
149 assert count > 0;
150 ByteBuffer buf = buffers[start];
151 buffers[start] = null;
152 start = next(start);
153 count--;
154 return buf;
155 }
156 }
157
158 /**
159 * Returns a temporary buffer of at least the given size
160 */
161 static ByteBuffer getTemporaryDirectBuffer(int size) {
162 BufferCache cache = bufferCache.get();
163 ByteBuffer buf = cache.get(size);
164 if (buf != null) {
165 return buf;
166 } else {
167 // No suitable buffer in the cache so we need to allocate a new
168 // one. To avoid the cache growing then we remove the first
169 // buffer from the cache and free it.
170 if (!cache.isEmpty()) {
171 buf = cache.removeFirst();
172 free(buf);
173 }
174 return ByteBuffer.allocateDirect(size);
175 }
176 }
177
178 /**
179 * Releases a temporary buffer by returning to the cache or freeing it.
180 */
181 static void releaseTemporaryDirectBuffer(ByteBuffer buf) {
182 offerFirstTemporaryDirectBuffer(buf);
183 }
184
185 /**
186 * Releases a temporary buffer by returning to the cache or freeing it. If
187 * returning to the cache then insert it at the start so that it is
188 * likely to be returned by a subsequent call to getTemporaryDirectBuffer.
189 */
190 static void offerFirstTemporaryDirectBuffer(ByteBuffer buf) {
191 assert buf != null;
192 BufferCache cache = bufferCache.get();
193 if (!cache.offerFirst(buf)) {
194 // cache is full
195 free(buf);
196 }
197 }
198
199 /**
200 * Releases a temporary buffer by returning to the cache or freeing it. If
201 * returning to the cache then insert it at the end. This makes it
450
451 private static volatile String bugLevel = null;
452
453 static boolean atBugLevel(String bl) { // package-private
454 if (bugLevel == null) {
455 if (!sun.misc.VM.isBooted())
456 return false;
457 String value = AccessController.doPrivileged(
458 new GetPropertyAction("sun.nio.ch.bugLevel"));
459 bugLevel = (value != null) ? value : "";
460 }
461 return bugLevel.equals(bl);
462 }
463
464
465
466 // -- Initialization --
467
468 private static boolean loaded = false;
469
470 static void load() {
471 synchronized (Util.class) {
472 if (loaded)
473 return;
474 loaded = true;
475 java.security.AccessController
476 .doPrivileged(new sun.security.action.LoadLibraryAction("net"));
477 java.security.AccessController
478 .doPrivileged(new sun.security.action.LoadLibraryAction("nio"));
479 // IOUtil must be initialized; Its native methods are called from
480 // other places in native nio code so they must be set up.
481 IOUtil.initIDs();
482 }
483 }
484
485 }
|
23 * questions.
24 */
25
26 package sun.nio.ch;
27
28 import java.lang.ref.SoftReference;
29 import java.lang.reflect.*;
30 import java.io.IOException;
31 import java.io.FileDescriptor;
32 import java.nio.ByteBuffer;
33 import java.nio.MappedByteBuffer;
34 import java.nio.channels.*;
35 import java.security.AccessController;
36 import java.security.PrivilegedAction;
37 import java.util.*;
38 import sun.misc.Unsafe;
39 import sun.misc.Cleaner;
40 import sun.security.action.GetPropertyAction;
41
42
43 public class Util {
44
45 // -- Caches --
46
47 // The number of temp buffers in our pool
48 private static final int TEMP_BUF_POOL_SIZE = 8;
49
50 // Per-thread cache of temporary direct buffers
51 private static ThreadLocal<BufferCache> bufferCache =
52 new ThreadLocal<BufferCache>()
53 {
54 @Override
55 protected BufferCache initialValue() {
56 return new BufferCache();
57 }
58 };
59
60 /**
61 * A simple cache of direct buffers.
62 */
63 private static class BufferCache {
141 }
142 }
143
144 boolean isEmpty() {
145 return count == 0;
146 }
147
148 ByteBuffer removeFirst() {
149 assert count > 0;
150 ByteBuffer buf = buffers[start];
151 buffers[start] = null;
152 start = next(start);
153 count--;
154 return buf;
155 }
156 }
157
158 /**
159 * Returns a temporary buffer of at least the given size
160 */
161 public static ByteBuffer getTemporaryDirectBuffer(int size) {
162 BufferCache cache = bufferCache.get();
163 ByteBuffer buf = cache.get(size);
164 if (buf != null) {
165 return buf;
166 } else {
167 // No suitable buffer in the cache so we need to allocate a new
168 // one. To avoid the cache growing then we remove the first
169 // buffer from the cache and free it.
170 if (!cache.isEmpty()) {
171 buf = cache.removeFirst();
172 free(buf);
173 }
174 return ByteBuffer.allocateDirect(size);
175 }
176 }
177
178 /**
179 * Releases a temporary buffer by returning to the cache or freeing it.
180 */
181 public static void releaseTemporaryDirectBuffer(ByteBuffer buf) {
182 offerFirstTemporaryDirectBuffer(buf);
183 }
184
185 /**
186 * Releases a temporary buffer by returning to the cache or freeing it. If
187 * returning to the cache then insert it at the start so that it is
188 * likely to be returned by a subsequent call to getTemporaryDirectBuffer.
189 */
190 static void offerFirstTemporaryDirectBuffer(ByteBuffer buf) {
191 assert buf != null;
192 BufferCache cache = bufferCache.get();
193 if (!cache.offerFirst(buf)) {
194 // cache is full
195 free(buf);
196 }
197 }
198
199 /**
200 * Releases a temporary buffer by returning to the cache or freeing it. If
201 * returning to the cache then insert it at the end. This makes it
450
451 private static volatile String bugLevel = null;
452
453 static boolean atBugLevel(String bl) { // package-private
454 if (bugLevel == null) {
455 if (!sun.misc.VM.isBooted())
456 return false;
457 String value = AccessController.doPrivileged(
458 new GetPropertyAction("sun.nio.ch.bugLevel"));
459 bugLevel = (value != null) ? value : "";
460 }
461 return bugLevel.equals(bl);
462 }
463
464
465
466 // -- Initialization --
467
468 private static boolean loaded = false;
469
470 public static void load() {
471 synchronized (Util.class) {
472 if (loaded)
473 return;
474 loaded = true;
475 java.security.AccessController
476 .doPrivileged(new sun.security.action.LoadLibraryAction("net"));
477 java.security.AccessController
478 .doPrivileged(new sun.security.action.LoadLibraryAction("nio"));
479 // IOUtil must be initialized; Its native methods are called from
480 // other places in native nio code so they must be set up.
481 IOUtil.initIDs();
482 }
483 }
484
485 }
|