1 16 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 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 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 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 (err.toString()); 97 } 98 } 99 | Popular Tags |