View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2001-2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * 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   * $Id: SlotAllocator.java,v 1.2.4.1 2005/09/05 11:32:51 pvedula Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.compiler.util;
25  
26  import com.sun.org.apache.bcel.internal.generic.LocalVariableGen;
27  import com.sun.org.apache.bcel.internal.generic.Type;
28  
29  /**
30   * @author Jacek Ambroziak
31   */
32  final class SlotAllocator {
33  
34      private int   _firstAvailableSlot;
35      private int   _size = 8;
36      private int   _free = 0;
37      private int[] _slotsTaken = new int[_size];
38  
39      public void initialize(LocalVariableGen[] vars) {
40          final int length = vars.length;
41          int slot = 0, size, index;
42  
43          for (int i = 0; i < length; i++) {
44              size  = vars[i].getType().getSize();
45              index = vars[i].getIndex();
46              slot  = Math.max(slot, index + size);
47          }
48          _firstAvailableSlot = slot;
49      }
50  
51      public int allocateSlot(Type type) {
52          final int size = type.getSize();
53          final int limit = _free;
54          int slot = _firstAvailableSlot, where = 0;
55  
56          if (_free + size > _size) {
57              final int[] array = new int[_size *= 2];
58              for (int j = 0; j < limit; j++)
59                  array[j] = _slotsTaken[j];
60              _slotsTaken = array;
61          }
62  
63          while (where < limit) {
64              if (slot + size <= _slotsTaken[where]) {
65                  // insert
66                  for (int j = limit - 1; j >= where; j--)
67                      _slotsTaken[j + size] = _slotsTaken[j];
68                  break;
69              }
70              else {
71                  slot = _slotsTaken[where++] + 1;
72              }
73          }
74  
75          for (int j = 0; j < size; j++)
76              _slotsTaken[where + j] = slot + j;
77  
78          _free += size;
79          return slot;
80      }
81  
82      public void releaseSlot(LocalVariableGen lvg) {
83          final int size = lvg.getType().getSize();
84          final int slot = lvg.getIndex();
85          final int limit = _free;
86  
87          for (int i = 0; i < limit; i++) {
88              if (_slotsTaken[i] == slot) {
89                  int j = i + size;
90                  while (j < limit) {
91                      _slotsTaken[i++] = _slotsTaken[j++];
92                  }
93                  _free -= size;
94                  return;
95              }
96          }
97          String state = "Variable slot allocation error"+
98                         "(size="+size+", slot="+slot+", limit="+limit+")";
99          ErrorMsg err = new ErrorMsg(ErrorMsg.INTERNAL_ERR, state);
100         throw new Error(err.toString());
101     }
102 }