KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > util > FreeList


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.util;
30
31 /**
32  * FreeList provides a simple class to manage free objects. This is useful
33  * for large data structures that otherwise would gobble up huge GC time.
34  *
35  * <p>The free list is bounded. Freeing an object when the list is full will
36  * do nothing.
37  */

38 public final class FreeList<T> {
39   private T _freeStack[];
40   private int _top;
41
42   /**
43    * Create a new free list.
44    *
45    * @param initialSize maximum number of free objects to store.
46    */

47   public FreeList(int size)
48   {
49     _freeStack = (T []) new Object JavaDoc[size];
50   }
51   /**
52    * Try to get an object from the free list. Returns null if the free list
53    * is empty.
54    *
55    * @return the new object or null.
56    */

57   public T allocate()
58   {
59     synchronized (_freeStack) {
60       if (_top > 0)
61         return _freeStack[--_top];
62       else
63         return null;
64     }
65   }
66   /**
67    * Frees the object. If the free list is full, the object will be garbage
68    * collected.
69    *
70    * @param obj the object to be freed.
71    */

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   /**
90    * Frees the object. If the free list is full, the object will be garbage
91    * collected.
92    *
93    * @param obj the object to be freed.
94    */

95   public void freeCareful(T obj)
96   {
97     if (checkDuplicate(obj))
98       throw new IllegalStateException JavaDoc("tried to free object twice: " + obj);
99
100     free(obj);
101   }
102
103   /**
104    * Debugging to see if the object has already been freed.
105    */

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