1 26 27 28 package org.objectweb.jonathan.libs.resources; 29 30 import org.objectweb.jonathan.apis.resources.Chunk; 31 import org.objectweb.jonathan.apis.resources.ChunkFactory; 32 33 import org.objectweb.util.monolog.api.BasicLevel; 34 35 44 public class JChunkFactory implements ChunkFactory { 45 46 47 BigPoolChunk[] big_pool; 48 49 50 int big_pool_size; 51 52 53 int big_free; 54 55 56 int big_size; 57 58 59 SmallPoolChunk[] small_pool; 60 61 62 int small_pool_size; 63 64 65 int small_free; 66 67 68 int small_size; 69 70 81 public boolean verbose; 82 83 86 public JChunkFactory() { 87 this(5,32768,10,8192,false); 88 } 89 90 101 public JChunkFactory(int big_pool_size,int big_size, 102 int small_pool_size, int small_size, 103 boolean verbose) { 104 this.big_pool_size = big_pool_size; 105 this.big_size = big_size; 106 this.small_pool_size = small_pool_size; 107 this.small_size = small_size; 108 this.verbose = verbose; 109 110 big_free = big_pool_size; 111 big_pool = new BigPoolChunk[big_free]; 112 for (int i = 0; i < big_free; i++) { 113 big_pool[i] = new BigPoolChunk(); 114 } 115 small_free = small_pool_size; 116 small_pool = new SmallPoolChunk[small_free]; 117 for (int i = 0; i < small_free; i++) { 118 small_pool[i] = new SmallPoolChunk(); 119 } 120 } 121 122 127 public Chunk newChunk() { 128 synchronized(small_pool) { 129 PoolChunk chunk = null; 130 if (small_free > 0) { 131 chunk = small_pool[--small_free]; 132 small_pool[small_free] = null; 133 } else { 134 if ((LoggerProvider.logger != null) 136 &&(LoggerProvider.logger.isLoggable(BasicLevel.INFO))) { 137 LoggerProvider.logger.log(BasicLevel.INFO,"Allocating chunk outside small pool"); 138 } 139 chunk = new SmallPoolChunk(); 141 } 142 return chunk; 143 } 144 } 145 146 154 public Chunk newChunk(int size) { 155 if (size <= small_size) { 156 return newSmallChunk(); 157 } else if (size <= big_size) { 158 return newBigChunk(); 159 } else { 160 if ((LoggerProvider.logger != null) 162 &&(LoggerProvider.logger.isLoggable(BasicLevel.INFO))) { 163 LoggerProvider.logger.log(BasicLevel.INFO,"Chunk of size " + size + " requested."); 164 } 165 Chunk c = new Chunk(new byte[size],0,0); 167 return c; 169 } 170 } 171 172 173 176 Chunk newBigChunk() { 177 synchronized(big_pool) { 178 BigPoolChunk chunk = null; 179 if (big_free > 0) { 180 chunk = big_pool[--big_free]; 181 big_pool[big_free] = null; 182 } else { 183 if ((LoggerProvider.logger != null) 185 &&(LoggerProvider.logger.isLoggable(BasicLevel.INFO))) { 186 LoggerProvider.logger.log(BasicLevel.INFO,"Allocating chunk outside big pool"); 187 } 188 chunk = new BigPoolChunk(); 193 } 194 return chunk; 195 } 196 } 197 198 201 Chunk newSmallChunk() { 202 synchronized(small_pool) { 203 SmallPoolChunk chunk = null; 204 if (small_free > 0) { 205 chunk = small_pool[--small_free]; 206 small_pool[small_free] = null; 207 } else { 208 if ((LoggerProvider.logger != null) 213 &&(LoggerProvider.logger.isLoggable(BasicLevel.INFO))) { 214 LoggerProvider.logger.log(BasicLevel.INFO,"Allocating chunk outside small pool"); 215 } 216 chunk = new SmallPoolChunk(); 218 } 219 return chunk; 220 } 221 } 222 223 224 229 void addToBigPool(BigPoolChunk resource) { 230 synchronized(big_pool) { 231 if (big_free < big_pool.length) { 232 big_pool[big_free++] = resource; 233 } 234 } 235 } 236 237 242 void addToSmallPool(SmallPoolChunk resource) { 243 synchronized(small_pool) { 244 if (small_free < small_pool.length) { 245 small_pool[small_free++] = resource; 246 } 247 } 248 } 249 250 251 static class PoolChunk extends Chunk { 252 253 protected int acquired; 254 255 PoolChunk(int size) { 256 super(new byte[size],0,0); 257 acquired = 1; 258 } 259 260 public Chunk duplicate() { 261 return new Duplicate(this,offset,top); 262 } 263 264 public Chunk duplicate(int offset, int top) { 265 return new Duplicate(this,offset,top); 266 } 267 268 synchronized void acquire() { 269 acquired++; 270 } 271 } 272 273 final class SmallPoolChunk extends PoolChunk { 274 275 SmallPoolChunk () { 276 super(JChunkFactory.this.small_size); 277 } 278 279 public synchronized void release() { 280 if (acquired == 1) { 281 next = null; 282 super.release(); 283 addToSmallPool(this); 284 } else { 285 acquired--; 286 } 287 } 288 } 289 290 final class BigPoolChunk extends PoolChunk { 291 292 BigPoolChunk () { 293 super(JChunkFactory.this.big_size); 294 } 295 296 public synchronized void release() { 297 if (acquired == 1) { 298 next = null; 299 super.release(); 300 addToBigPool(this); 301 } else { 302 acquired--; 303 } 304 } 305 } 306 307 static final class Duplicate extends Chunk { 308 PoolChunk target; 309 310 Duplicate(PoolChunk chunk,int offset, int top) { 311 super(chunk.data,offset,top); 312 this.target = chunk; 313 chunk.acquire(); 314 } 315 316 public Chunk duplicate() { 317 return new Duplicate(target,offset,top); 318 } 319 320 public Chunk duplicate(int offset, int top) { 321 return new Duplicate(target,offset,top); 322 } 323 324 public void release() { 325 next = null; 326 target.release(); 327 target = null; 328 } 329 } 330 } 331 | Popular Tags |