MOAB: Mesh Oriented datABase  (version 5.2.1)
SequenceData.cpp
Go to the documentation of this file.
00001 #include "SequenceData.hpp"
00002 #include "SysUtil.hpp"
00003 #include "VarLenTag.hpp"
00004 #include <assert.h>
00005 
00006 namespace moab
00007 {
00008 
00009 SequenceData::~SequenceData()
00010 {
00011     for( int i = -numSequenceData; i <= (int)numTagData; ++i )
00012         free( arraySet[i] );
00013     free( arraySet - numSequenceData );
00014 }
00015 
00016 void* SequenceData::create_data( int index, int bytes_per_ent, const void* initial_value )
00017 {
00018     char* array = (char*)malloc( bytes_per_ent * size() );
00019     if( initial_value ) SysUtil::setmem( array, initial_value, bytes_per_ent, size() );
00020 
00021     arraySet[index] = array;
00022     return array;
00023 }
00024 
00025 void* SequenceData::create_sequence_data( int array_num, int bytes_per_ent, const void* initial_value )
00026 {
00027     const int index = -1 - array_num;
00028     assert( array_num < numSequenceData );
00029     assert( !arraySet[index] );
00030     return create_data( index, bytes_per_ent, initial_value );
00031 }
00032 
00033 void* SequenceData::create_custom_data( int array_num, size_t total_bytes )
00034 {
00035     const int index = -1 - array_num;
00036     assert( array_num < numSequenceData );
00037     assert( !arraySet[index] );
00038 
00039     void* array     = malloc( total_bytes );
00040     arraySet[index] = array;
00041     return array;
00042 }
00043 
00044 SequenceData::AdjacencyDataType* SequenceData::allocate_adjacency_data()
00045 {
00046     assert( !arraySet[0] );
00047     const size_t s = sizeof( AdjacencyDataType* ) * size();
00048     arraySet[0]    = malloc( s );
00049     memset( arraySet[0], 0, s );
00050     return reinterpret_cast< AdjacencyDataType* >( arraySet[0] );
00051 }
00052 
00053 void SequenceData::increase_tag_count( unsigned amount )
00054 {
00055     void** list     = arraySet - numSequenceData;
00056     const size_t sz = sizeof( void* ) * ( numSequenceData + numTagData + amount + 1 );
00057     void** new_list = (void**)realloc( list, sz );
00058     if( !new_list )
00059     {
00060         fprintf( stderr, "SequenceData::increase_tag_count(): reallocation of list failed\n" );
00061         // Note: free(list) will be called in the destructor
00062         return;
00063     }
00064     else
00065         list = new_list;
00066     arraySet = list + numSequenceData;
00067     memset( arraySet + numTagData + 1, 0, sizeof( void* ) * amount );
00068     numTagData += amount;
00069 }
00070 
00071 void* SequenceData::allocate_tag_array( int tag_num, int bytes_per_ent, const void* default_value )
00072 {
00073     if( (unsigned)tag_num >= numTagData ) increase_tag_count( tag_num - numTagData + 1 );
00074 
00075     assert( !arraySet[tag_num + 1] );
00076     return create_data( tag_num + 1, bytes_per_ent, default_value );
00077 }
00078 
00079 SequenceData* SequenceData::subset( EntityHandle start, EntityHandle end, const int* sequence_data_sizes ) const
00080 {
00081     return new SequenceData( this, start, end, sequence_data_sizes );
00082 }
00083 
00084 SequenceData::SequenceData( const SequenceData* from, EntityHandle start, EntityHandle end,
00085                             const int* sequence_data_sizes )
00086     : numSequenceData( from->numSequenceData ), numTagData( from->numTagData ), startHandle( start ), endHandle( end )
00087 {
00088     assert( start <= end );
00089     assert( from != 0 );
00090     assert( from->start_handle() <= start );
00091     assert( from->end_handle() >= end );
00092 
00093     void** array        = (void**)malloc( sizeof( void* ) * ( numSequenceData + numTagData + 1 ) );
00094     arraySet            = array + numSequenceData;
00095     const size_t offset = start - from->start_handle();
00096     const size_t count  = end - start + 1;
00097 
00098     for( int i = 0; i < numSequenceData; ++i )
00099         copy_data_subset( -1 - i, sequence_data_sizes[i], from->get_sequence_data( i ), offset, count );
00100     copy_data_subset( 0, sizeof( AdjacencyDataType* ), from->get_adjacency_data(), offset, count );
00101     for( unsigned i = 1; i <= numTagData; ++i )
00102         arraySet[i] = 0;
00103 }
00104 
00105 void SequenceData::copy_data_subset( int index, int size_per_ent, const void* source, size_t offset, size_t count )
00106 {
00107     if( !source )
00108         arraySet[index] = 0;
00109     else
00110     {
00111         arraySet[index] = malloc( count * size_per_ent );
00112         memcpy( arraySet[index], (const char*)source + offset * size_per_ent, count * size_per_ent );
00113     }
00114 }
00115 
00116 void SequenceData::move_tag_data( SequenceData* destination, const int* tag_sizes, int num_tag_sizes )
00117 {
00118     assert( destination->start_handle() >= start_handle() );
00119     assert( destination->end_handle() <= end_handle() );
00120     const size_t offset = destination->start_handle() - start_handle();
00121     const size_t count  = destination->size();
00122     if( destination->numTagData < numTagData ) destination->increase_tag_count( numTagData - destination->numTagData );
00123 
00124     for( unsigned i = 1; i <= numTagData; ++i )
00125     {
00126         if( !arraySet[i] ) continue;
00127 
00128         assert( i <= (unsigned)num_tag_sizes );
00129         if( num_tag_sizes ) {}  // empty line to prevent compiler warning
00130 
00131         const int tag_size = tag_sizes[i - 1];
00132         if( !destination->arraySet[i] ) destination->arraySet[i] = malloc( count * tag_size );
00133         memcpy( destination->arraySet[i], reinterpret_cast< char* >( arraySet[i] ) + offset * tag_size,
00134                 count * tag_size );
00135     }
00136 }
00137 
00138 void SequenceData::release_tag_data( const int* tag_sizes, int num_tag_sizes )
00139 {
00140     assert( num_tag_sizes >= (int)numTagData );
00141     if( num_tag_sizes ) {}  // empty line to prevent compiler warning
00142     for( unsigned i = 0; i < numTagData; ++i )
00143         release_tag_data( i, tag_sizes[i] );
00144 }
00145 
00146 void SequenceData::release_tag_data( int tag_num, int tag_size )
00147 {
00148     if( (unsigned)tag_num < numTagData )
00149     {
00150         if( tag_size == MB_VARIABLE_LENGTH && arraySet[tag_num + 1] )
00151         {
00152             VarLenTag* iter      = reinterpret_cast< VarLenTag* >( arraySet[tag_num + 1] );
00153             VarLenTag* const end = iter + size();
00154             for( ; iter != end; ++iter )
00155                 iter->clear();
00156         }
00157         free( arraySet[tag_num + 1] );
00158         arraySet[tag_num + 1] = 0;
00159     }
00160 }
00161 
00162 }  // namespace moab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines