/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Collection of relocatable constant pool references.
* It operates with respect to a particular byte array,
* and stores some of its state in the bytes themselves.
* <p>
* As a Collection, it can be iterated over, but it is not a List,
* since it does not natively support indexed access.
* <p>
*
* @author John Rose
*/
// A "desc" (descriptor) is a bit-encoded pair of a location
// and format. Every fixup occurs at a "desc". Until final
// patching, bytes addressed by descs may also be used to
// link this data structure together. If the bytes are missing,
// or if the "desc" is too large to encode in the bytes,
// it is kept in the bigDescs array.
}
Fixups() {
// If there are no bytes, all descs are kept in bigDescs.
this((byte[])null);
}
this(bytes);
}
this((byte[])null);
}
// cleverly share empty bigDescs:
public int size() {
return size;
}
public void trimToSize() {
}
if (bigSize == MINBIGSIZE) {
int[] oldBigDescs = bigDescs;
}
}
for (int i = 0; i < size; i++) {
}
}
public void clear() {
// Clean the bytes:
//System.out.println("clean "+fx);
}
}
size = 0;
if (bigDescs != noBigDescs)
// do not trim to size, however
}
public byte[] getBytes() {
return bytes;
}
@SuppressWarnings("unchecked")
// One or the other representations is deficient.
// Construct a checkpoint.
clear();
} else {
// assume newBytes is some sort of bitwise copy of the old bytes
}
}
// fill pointer of bigDescs array is in element [0]
// Format values:
// Special values for the static methods.
return desc;
}
assert(b1 != OVERFLOW_BYTE);
int value;
} else {
}
// Stored loc field is difference between its own loc and next loc.
}
return false;
switch (fmt) {
case U2_FORMAT:
return true;
}
break;
case U1_FORMAT:
return true;
}
break;
default: assert(false);
}
// Failure. Caller must allocate a bigDesc.
return false;
}
}
static
switch (fmt) {
case U2_FORMAT:
break;
case U1_FORMAT:
break;
default: assert(false);
}
}
/** Simple and necessary tuple to present each fixup. */
public static
}
}
// Ordering depends only on location.
}
}
if (!(x instanceof Fixup)) return false;
}
}
}
private
}
int nextDesc() {
index++;
// Fetch next desc eagerly, in case this fixup gets finalized.
} else {
// The unused extra byte is "asserted" to be equal to BI.
// This helps keep the overflow descs in sync.
}
}
return thisDesc;
}
}
return new Itr();
}
}
return true;
}
}
@SuppressWarnings("unchecked")
if (c instanceof Fixups) {
// Use knowledge of Itr structure to avoid building little structs.
}
return true;
} else {
return super.addAll(c);
}
}
// Here is how things get added:
if (size == 0) {
} else {
// Store new desc in previous tail.
// The collection must go in ascending order, and not overlap.
// overflow
growBigDescs();
//System.out.println("bigDescs["+bigSize+"] = "+thisDesc);
}
}
size += 1;
}
throw new IllegalArgumentException("locs must be ascending and must not overlap: "+thisLoc+" >> "+this);
}
}
private void growBigDescs() {
int[] oldBigDescs = bigDescs;
}
/// Static methods that optimize the use of this class.
public static
Entry e) {
Fixups f;
if (prevFixups == null) {
// Special convention: If the attribute has a
// U2 relocation at position zero, store the Entry
// rather than building a Fixups structure.
return e;
}
} else if (!(prevFixups instanceof Fixups)) {
// Recognize the special convention:
} else {
f = (Fixups) prevFixups;
}
return f;
}
public static
}
}
public static
f.trimToSize();
if (f.size() == 0)
}
return fixups;
}
// Iterate over all the references in this set of fixups.
public static
// Special convention; see above.
} else {
}
}
// Clear out this set of fixups by replacing each reference
// by a hardcoded coding of its reference, drawn from ix.
public static
return;
// Special convention; see above.
return;
}
f.finishRefs(ix);
}
if (isEmpty())
return;
//System.out.println("finish "+fx+" = "+index);
// Note that the iterator has already fetched the
// bytes we are about to overwrite.
}
// Further iterations should do nothing:
clear();
}
/*
/// Testing.
public static void main(String[] av) {
byte[] bytes = new byte[1 << 20];
ConstantPool cp = new ConstantPool();
Fixups f = new Fixups(bytes);
boolean isU1 = false;
int span = 3;
int nextLoc = 0;
int[] locs = new int[100];
final int[] indexes = new int[100];
int iptr = 1;
for (int loc = 0; loc < bytes.length; loc++) {
if (loc == nextLoc && loc+1 < bytes.length) {
int fmt = (isU1 ? U1_FORMAT : U2_FORMAT);
Entry e = ConstantPool.getUtf8Entry("L"+loc);
f.add(loc, fmt, e);
isU1 ^= true;
if (iptr < 10) {
// Make it close in.
nextLoc += fmtLen(fmt) + (iptr < 5 ? 0 : 1);
} else {
nextLoc += span;
span = (int)(span * 1.77);
}
// Here are the bytes that would have gone here:
locs[iptr] = loc;
if (fmt == U1_FORMAT) {
indexes[iptr++] = (loc & 0xFF);
} else {
indexes[iptr++] = ((loc & 0xFF) << 8) | ((loc+1) & 0xFF);
++loc; // skip a byte
}
continue;
}
bytes[loc] = (byte)loc;
}
System.out.println("size="+f.size()
+" overflow="+(f.bigDescs[BIGSIZE]-1));
System.out.println("Fixups: "+f);
// Test collection contents.
assert(iptr == 1+f.size());
List l = new ArrayList(f);
Collections.sort(l); // should not change the order
if (!l.equals(new ArrayList(f))) System.out.println("** disordered");
f.setBytes(null);
if (!l.equals(new ArrayList(f))) System.out.println("** bad set 1");
f.setBytes(bytes);
if (!l.equals(new ArrayList(f))) System.out.println("** bad set 2");
Fixups f3 = new Fixups(f);
if (!l.equals(new ArrayList(f3))) System.out.println("** bad set 3");
Iterator fi = f.iterator();
for (int i = 1; i < iptr; i++) {
Fixup fx = (Fixup) fi.next();
if (fx.location() != locs[i]) {
System.out.println("** "+fx+" != "+locs[i]);
}
if (fx.format() == U1_FORMAT)
System.out.println(fx+" -> "+bytes[locs[i]]);
else
System.out.println(fx+" -> "+bytes[locs[i]]+" "+bytes[locs[i]+1]);
}
assert(!fi.hasNext());
indexes[0] = 1; // like iptr
Index ix = new Index("ix") {
public int indexOf(Entry e) {
return indexes[indexes[0]++];
}
};
f.finishRefs(ix);
for (int loc = 0; loc < bytes.length; loc++) {
if (bytes[loc] != (byte)loc) {
System.out.println("** ["+loc+"] = "+bytes[loc]+" != "+(byte)loc);
}
}
}
//*/
}