KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > compiler > util > SlotAllocator


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: SlotAllocator.java,v 1.6 2004/02/16 22:26:44 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.compiler.util;
21
22 import org.apache.bcel.generic.LocalVariableGen;
23 import org.apache.bcel.generic.Type;
24
25 /**
26  * @author Jacek Ambroziak
27  */

28 final class SlotAllocator {
29
30     private int _firstAvailableSlot;
31     private int _size = 8;
32     private int _free = 0;
33     private int[] _slotsTaken = new int[_size];
34     
35     public void initialize(LocalVariableGen[] vars) {
36     final int length = vars.length;
37     int slot = 0, size, index;
38
39     for (int i = 0; i < length; i++) {
40         size = vars[i].getType().getSize();
41         index = vars[i].getIndex();
42         slot = Math.max(slot, index + size);
43     }
44     _firstAvailableSlot = slot;
45     }
46
47     public int allocateSlot(Type type) {
48     final int size = type.getSize();
49     final int limit = _free;
50     int slot = _firstAvailableSlot, where = 0;
51
52     if (_free + size > _size) {
53         final int[] array = new int[_size *= 2];
54         for (int j = 0; j < limit; j++)
55         array[j] = _slotsTaken[j];
56         _slotsTaken = array;
57     }
58
59     while (where < limit) {
60         if (slot + size <= _slotsTaken[where]) {
61         // insert
62
for (int j = limit - 1; j >= where; j--)
63             _slotsTaken[j + size] = _slotsTaken[j];
64         break;
65         }
66         else {
67         slot = _slotsTaken[where++] + 1;
68         }
69     }
70     
71     for (int j = 0; j < size; j++)
72         _slotsTaken[where + j] = slot + j;
73     
74     _free += size;
75     return slot;
76     }
77
78     public void releaseSlot(LocalVariableGen lvg) {
79     final int size = lvg.getType().getSize();
80     final int slot = lvg.getIndex();
81     final int limit = _free;
82     
83     for (int i = 0; i < limit; i++) {
84         if (_slotsTaken[i] == slot) {
85         int j = i + size;
86         while (j < limit) {
87             _slotsTaken[i++] = _slotsTaken[j++];
88         }
89         _free -= size;
90         return;
91         }
92     }
93     String JavaDoc state = "Variable slot allocation error"+
94                    "(size="+size+", slot="+slot+", limit="+limit+")";
95     ErrorMsg err = new ErrorMsg(ErrorMsg.INTERNAL_ERR, state);
96     throw new Error JavaDoc(err.toString());
97     }
98 }
99
Popular Tags