1 21 22 package org.apache.derby.impl.store.access.sort; 23 24 29 30 final class NodeAllocator 31 { 32 private static final int DEFAULT_INIT_SIZE = 128; 33 private static final int GROWTH_MULTIPLIER = 2; 34 private static final int DEFAULT_MAX_SIZE = 1024; 35 36 private Node array[]; 37 private int maxSize; 38 private int nAllocated; 39 private Node freeList = null; 40 41 45 public NodeAllocator() 46 { 47 array = null; 48 nAllocated = 0; 49 maxSize = 0; 50 } 51 52 public Node newNode() 53 { 54 if (array == null) 56 { 57 if (init() == false) 58 return null; 59 } 60 61 if (freeList != null) 62 { 63 Node n = freeList; 64 freeList = n.rightLink; 65 n.rightLink = null; 66 return n; 67 } 68 69 if (nAllocated == array.length) 71 { 72 if (array.length >= maxSize) 76 return null; 77 78 Node[] newArray = new Node[array.length * GROWTH_MULTIPLIER]; 82 if (newArray == null) 83 return null; 84 85 System.arraycopy(array, 0, newArray, 0, array.length); 89 array = newArray; 90 } 91 92 if (array[nAllocated] == null) 95 array[nAllocated] = new Node(nAllocated); 96 97 return array[nAllocated++]; 99 } 100 101 104 public void freeNode(Node n) 105 { 106 n.reset(); 107 n.rightLink = freeList; 108 freeList = n; 109 } 110 111 116 public boolean init() 117 { 118 return init(DEFAULT_INIT_SIZE, DEFAULT_MAX_SIZE); 119 } 120 121 126 public boolean init(int maxSize) 127 { 128 return init(DEFAULT_INIT_SIZE, maxSize); 129 } 130 131 138 public boolean init(int initSize, int maxSize) 139 { 140 this.maxSize = maxSize; 141 if (maxSize < initSize) 142 initSize = maxSize; 143 array = new Node[initSize]; 144 if (array == null) 145 return false; 146 nAllocated = 0; 147 return true; 148 } 149 150 153 public void grow(int percent) 154 { 155 if (percent > 0) maxSize = maxSize * (100+percent)/100; 157 } 158 159 163 public void reset() 164 { 165 if (array == null) 166 return; 167 for (int i = 0; i < nAllocated; i++) 168 array[i].reset(); 169 nAllocated = 0; 170 freeList = null; 171 } 172 173 public void close() 174 { 175 array = null; 176 nAllocated = 0; 177 maxSize = 0; 178 freeList = null; 179 } 180 181 public int capacity() 182 { 183 return maxSize; 184 } 185 } 186 | Popular Tags |