BytePoolDim1.java 1.9 KB
Newer Older
1 2
package org.postgresql.core;

B
Bruce Momjian 已提交
3
/*
4 5 6
 * A simple and efficient class to pool one dimensional byte arrays
 * of different sizes.
 */
7 8
public class BytePoolDim1
{
B
Bruce Momjian 已提交
9
	/*
10 11 12
	 * The maximum size of the array we manage.
	 */
	int maxsize = 256;
B
Bruce Momjian 已提交
13
	/*
14 15 16
	 * The pools not currently in use
	 */
	ObjectPool notusemap[] = new ObjectPool[maxsize + 1];
B
Bruce Momjian 已提交
17
	/*
18 19 20
	 * The pools currently in use
	 */
	ObjectPool inusemap[] = new ObjectPool[maxsize + 1];
B
Bruce Momjian 已提交
21
	/*
22 23 24
	 *
	 */
	byte binit[][] = new byte[maxsize + 1][0];
25

B
Bruce Momjian 已提交
26
	/*
27 28 29 30 31 32 33 34 35 36
	 * Construct a new pool
	 */
	public BytePoolDim1()
	{
		for (int i = 0; i <= maxsize; i++)
		{
			binit[i] = new byte[i];
			inusemap[i] = new SimpleObjectPool();
			notusemap[i] = new SimpleObjectPool();
		}
37 38
	}

B
Bruce Momjian 已提交
39
	/*
40 41 42 43 44 45 46 47 48
	 * Allocate a byte[] of a specified size and put it in the pool. If it's
	 * larger than maxsize then it is not pooled.
	 * @return the byte[] allocated
	 */
	public byte[] allocByte(int size)
	{
		// for now until the bug can be removed
		return new byte[size];
		/*
49 50 51 52
				  // Don't pool if >maxsize
				if (size > maxsize){
				return new byte[size];
			}
53

54 55 56
				ObjectPool not_usel = notusemap[size];
				ObjectPool in_usel = inusemap[size];
				byte b[] = null;
57

58 59 60 61 62 63 64 65
				  // Fetch from the unused pool if available otherwise allocate a new
				  // now array
				if (!not_usel.isEmpty()) {
				Object o = not_usel.remove();
				b = (byte[]) o;
			} else
				b = new byte[size];
				in_usel.add(b);
66

67 68
				return b;
		*/
69
	}
70

B
Bruce Momjian 已提交
71
	/*
72 73 74 75 76 77 78 79
	 * Release an array
	 * @param b byte[] to release
	 */
	public void release(byte[] b)
	{
		// If it's larger than maxsize then we don't touch it
		if (b.length > maxsize)
			return ;
80

81 82
		ObjectPool not_usel = notusemap[b.length];
		ObjectPool in_usel = inusemap[b.length];
83

84 85 86
		in_usel.remove(b);
		not_usel.add(b);
	}
87

B
Bruce Momjian 已提交
88
	/*
89 90 91 92 93 94 95 96 97 98
	 * Deallocate all
	 * @deprecated Real bad things happen if this is called!
	 */
	public void deallocate()
	{
		//for(int i = 0; i <= maxsize; i++){
		//	  notusemap[i].addAll(inusemap[i]);
		//	  inusemap[i].clear();
		//}
	}
99 100
}