< prev index next >

src/hotspot/share/memory/filemap.cpp

Print this page

        

@@ -157,12 +157,12 @@
   assert(_current_info == NULL, "must be singleton"); // not thread safe
   _current_info = this;
   memset((void*)this, 0, sizeof(FileMapInfo));
   _file_offset = 0;
   _file_open = false;
-  _header = new FileMapHeader();
-  _header->_version = _invalid_version;
+  _header = (FileMapHeader*)os::malloc(sizeof(FileMapHeader), mtInternal);
+  _header->_version = INVALID_CDS_ARCHIVE_VERSION;
   _header->_has_platform_or_app_classes = true;
 }
 
 FileMapInfo::~FileMapInfo() {
   assert(_current_info == this, "must be singleton"); // not thread safe

@@ -171,13 +171,13 @@
 
 void FileMapInfo::populate_header(size_t alignment) {
   _header->populate(this, alignment);
 }
 
-void FileMapInfo::FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
-  _magic = 0xf00baba2;
-  _version = _current_version;
+void FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
+  _magic = CDS_ARCHIVE_MAGIC;
+  _version = CURRENT_CDS_ARCHIVE_VERSION;
   _alignment = alignment;
   _obj_alignment = ObjectAlignmentInBytes;
   _compact_strings = CompactStrings;
   _narrow_oop_mode = Universe::narrow_oop_mode();
   _narrow_oop_base = Universe::narrow_oop_base();

@@ -491,18 +491,17 @@
 }
 
 // Read the FileMapInfo information from the file.
 
 bool FileMapInfo::init_from_file(int fd) {
-  size_t sz = _header->data_size();
-  char* addr = _header->data();
-  size_t n = os::read(fd, addr, (unsigned int)sz);
+  size_t sz = sizeof(FileMapHeader);
+  size_t n = os::read(fd, _header, (unsigned int)sz);
   if (n != sz) {
     fail_continue("Unable to read the file header.");
     return false;
   }
-  if (_header->_version != current_version()) {
+  if (_header->_version != CURRENT_CDS_ARCHIVE_VERSION) {
     fail_continue("The shared archive file has the wrong version.");
     return false;
   }
   _file_offset = (long)n;
 

@@ -519,12 +518,11 @@
     _paths_misc_info = NULL;
     return false;
   }
 
   size_t len = lseek(fd, 0, SEEK_END);
-  struct FileMapInfo::FileMapHeader::space_info* si =
-    &_header->_space[MetaspaceShared::last_valid_region];
+  CDSFileMapRegion* si = space_at(MetaspaceShared::last_valid_region);
   // The last space might be empty
   if (si->_file_offset > len || len - si->_file_offset < si->_used) {
     fail_continue("The shared archive file has been truncated.");
     return false;
   }

@@ -589,23 +587,21 @@
   int info_size = ClassLoader::get_shared_paths_misc_info_size();
 
   _header->_paths_misc_info_size = info_size;
 
   align_file_position();
-  size_t sz = _header->data_size();
-  char* addr = _header->data();
-  write_bytes(addr, (int)sz); // skip the C++ vtable
-  write_bytes(ClassLoader::get_shared_paths_misc_info(), info_size);
+  write_bytes(_header, sizeof(FileMapHeader));
+  write_bytes(ClassLoader::get_shared_paths_misc_info(), (size_t)info_size);
   align_file_position();
 }
 
 
 // Dump region to file.
 
 void FileMapInfo::write_region(int region, char* base, size_t size,
                                bool read_only, bool allow_exec) {
-  struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[region];
+  CDSFileMapRegion* si = space_at(region);
 
   if (_file_open) {
     guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
     log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08)
                   " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08),

@@ -626,11 +622,11 @@
   si->_used = size;
   si->_read_only = read_only;
   si->_allow_exec = allow_exec;
   si->_crc = ClassLoader::crc32(0, base, (jint)size);
   if (base != NULL) {
-    write_bytes_aligned(base, (int)size);
+    write_bytes_aligned(base, size);
   }
 }
 
 // Write out the given archive heap memory regions.  GC code combines multiple
 // consecutive archive GC regions into one MemRegion whenever possible and

@@ -692,13 +688,13 @@
   return total_size;
 }
 
 // Dump bytes to file -- at the current file position.
 
-void FileMapInfo::write_bytes(const void* buffer, int nbytes) {
+void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) {
   if (_file_open) {
-    int n = ::write(_fd, buffer, nbytes);
+    size_t n = os::write(_fd, buffer, (unsigned int)nbytes);
     if (n != nbytes) {
       // It is dangerous to leave the corrupted shared archive file around,
       // close and remove the file. See bug 6372906.
       close();
       remove(_full_path);

@@ -730,11 +726,11 @@
 }
 
 
 // Dump bytes to file -- at the current file position.
 
-void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) {
+void FileMapInfo::write_bytes_aligned(const void* buffer, size_t nbytes) {
   align_file_position();
   write_bytes(buffer, nbytes);
   align_file_position();
 }
 

@@ -754,11 +750,11 @@
 
 // JVM/TI RedefineClasses() support:
 // Remap the shared readonly space to shared readwrite, private.
 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
   int idx = MetaspaceShared::ro;
-  struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[idx];
+  CDSFileMapRegion* si = space_at(idx);
   if (!si->_read_only) {
     // the space is already readwrite so we are done
     return true;
   }
   size_t used = si->_used;

@@ -806,11 +802,11 @@
 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", "OptionalData",
                                             "String1", "String2", "OpenArchive1", "OpenArchive2" };
 
 char* FileMapInfo::map_region(int i, char** top_ret) {
   assert(!MetaspaceShared::is_heap_region(i), "sanity");
-  struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
+  CDSFileMapRegion* si = space_at(i);
   size_t used = si->_used;
   size_t alignment = os::vm_allocation_granularity();
   size_t size = align_up(used, alignment);
   char *requested_addr = _header->region_addr(i);
 

@@ -873,11 +869,11 @@
     if (narrow_oop_mode() != Universe::narrow_oop_mode() ||
         narrow_oop_base() != Universe::narrow_oop_base() ||
         narrow_oop_shift() != Universe::narrow_oop_shift() ||
         narrow_klass_base() != Universe::narrow_klass_base() ||
         narrow_klass_shift() != Universe::narrow_klass_shift()) {
-      if (log_is_enabled(Info, cds) && _header->_space[MetaspaceShared::first_string]._used > 0) {
+      if (log_is_enabled(Info, cds) && space_at(MetaspaceShared::first_string)->_used > 0) {
         log_info(cds)("Cached heap data from the CDS archive is being ignored. "
                       "The current CompressedOops/CompressedClassPointers encoding differs from "
                       "that archived due to heap size change. The archive was dumped using max heap "
                       "size " UINTX_FORMAT "M.", max_heap_size()/M);
         log_info(cds)("Current narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",

@@ -904,11 +900,11 @@
           MetaspaceShared::set_open_archive_heap_region_mapped();
         }
       }
     }
   } else {
-    if (log_is_enabled(Info, cds) && _header->_space[MetaspaceShared::first_string]._used > 0) {
+    if (log_is_enabled(Info, cds) && space_at(MetaspaceShared::first_string)->_used > 0) {
       log_info(cds)("Cached heap data from the CDS archive is being ignored. UseG1GC, "
                     "UseCompressedOops and UseCompressedClassPointers are required.");
     }
   }
 

@@ -922,16 +918,16 @@
 }
 
 bool FileMapInfo::map_heap_data(MemRegion **heap_mem, int first,
                                 int max, int* num, bool is_open_archive) {
   MemRegion * regions = new MemRegion[max];
-  struct FileMapInfo::FileMapHeader::space_info* si;
+  CDSFileMapRegion* si;
   int region_num = 0;
 
   for (int i = first;
            i < first + max; i++) {
-    si = &_header->_space[i];
+    si = space_at(i);
     size_t used = si->_used;
     if (used > 0) {
       size_t size = used;
       char* requested_addr = (char*)((void*)CompressedOops::decode_not_null(
                                             (narrowOop)si->_addr._offset));

@@ -961,11 +957,11 @@
 
   // Map the archived heap data. No need to call MemTracker::record_virtual_memory_type()
   // for mapped regions as they are part of the reserved java heap, which is
   // already recorded.
   for (int i = 0; i < region_num; i++) {
-    si = &_header->_space[first + i];
+    si = space_at(first + i);
     char* addr = (char*)regions[i].start();
     char* base = os::map_memory(_fd, _full_path, si->_file_offset,
                                 addr, regions[i].byte_size(), si->_read_only,
                                 si->_allow_exec);
     if (base == NULL || base != addr) {

@@ -1028,11 +1024,11 @@
   assert(i >= 0 && i < MetaspaceShared::n_regions, "invalid region");
   if (!VerifySharedSpaces) {
     return true;
   }
 
-  size_t sz = _header->_space[i]._used;
+  size_t sz = space_at(i)->_used;
 
   if (sz == 0) {
     return true; // no data
   }
   if ((MetaspaceShared::is_string_region(i) &&

@@ -1041,22 +1037,22 @@
        !MetaspaceShared::open_archive_heap_region_mapped())) {
     return true; // archived heap data is not mapped
   }
   const char* buf = _header->region_addr(i);
   int crc = ClassLoader::crc32(0, buf, (jint)sz);
-  if (crc != _header->_space[i]._crc) {
+  if (crc != space_at(i)->_crc) {
     fail_continue("Checksum verification failed.");
     return false;
   }
   return true;
 }
 
 // Unmap a memory region in the address space.
 
 void FileMapInfo::unmap_region(int i) {
   assert(!MetaspaceShared::is_heap_region(i), "sanity");
-  struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
+  CDSFileMapRegion* si = space_at(i);
   size_t used = si->_used;
   size_t size = align_up(used, os::vm_allocation_granularity());
 
   if (used == 0) {
     return;

@@ -1110,45 +1106,45 @@
     return false;
   }
   return true;
 }
 
-char* FileMapInfo::FileMapHeader::region_addr(int idx) {
+char* FileMapHeader::region_addr(int idx) {
   if (MetaspaceShared::is_heap_region(idx)) {
     return _space[idx]._used > 0 ?
              (char*)((void*)CompressedOops::decode_not_null((narrowOop)_space[idx]._addr._offset)) : NULL;
   } else {
     return _space[idx]._addr._base;
   }
 }
 
-int FileMapInfo::FileMapHeader::compute_crc() {
-  char* header = data();
+int FileMapHeader::compute_crc() {
+  char* start = (char*)this;
   // start computing from the field after _crc
-  char* buf = (char*)&_crc + sizeof(int);
-  size_t sz = data_size() - (buf - header);
+  char* buf = (char*)&_crc + sizeof(_crc);
+  size_t sz = sizeof(FileMapHeader) - (buf - start);
   int crc = ClassLoader::crc32(0, buf, (jint)sz);
   return crc;
 }
 
 // This function should only be called during run time with UseSharedSpaces enabled.
-bool FileMapInfo::FileMapHeader::validate() {
+bool FileMapHeader::validate() {
   if (VerifySharedSpaces && compute_crc() != _crc) {
-    fail_continue("Header checksum verification failed.");
+    FileMapInfo::fail_continue("Header checksum verification failed.");
     return false;
   }
 
   if (!Arguments::has_jimage()) {
     FileMapInfo::fail_continue("The shared archive file cannot be used with an exploded module build.");
     return false;
   }
 
-  if (_version != current_version()) {
+  if (_version != CURRENT_CDS_ARCHIVE_VERSION) {
     FileMapInfo::fail_continue("The shared archive file is the wrong version.");
     return false;
   }
-  if (_magic != (int)0xf00baba2) {
+  if (_magic != CDS_ARCHIVE_MAGIC) {
     FileMapInfo::fail_continue("The shared archive file has a bad magic number.");
     return false;
   }
   char header_version[JVM_IDENT_MAX];
   get_header_version(header_version);

@@ -1220,20 +1216,20 @@
   assert(idx == MetaspaceShared::ro ||
          idx == MetaspaceShared::rw ||
          idx == MetaspaceShared::mc ||
          idx == MetaspaceShared::md, "invalid region index");
   char* base = _header->region_addr(idx);
-  if (p >= base && p < base + _header->_space[idx]._used) {
+  if (p >= base && p < base + space_at(idx)->_used) {
     return true;
   }
   return false;
 }
 
 void FileMapInfo::print_shared_spaces() {
   tty->print_cr("Shared Spaces:");
   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
-    struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
+    CDSFileMapRegion* si = space_at(i);
     char *base = _header->region_addr(i);
     tty->print("  %s " INTPTR_FORMAT "-" INTPTR_FORMAT,
                         shared_region_name[i],
                         p2i(base), p2i(base + si->_used));
   }

@@ -1248,11 +1244,11 @@
     map_info->fail_continue("%s", msg);
     for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) {
       char *addr = map_info->_header->region_addr(i);
       if (addr != NULL && !MetaspaceShared::is_heap_region(i)) {
         map_info->unmap_region(i);
-        map_info->_header->_space[i]._addr._base = NULL;
+        map_info->space_at(i)->_addr._base = NULL;
       }
     }
     // Dealloc the archive heap regions only without unmapping. The regions are part
     // of the java heap. Unmapping of the heap regions are managed by GC.
     map_info->dealloc_archive_heap_regions(open_archive_heap_ranges,
< prev index next >