1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.channel.socket.nio;
17
18 import java.lang.ref.SoftReference;
19 import java.nio.ByteBuffer;
20
21
22
23
24
25
26 final class SocketReceiveBufferPool {
27
28 private static final int POOL_SIZE = 8;
29
30 @SuppressWarnings("unchecked")
31 private final SoftReference<ByteBuffer>[] pool = new SoftReference[POOL_SIZE];
32
33 SocketReceiveBufferPool() {
34 super();
35 }
36
37 final ByteBuffer acquire(int size) {
38 final SoftReference<ByteBuffer>[] pool = this.pool;
39 for (int i = 0; i < POOL_SIZE; i ++) {
40 SoftReference<ByteBuffer> ref = pool[i];
41 if (ref == null) {
42 continue;
43 }
44
45 ByteBuffer buf = ref.get();
46 if (buf == null) {
47 pool[i] = null;
48 continue;
49 }
50
51 if (buf.capacity() < size) {
52 continue;
53 }
54
55 pool[i] = null;
56
57 buf.clear();
58 return buf;
59 }
60
61 ByteBuffer buf = ByteBuffer.allocateDirect(normalizeCapacity(size));
62 buf.clear();
63 return buf;
64 }
65
66 final void release(ByteBuffer buffer) {
67 final SoftReference<ByteBuffer>[] pool = this.pool;
68 for (int i = 0; i < POOL_SIZE; i ++) {
69 SoftReference<ByteBuffer> ref = pool[i];
70 if (ref == null || ref.get() == null) {
71 pool[i] = new SoftReference<ByteBuffer>(buffer);
72 return;
73 }
74 }
75
76
77 final int capacity = buffer.capacity();
78 for (int i = 0; i< POOL_SIZE; i ++) {
79 SoftReference<ByteBuffer> ref = pool[i];
80 ByteBuffer pooled = ref.get();
81 if (pooled == null) {
82 pool[i] = null;
83 continue;
84 }
85
86 if (pooled.capacity() < capacity) {
87 pool[i] = new SoftReference<ByteBuffer>(buffer);
88 return;
89 }
90 }
91 }
92
93 private static final int normalizeCapacity(int capacity) {
94
95 int q = capacity >>> 10;
96 int r = capacity & 1023;
97 if (r != 0) {
98 q ++;
99 }
100 return q << 10;
101 }
102 }