src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/util/SlotAllocator.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 44797 jaxp/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/util/SlotAllocator.java@8b3b3b911b8a
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     8
 * this work for additional information regarding copyright ownership.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    11
 * the License.  You may obtain a copy of the License at
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.xalan.internal.xsltc.compiler.util;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
import com.sun.org.apache.bcel.internal.generic.LocalVariableGen;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import com.sun.org.apache.bcel.internal.generic.Type;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
 * @author Jacek Ambroziak
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
final class SlotAllocator {
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
    private int   _firstAvailableSlot;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
    private int   _size = 8;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
    private int   _free = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
    private int[] _slotsTaken = new int[_size];
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
    public void initialize(LocalVariableGen[] vars) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
        final int length = vars.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
        int slot = 0, size, index;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
        for (int i = 0; i < length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
            size  = vars[i].getType().getSize();
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
            index = vars[i].getIndex();
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
            slot  = Math.max(slot, index + size);
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
        _firstAvailableSlot = slot;
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
    public int allocateSlot(Type type) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
        final int size = type.getSize();
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
        final int limit = _free;
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
        int slot = _firstAvailableSlot, where = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
        if (_free + size > _size) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
            final int[] array = new int[_size *= 2];
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
            for (int j = 0; j < limit; j++)
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
                array[j] = _slotsTaken[j];
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
            _slotsTaken = array;
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
        while (where < limit) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
            if (slot + size <= _slotsTaken[where]) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
                // insert
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
                for (int j = limit - 1; j >= where; j--)
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
                    _slotsTaken[j + size] = _slotsTaken[j];
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
                break;
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
                slot = _slotsTaken[where++] + 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
        for (int j = 0; j < size; j++)
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
            _slotsTaken[where + j] = slot + j;
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
        _free += size;
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
        return slot;
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
    public void releaseSlot(LocalVariableGen lvg) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
        final int size = lvg.getType().getSize();
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
        final int slot = lvg.getIndex();
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
        final int limit = _free;
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
        for (int i = 0; i < limit; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
            if (_slotsTaken[i] == slot) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
                int j = i + size;
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
                while (j < limit) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
                    _slotsTaken[i++] = _slotsTaken[j++];
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
                _free -= size;
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
        String state = "Variable slot allocation error"+
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
                       "(size="+size+", slot="+slot+", limit="+limit+")";
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
        ErrorMsg err = new ErrorMsg(ErrorMsg.INTERNAL_ERR, state);
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
        throw new Error(err.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
}