[MOAB-dev] r3706 - in MOAB/trunk: src/io/mhdf/src test/h5file

kraftche at cae.wisc.edu kraftche at cae.wisc.edu
Wed Mar 24 15:06:48 CDT 2010


Author: kraftche
Date: 2010-03-24 15:06:48 -0500 (Wed, 24 Mar 2010)
New Revision: 3706

Modified:
   MOAB/trunk/src/io/mhdf/src/file-desc.c
   MOAB/trunk/test/h5file/h5regression.cpp
Log:
fix bug reading HDF5 files with many tags, and add regression test for bug

Modified: MOAB/trunk/src/io/mhdf/src/file-desc.c
===================================================================
--- MOAB/trunk/src/io/mhdf/src/file-desc.c	2010-03-24 19:31:21 UTC (rev 3705)
+++ MOAB/trunk/src/io/mhdf/src/file-desc.c	2010-03-24 20:06:48 UTC (rev 3706)
@@ -29,8 +29,13 @@
   void* result_ptr;
   struct mhdf_FileDesc* const input_ptr = *data;
   unsigned char* mem_ptr = (unsigned char*)input_ptr;
-  size_t new_size, orig_size = input_ptr->offset - mem_ptr;
+  size_t new_size, occupied_size = input_ptr->offset - mem_ptr;
   
+  /* input_ptr->offset - input_ptr == currently occupied size
+     input_ptr->total_size         == currently allocated size
+   */
+  
+  /* if the end of the allocated space is before the end of the required space */
   if (mem_ptr + input_ptr->total_size < input_ptr->offset + append_bytes) {
     if (append_bytes < input_ptr->total_size)
       new_size = 2 * input_ptr->total_size;
@@ -40,11 +45,15 @@
     if (mhdf_isError( status ))
       return 0;
 
-    if (*data != input_ptr)
+    /* if realloc moved us to a different location in memory,
+     * we need to update all of the internal pointers to
+     * new locations relative to the start of the struct */
+    if (*data != input_ptr) {
       mhdf_fixFileDesc( *data, input_ptr );
-      
-    mem_ptr = (unsigned char*)(*data);
-    (*data)->offset = mem_ptr + orig_size;
+      mem_ptr = (unsigned char*)(*data);
+      (*data)->offset = mem_ptr + occupied_size;
+    }
+    (*data)->total_size = new_size;
   }
   
   result_ptr = (*data)->offset;
@@ -53,7 +62,7 @@
 }
 
 #define FIX_OFFSET( TYPE, FIELD ) \
-  copy_ptr->FIELD = (TYPE)( ((char*)(copy_ptr->FIELD) - (char*)orig_addr) + (char*)copy_ptr )  
+  if (copy_ptr->FIELD != NULL) copy_ptr->FIELD = (TYPE)( ((char*)(copy_ptr->FIELD) - (char*)orig_addr) + (char*)copy_ptr )  
 
   
 void 
@@ -467,10 +476,9 @@
     free( result );
     return NULL;
   }
-  result->num_tag_desc = num_tag_names;
   
     /* allocate array of tag descriptors */
-  size = result->num_tag_desc * sizeof(struct mhdf_TagDesc);
+  size = num_tag_names * sizeof(struct mhdf_TagDesc);
   ptr = realloc_data( &result, size, status );
   if (NULL == ptr) {
     free( elem_handles );
@@ -479,6 +487,8 @@
   }
   memset( ptr, 0, size );
   result->tags = ptr;
+  result->num_tag_desc = num_tag_names;
+  memset( result->tags, 0, size );
   
     /* Initialize each tag descriptor */
   for (i = 0; i < result->num_tag_desc; ++i) {

Modified: MOAB/trunk/test/h5file/h5regression.cpp
===================================================================
--- MOAB/trunk/test/h5file/h5regression.cpp	2010-03-24 19:31:21 UTC (rev 3705)
+++ MOAB/trunk/test/h5file/h5regression.cpp	2010-03-24 20:06:48 UTC (rev 3706)
@@ -7,6 +7,7 @@
 
 #include <algorithm>
 #include <iostream>
+#include <sstream>
 #include <stdlib.h>
 #include <math.h>
 
@@ -15,11 +16,13 @@
 const char filename[] = "bad.h5m";
   
 void test_write_invalid_elem();
+void test_write_read_many_tags();
 
 int main(int argc, char* argv[])
 {
   int exitval = 0;
   exitval += RUN_TEST( test_write_invalid_elem );
+  exitval += RUN_TEST( test_write_read_many_tags );
   return exitval;
 }
 
@@ -63,4 +66,78 @@
   CHECK(MB_SUCCESS != rval);
 }
 
+void test_write_read_many_tags()
+{
+  const int N = 200;
+  Core mbcore;
+  Interface& mb = mbcore;
+  ErrorCode rval;
   
+  double coords[3] = { 0, 0, 0 };
+  EntityHandle node;
+  rval = mb.create_vertex( coords, node );
+  CHECK_ERR(rval);
+  
+    // create a lot of tags
+  std::vector<Tag> tags;
+  for (int i = 0; i < N; ++i) {
+    Tag t;
+    std::ostringstream name("IntTag");
+    name << i;
+    rval = mb.tag_create( name.str().c_str(),
+                          sizeof(int),
+                          i % 2 ? MB_TAG_SPARSE : MB_TAG_DENSE,
+                          MB_TYPE_INTEGER,
+                          t,
+                          &i );
+    CHECK_ERR(rval);
+    tags.push_back(t);
+  }
+  
+    // write the file
+  rval = mb.write_file( filename, "MOAB" );
+  CHECK_ERR(rval);
+  
+    // clear moab instance
+  rval = mb.delete_mesh();
+  CHECK_ERR(rval);
+  for (int i = 0; i < N; ++i) {
+    rval = mb.tag_delete( tags[i] );
+    CHECK_ERR(rval);
+  }
+  
+  
+    // read the file
+  rval = mb.load_file( filename );
+  CHECK_ERR(rval);
+  remove(filename);
+  
+    // check that we have the expected tags
+  for (int i = 0; i < N; ++i) {
+    Tag t;
+    std::ostringstream name("IntTag");
+    name << i;
+    rval = mb.tag_get_handle( name.str().c_str(), t );
+    CHECK_ERR(rval);
+    
+    TagType storage;
+    rval = mb.tag_get_type( t, storage );
+    CHECK_ERR(rval);
+    CHECK_EQUAL( (i%2)?MB_TAG_SPARSE:MB_TAG_DENSE, storage );
+    
+    DataType type;
+    rval = mb.tag_get_data_type( t, type );
+    CHECK_ERR(rval);
+    CHECK_EQUAL( MB_TYPE_INTEGER, type );
+    
+    int size;
+    rval = mb.tag_get_size( t, size );
+    CHECK_ERR(rval);
+    CHECK_EQUAL( (int)sizeof(int), size );
+    
+    int def;
+    rval = mb.tag_get_default_value( t, &def );
+    CHECK_ERR(rval);
+    CHECK_EQUAL( i, def );
+  }
+}  



More information about the moab-dev mailing list