jdk/src/share/classes/com/sun/java/util/jar/pack/Fixups.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 7192 445c518364c4
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.java.util.jar.pack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.java.util.jar.pack.ConstantPool.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Collection of relocatable constant pool references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * It operates with respect to a particular byte array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * and stores some of its state in the bytes themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * As a Collection, it can be iterated over, but it is not a List,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * since it does not natively support indexed access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class Fixups extends AbstractCollection implements Constants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    byte[] bytes;    // the subject of the relocations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    int head;        // desc locating first reloc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    int tail;        // desc locating last reloc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    int size;        // number of relocations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    Entry[] entries; // [0..size-1] relocations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    int[] bigDescs;  // descs which cannot be stored in the bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // A "desc" (descriptor) is a bit-encoded pair of a location
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // and format.  Every fixup occurs at a "desc".  Until final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // patching, bytes addressed by descs may also be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // link this data structure together.  If the bytes are missing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // or if the "desc" is too large to encode in the bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // it is kept in the bigDescs array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    Fixups(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.bytes = bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        entries = new Entry[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        bigDescs = noBigDescs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    Fixups() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        // If there are no bytes, all descs are kept in bigDescs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this((byte[])null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    Fixups(byte[] bytes, Collection fixups) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        addAll(fixups);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    Fixups(Collection fixups) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        this((byte[])null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        addAll(fixups);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static final int MINBIGSIZE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // cleverly share empty bigDescs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static int[] noBigDescs = {MINBIGSIZE};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public void trimToSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (size != entries.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            Entry[] oldEntries = entries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            entries = new Entry[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            System.arraycopy(oldEntries, 0, entries, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        int bigSize = bigDescs[BIGSIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (bigSize == MINBIGSIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            bigDescs = noBigDescs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } else if (bigSize != bigDescs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            int[] oldBigDescs = bigDescs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            bigDescs = new int[bigSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            System.arraycopy(oldBigDescs, 0, bigDescs, 0, bigSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public void visitRefs(Collection refs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            refs.add(entries[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (bytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            // Clean the bytes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            for (Iterator i = iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                Fixup fx = (Fixup) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                //System.out.println("clean "+fx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                storeIndex(fx.location(), fx.format(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (bigDescs != noBigDescs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            bigDescs[BIGSIZE] = MINBIGSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // do not trim to size, however
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public void setBytes(byte[] newBytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (bytes == newBytes)  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        ArrayList old = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        assert((old = new ArrayList(this)) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (bytes == null || newBytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            // One or the other representations is deficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            // Construct a checkpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            ArrayList save = new ArrayList(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            bytes = newBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            addAll(save);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            // assume newBytes is some sort of bitwise copy of the old bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            bytes = newBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        assert(old.equals(new ArrayList(this)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    static final int LOC_SHIFT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    static final int FMT_MASK = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    static final byte UNUSED_BYTE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    static final byte OVERFLOW_BYTE = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    // fill pointer of bigDescs array is in element [0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    static final int BIGSIZE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    // Format values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public static final int U2_FORMAT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public static final int U1_FORMAT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    // Special values for the static methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static final int SPECIAL_LOC = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static final int SPECIAL_FMT = U2_FORMAT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    static int fmtLen(int fmt) { return 1+(fmt-U1_FORMAT)/(U2_FORMAT-U1_FORMAT); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    static int descLoc(int desc) { return desc >>> LOC_SHIFT; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    static int descFmt(int desc) { return desc  &  FMT_MASK; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    static int descEnd(int desc) { return descLoc(desc) + fmtLen(descFmt(desc)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    static int makeDesc(int loc, int fmt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        int desc = (loc << LOC_SHIFT) | fmt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        assert(descLoc(desc) == loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        assert(descFmt(desc) == fmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    int fetchDesc(int loc, int fmt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        byte b1 = bytes[loc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        assert(b1 != OVERFLOW_BYTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (fmt == U2_FORMAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            byte b2 = bytes[loc+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            value = ((b1 & 0xFF) << 8) + (b2 & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            value = (b1 & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // Stored loc field is difference between its own loc and next loc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return value + (loc << LOC_SHIFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    boolean storeDesc(int loc, int fmt, int desc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (bytes == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        int value = desc - (loc << LOC_SHIFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        byte b1, b2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        switch (fmt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        case U2_FORMAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            assert(bytes[loc+0] == UNUSED_BYTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            assert(bytes[loc+1] == UNUSED_BYTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            b1 = (byte)(value >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            b2 = (byte)(value >> 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (value == (value & 0xFFFF) && b1 != OVERFLOW_BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                bytes[loc+0] = b1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                bytes[loc+1] = b2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                assert(fetchDesc(loc, fmt) == desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        case U1_FORMAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            assert(bytes[loc] == UNUSED_BYTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            b1 = (byte)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (value == (value & 0xFF) && b1 != OVERFLOW_BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                bytes[loc] = b1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                assert(fetchDesc(loc, fmt) == desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        default: assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // Failure.  Caller must allocate a bigDesc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        bytes[loc] = OVERFLOW_BYTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        assert(fmt==U1_FORMAT || (bytes[loc+1]=(byte)bigDescs[BIGSIZE])!=999);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    void storeIndex(int loc, int fmt, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        storeIndex(bytes, loc, fmt, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    void storeIndex(byte[] bytes, int loc, int fmt, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        switch (fmt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        case U2_FORMAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            assert(value == (value & 0xFFFF)) : (value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            bytes[loc+0] = (byte)(value >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            bytes[loc+1] = (byte)(value >> 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        case U1_FORMAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            assert(value == (value & 0xFF)) : (value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            bytes[loc] = (byte)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        default: assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /** Simple and necessary tuple to present each fixup. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    class Fixup implements Comparable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        int desc;         // location and format of reloc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        Entry entry;      // which entry to plug into the bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        Fixup(int desc, Entry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            this.desc = desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            this.entry = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        public Fixup(int loc, int fmt, Entry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            this.desc = makeDesc(loc, fmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            this.entry = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        public int location() { return descLoc(desc); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        public int format() { return descFmt(desc); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        public Entry entry() { return entry; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        public int compareTo(Fixup that) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            // Ordering depends only on location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return this.location() - that.location();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        public int compareTo(Object that) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return compareTo((Fixup)that);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        public boolean equals(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            if (!(x instanceof Fixup))  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            Fixup that = (Fixup) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            return this.desc == that.desc && this.entry == that.entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            return "@"+location()+(format()==U1_FORMAT?".1":"")+"="+entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    class Itr implements Iterator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        int index = 0;               // index into entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        int bigIndex = BIGSIZE+1;    // index into bigDescs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        int next = head;             // desc pointing to next fixup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        public boolean hasNext() { return index < size; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        public void remove() { throw new UnsupportedOperationException(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        public Object next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            int thisIndex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return new Fixup(nextDesc(), entries[thisIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        int nextDesc() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            int thisIndex = index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            int thisDesc = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            if (index < size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                // Fetch next desc eagerly, in case this fixup gets finalized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                int loc = descLoc(thisDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                int fmt = descFmt(thisDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                if (bytes != null && bytes[loc] != OVERFLOW_BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    next = fetchDesc(loc, fmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    // The unused extra byte is "asserted" to be equal to BI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    // This helps keep the overflow descs in sync.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    assert(fmt==U1_FORMAT || bytes == null || bytes[loc+1]==(byte)bigIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    next = bigDescs[bigIndex++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return thisDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public Iterator iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return new Itr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public void add(int location, int format, Entry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        addDesc(makeDesc(location, format), entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public boolean add(Fixup f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        addDesc(f.desc, f.entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public boolean add(Object fixup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return add((Fixup) fixup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public boolean addAll(Collection c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (c instanceof Fixups) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            // Use knowledge of Itr structure to avoid building little structs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            Fixups that = (Fixups) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (that.size == 0)  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (this.size == 0 && entries.length < that.size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                growEntries(that.size);  // presize exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            Entry[] thatEntries = that.entries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            for (Itr i = that.new Itr(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                int ni = i.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                addDesc(i.nextDesc(), thatEntries[ni]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            return super.addAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    // Here is how things get added:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    private void addDesc(int thisDesc, Entry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if (entries.length == size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            growEntries(size * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        entries[size] = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            head = tail = thisDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            int prevDesc = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            // Store new desc in previous tail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            int prevLoc = descLoc(prevDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            int prevFmt = descFmt(prevDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            int prevLen = fmtLen(prevFmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            int thisLoc = descLoc(thisDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            // The collection must go in ascending order, and not overlap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            if (thisLoc < prevLoc + prevLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                badOverlap(thisLoc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            tail = thisDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if (!storeDesc(prevLoc, prevFmt, thisDesc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                // overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                int bigSize = bigDescs[BIGSIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                if (bigDescs.length == bigSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    growBigDescs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                //System.out.println("bigDescs["+bigSize+"] = "+thisDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                bigDescs[bigSize++] = thisDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                bigDescs[BIGSIZE] = bigSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        size += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    private void badOverlap(int thisLoc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        throw new IllegalArgumentException("locs must be ascending and must not overlap:  "+thisLoc+" >> "+this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private void growEntries(int newSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        Entry[] oldEntries = entries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        entries = new Entry[Math.max(3, newSize)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        System.arraycopy(oldEntries, 0, entries, 0, oldEntries.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private void growBigDescs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        int[] oldBigDescs = bigDescs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        bigDescs = new int[oldBigDescs.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        System.arraycopy(oldBigDescs, 0, bigDescs, 0, oldBigDescs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /// Static methods that optimize the use of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    Object add(Object prevFixups,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
               byte[] bytes, int loc, int fmt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
               Entry e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        Fixups f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (prevFixups == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if (loc == SPECIAL_LOC && fmt == SPECIAL_FMT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                // Special convention:  If the attribute has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                // U2 relocation at position zero, store the Entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                // rather than building a Fixups structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            f = new Fixups(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        } else if (!(prevFixups instanceof Fixups)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            // Recognize the special convention:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            Entry firstEntry = (Entry) prevFixups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            f = new Fixups(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            f.add(SPECIAL_LOC, SPECIAL_FMT, firstEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            f = (Fixups) prevFixups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            assert(f.bytes == bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        f.add(loc, fmt, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    void setBytes(Object fixups, byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (fixups instanceof Fixups) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            Fixups f = (Fixups) fixups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            f.setBytes(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    Object trimToSize(Object fixups) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (fixups instanceof Fixups) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            Fixups f = (Fixups) fixups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            f.trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            if (f.size() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                fixups = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return fixups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    // Iterate over all the references in this set of fixups.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    void visitRefs(Object fixups, Collection refs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (fixups == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        } else if (!(fixups instanceof Fixups)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            // Special convention; see above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            refs.add((Entry) fixups);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            Fixups f = (Fixups) fixups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            f.visitRefs(refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    // Clear out this set of fixups by replacing each reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    // by a hardcoded coding of its reference, drawn from ix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    public static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    void finishRefs(Object fixups, byte[] bytes, ConstantPool.Index ix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (fixups == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        if (!(fixups instanceof Fixups)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            // Special convention; see above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            int index = ix.indexOf((Entry) fixups);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            storeIndex(bytes, SPECIAL_LOC, SPECIAL_FMT, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        Fixups f = (Fixups) fixups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        assert(f.bytes == bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        f.finishRefs(ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    void finishRefs(ConstantPool.Index ix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        for (Iterator i = iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            Fixup fx = (Fixup) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            int index = ix.indexOf(fx.entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            //System.out.println("finish "+fx+" = "+index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            // Note that the iterator has already fetched the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            // bytes we are about to overwrite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            storeIndex(fx.location(), fx.format(), index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        // Further iterations should do nothing:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        bytes = null;  // do not clean them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /// Testing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    public static void main(String[] av) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        byte[] bytes = new byte[1 << 20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        ConstantPool cp = new ConstantPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        Fixups f = new Fixups(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        boolean isU1 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        int span = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        int nextLoc = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        int[] locs = new int[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        final int[] indexes = new int[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        int iptr = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        for (int loc = 0; loc < bytes.length; loc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            if (loc == nextLoc && loc+1 < bytes.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                int fmt = (isU1 ? U1_FORMAT : U2_FORMAT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                Entry e = ConstantPool.getUtf8Entry("L"+loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                f.add(loc, fmt, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                isU1 ^= true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                if (iptr < 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    // Make it close in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    nextLoc += fmtLen(fmt) + (iptr < 5 ? 0 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    nextLoc += span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    span = (int)(span * 1.77);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                // Here are the bytes that would have gone here:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                locs[iptr] = loc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                if (fmt == U1_FORMAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    indexes[iptr++] = (loc & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    indexes[iptr++] = ((loc & 0xFF) << 8) | ((loc+1) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    ++loc;  // skip a byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            bytes[loc] = (byte)loc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        System.out.println("size="+f.size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                           +" overflow="+(f.bigDescs[BIGSIZE]-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        System.out.println("Fixups: "+f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        // Test collection contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        assert(iptr == 1+f.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        List l = new ArrayList(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        Collections.sort(l);  // should not change the order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (!l.equals(new ArrayList(f)))  System.out.println("** disordered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        f.setBytes(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        if (!l.equals(new ArrayList(f)))  System.out.println("** bad set 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        f.setBytes(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        if (!l.equals(new ArrayList(f)))  System.out.println("** bad set 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        Fixups f3 = new Fixups(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        if (!l.equals(new ArrayList(f3))) System.out.println("** bad set 3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        Iterator fi = f.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        for (int i = 1; i < iptr; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            Fixup fx = (Fixup) fi.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (fx.location() != locs[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                System.out.println("** "+fx+" != "+locs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            if (fx.format() == U1_FORMAT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                System.out.println(fx+" -> "+bytes[locs[i]]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                System.out.println(fx+" -> "+bytes[locs[i]]+" "+bytes[locs[i]+1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        assert(!fi.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        indexes[0] = 1;  // like iptr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        Index ix = new Index("ix") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            public int indexOf(Entry e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                return indexes[indexes[0]++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        f.finishRefs(ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        for (int loc = 0; loc < bytes.length; loc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            if (bytes[loc] != (byte)loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                System.out.println("** ["+loc+"] = "+bytes[loc]+" != "+(byte)loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
//*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
}