ConstantPool::allocate记录

常量池的开辟方法

ConstantPool* cp_oop = ConstantPool::allocate(loader_data, cp_length, CHECK_(empty));

ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
  Array* tags = MetadataFactory::new_writeable_array(loader_data, length, 0, CHECK_NULL);

  int size = ConstantPool::size(length);
  return new (loader_data, size, false, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
}

此处的new为运算符重载查看父类new的重载方法

void* MetaspaceObj::operator new(size_t size, ClassLoaderData* loader_data,
                                 size_t word_size, bool read_only,
                                 MetaspaceObj::Type type, TRAPS) throw() {
  // Klass has it's own operator new
  return Metaspace::allocate(loader_data, word_size, read_only,
                             type, CHECK_NULL);
}

刚开始看的时候,反复对了参数,看这个重载方法怎么多个size参数,查看资料才明白这个new的关键字的函数原型为operator new(size_t size);这个size为sizeof(类)计算出来类的大小,这里c++默认给你做了处理,至此对new运算符更新了认识,其他初学者也会犯这种低级的错误,原因在于没有仔细了解过new的运算符重载。

你可能感兴趣的:(jvm)