java字节码常量池处理说明
1. 根据java的字节码格式说明,常量池中每一项的大小不一样的。运行时,若要通过数组索引获取具体的一项时,
必须要经过一定的处理才能根据数组下标进行处理,具体的实现原理实际上就是空间换时间,可以参考kvm的实现:
每一项的定义,采用的是union的定义(会取最大的字节数进行内存分配)
/* Each of these represents one entry in the constant pool */
union constantPoolEntryStruct {
struct {
unsigned short classIndex;
unsigned short nameTypeIndex;
} method; /* Also used by Fields */
CLASS clazz;
INTERNED_STRING_INSTANCE String;
cell *cache; /* Either clazz or String */
cell integer;
long length;
NameTypeKey nameTypeKey;
NameKey nameKey;
UString ustring;
};
2. 常量池的定义:
注意:这里有多少个数组元素,实际上不确定的,这里只是占位而已,具体使用时是通过分配不同的
内存大小实现可变大小。
struct constantPoolStruct {
union constantPoolEntryStruct entries[1];
};
typedef struct constantPoolStruct* CONSTANTPOOL;
ConstantPool = (CONSTANTPOOL)callocPermanentObject(tableSize);