[MOAB-dev] r2531 - MOAB/trunk/tools/iMesh

kraftche at mcs.anl.gov kraftche at mcs.anl.gov
Fri Jan 9 19:34:03 CST 2009


Author: kraftche
Date: 2009-01-09 19:34:03 -0600 (Fri, 09 Jan 2009)
New Revision: 2531

Modified:
   MOAB/trunk/tools/iMesh/iMesh_MOAB.cpp
Log:
o Fix bug in previous checkin: doubling array size is not necessarily 
  sufficient to accomodate all adjacencies for next entity.

o Do not allocate any array space until after we have the adjacencies 
  for the first entity: avoids unnecessarily resizing array for 
  single input entity case.

o Guess initial array size as number of adjacencies for first entity
  times number of entities: is as good a guess as any in general, and
  results in only one allocation in many common cases such as querying
  connectivity of a non-mixed mesh or regions adjacent to faces.



Modified: MOAB/trunk/tools/iMesh/iMesh_MOAB.cpp
===================================================================
--- MOAB/trunk/tools/iMesh/iMesh_MOAB.cpp	2009-01-09 22:48:13 UTC (rev 2530)
+++ MOAB/trunk/tools/iMesh/iMesh_MOAB.cpp	2009-01-10 01:34:03 UTC (rev 2531)
@@ -991,16 +991,17 @@
     const MBEntityHandle *connect;
     int num_connect;
     
-    MBEntityHandle* array = reinterpret_cast<MBEntityHandle*>(*adjacentEntityHandles);
-    int array_alloc = *adjacentEntityHandles_allocated;
-    const bool allocated_array = !array || !array_alloc;
+    MBEntityHandle* array; // ptr to working array of result handles
+    int array_alloc;       // allocated size of 'array'
+    const bool allocated_array = !*adjacentEntityHandles_allocated || !*adjacentEntityHandles;
     if (allocated_array) {
-      array_alloc = 2 * entity_handles_size;
-      array = (MBEntityHandle*)malloc(array_alloc*sizeof(MBEntityHandle));
-      if (!array) {
-        RETURN(iBase_MEMORY_ALLOCATION_FAILED);
-      }
+      array = 0;
+      array_alloc = 0;
     }
+    else {
+      array = reinterpret_cast<MBEntityHandle*>(*adjacentEntityHandles);
+      array_alloc = *adjacentEntityHandles_allocated;
+    }
     
     for ( ; entity_iter != entity_end; ++entity_iter)
     {
@@ -1045,7 +1046,16 @@
         std::copy(connect, connect+num_connect, array+prev_off);
       }
       else if (allocated_array) {
-        array_alloc *= 2;
+          // if array is not allocated yet, guess at initial size
+          // as the number of adjacencies for the first entity times
+          // the number of input entities.  This will result in a single
+          // exact allocation if one input entity or typical queries 
+          // such as connectivity of a non-mixed mesh or regions adjacent
+          // to faces.
+        if (!array_alloc) 
+          array_alloc = entity_handles_size * num_connect;
+        else
+          array_alloc = std::max(array_alloc*2, prev_off+num_connect);  
         array = (MBEntityHandle*)realloc( array, array_alloc*sizeof(MBEntityHandle) );
         if (!array) {
           RETURN(iBase_MEMORY_ALLOCATION_FAILED);




More information about the moab-dev mailing list