1 28 29 package com.caucho.util; 30 31 38 public final class FreeList<T> { 39 private T _freeStack[]; 40 private int _top; 41 42 47 public FreeList(int size) 48 { 49 _freeStack = (T []) new Object [size]; 50 } 51 57 public T allocate() 58 { 59 synchronized (_freeStack) { 60 if (_top > 0) 61 return _freeStack[--_top]; 62 else 63 return null; 64 } 65 } 66 72 public boolean free(T obj) 73 { 74 synchronized (_freeStack) { 75 if (_top < _freeStack.length) { 76 _freeStack[_top++] = obj; 77 return true; 78 } 79 else 80 return false; 81 } 82 } 83 84 public boolean allowFree(T obj) 85 { 86 return _top < _freeStack.length; 87 } 88 89 95 public void freeCareful(T obj) 96 { 97 if (checkDuplicate(obj)) 98 throw new IllegalStateException ("tried to free object twice: " + obj); 99 100 free(obj); 101 } 102 103 106 public boolean checkDuplicate(T obj) 107 { 108 synchronized (_freeStack) { 109 int top = _top; 110 111 for (int i = _top - 1; i >= 0; i--) { 112 if (_freeStack[i] == obj) 113 return true; 114 } 115 } 116 117 return false; 118 } 119 } 120 | Popular Tags |