KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > access > sort > NodeAllocator


1 /*
2
3    Derby - Class org.apache.derby.impl.store.access.sort.NodeAllocator
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.store.access.sort;
23
24 /**
25
26   NodeAllocator manages an array of nodes which can be reused.
27
28 **/

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     /**
42     Construct an empty allocator. The caller must call
43     init() before using it.
44     **/

45     public NodeAllocator()
46     {
47         array = null;
48         nAllocated = 0;
49         maxSize = 0;
50     }
51
52     public Node newNode()
53     {
54         // Caller forgot to init?
55
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         // Do we need to try reallocating the array?
70
if (nAllocated == array.length)
71         {
72             // If the array is already the maximum size, then
73
// tell the caller that there are no more nodes
74
// available.
75
if (array.length >= maxSize)
76                 return null;
77
78             // Attempt to allocate a new array. If the allocation
79
// fails, tell the caller that there are no more
80
// nodes available.
81
Node[] newArray = new Node[array.length * GROWTH_MULTIPLIER];
82             if (newArray == null)
83                 return null;
84
85             // The new array was successfully allocated. Copy the
86
// nodes from the original array into it, and make the
87
// new array the allocator's array.
88
System.arraycopy(array, 0, newArray, 0, array.length);
89             array = newArray;
90         }
91
92         // If this slot in the array hasn't had a node
93
// allocated for it yet, do so now.
94
if (array[nAllocated] == null)
95             array[nAllocated] = new Node(nAllocated);
96
97         // Return the node and increase the allocated count.
98
return array[nAllocated++];
99     }
100
101     /**
102     Return a node to the allocator.
103     **/

104     public void freeNode(Node n)
105     {
106         n.reset();
107         n.rightLink = freeList;
108         freeList = n;
109     }
110
111     /**
112     Initialize the allocator with default values for
113     initial and maximum size. Returns false if sufficient
114     memory could not be allocated.
115     **/

116     public boolean init()
117     {
118         return init(DEFAULT_INIT_SIZE, DEFAULT_MAX_SIZE);
119     }
120
121     /**
122     Initialize the allocator with default values for
123     initial size and the provided maximum size.
124     Returns false if sufficient memory could not be allocated.
125     **/

126     public boolean init(int maxSize)
127     {
128         return init(DEFAULT_INIT_SIZE, maxSize);
129     }
130
131     /**
132     Initialize the allocator with the given initial and
133     maximum sizes. This method does not check, but assumes
134     that the value of initSize is less than the value of
135     maxSize, and that they are both powers of two. Returns
136     false if sufficient memory could not be allocated.
137     **/

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     /**
151     Expand the node allocator's capacity by certain percent.
152     **/

153     public void grow(int percent)
154     {
155         if (percent > 0) // cannot shrink
156
maxSize = maxSize * (100+percent)/100;
157     }
158
159     /**
160     Clear all nodes that this allocator has allocated.
161     The allocator must already have been initialized.
162     **/

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