MOAB: Mesh Oriented datABase  (version 5.4.1)
file-desc.c File Reference
#include "mhdf.h"
#include "util.h"
#include "status.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <H5Tpublic.h>
#include <H5Dpublic.h>
#include <H5Ppublic.h>
+ Include dependency graph for file-desc.c:

Go to the source code of this file.

Defines

#define FIX_OFFSET(TYPE, FIELD)

Functions

static struct mhdf_FileDescalloc_file_desc (mhdf_Status *status)
static void * realloc_data (struct mhdf_FileDesc **data, size_t append_bytes, mhdf_Status *status, int alignment)
void mhdf_fixFileDesc (struct mhdf_FileDesc *copy_ptr, const struct mhdf_FileDesc *orig_addr)
 Fix nested pointers for copied/moved FileDesc struct.
static struct mhdf_FileDescget_elem_desc (mhdf_FileHandle file_handle, struct mhdf_FileDesc *result, const char *elem_handle, int idx, mhdf_Status *status)
static unsigned get_file_id_size (hid_t file_id_type, mhdf_Status *status)
static struct mhdf_FileDescget_tag_desc (mhdf_FileHandle file_handle, struct mhdf_FileDesc *result, const char *name, int idx, hid_t type, mhdf_Status *status)
static void free_string_list (char **list, int count)
struct mhdf_FileDescmhdf_getFileSummary (mhdf_FileHandle file_handle, hid_t file_id_type, mhdf_Status *status, int extraSetInfo)
 Get summary of data tables contained within file.

Variables

static char buffer [512]

Define Documentation

#define FIX_OFFSET (   TYPE,
  FIELD 
)
Value:
if( copy_ptr->FIELD != NULL ) \
    copy_ptr->FIELD = (TYPE)( ( (char*)( copy_ptr->FIELD ) - (char*)orig_addr ) + (char*)copy_ptr )

Definition at line 73 of file file-desc.c.

Referenced by mhdf_fixFileDesc().


Function Documentation

static struct mhdf_FileDesc * alloc_file_desc ( mhdf_Status status) [static, read]

Definition at line 15 of file file-desc.c.

References mhdf_isError(), mhdf_malloc(), mhdf_FileDesc::offset, and mhdf_FileDesc::total_size.

Referenced by mhdf_getFileSummary().

{
    struct mhdf_FileDesc* result;
    /* allocate a little short of a page */
    result = (struct mhdf_FileDesc*)mhdf_malloc( 4000, status );
    if( mhdf_isError( status ) ) return 0;

    memset( result, 0, sizeof( struct mhdf_FileDesc ) );
    result->total_size = 4000;
    result->offset     = ( (unsigned char*)result ) + sizeof( struct mhdf_FileDesc );
    return result;
}
static void free_string_list ( char **  list,
int  count 
) [static]

Definition at line 377 of file file-desc.c.

Referenced by mhdf_getFileSummary().

{
    int i;
    for( i = 0; i < count; ++i )
        free( list[i] );
    free( list );
}
static struct mhdf_FileDesc* get_elem_desc ( mhdf_FileHandle  file_handle,
struct mhdf_FileDesc result,
const char *  elem_handle,
int  idx,
mhdf_Status status 
) [static, read]

Definition at line 120 of file file-desc.c.

References buffer, mhdf_EntDesc::count, mhdf_EntDesc::dense_tag_indices, mhdf_ElemDesc::desc, mhdf_FileDesc::elems, mhdf_ElemDesc::handle, mhdf_ElemDesc::have_adj, mhdf_closeData(), mhdf_getElemTypeName(), mhdf_haveAdjacency(), mhdf_isError(), mhdf_isPolyElement(), mhdf_openConnectivity(), mhdf_openPolyConnectivity(), mhdf_EntDesc::num_dense_tags, realloc_data(), mhdf_EntDesc::start_id, mhdf_ElemDesc::type, and mhdf_EntDesc::vals_per_ent.

Referenced by mhdf_getFileSummary().

{
    hid_t id_pair[2];
    int poly;
    void* ptr;
    long junk;

    ptr = realloc_data( &result, strlen( elem_handle ) + 1, status, sizeof(char) );
    if( !ptr ) return NULL;
    strcpy( ptr, elem_handle );
    result->elems[idx].handle = ptr;

    mhdf_getElemTypeName( file_handle, elem_handle, buffer, sizeof( buffer ), status );
    if( mhdf_isError( status ) )
    {
        free( result );
        return NULL;
    }

    ptr = realloc_data( &result, strlen( buffer ) + 1, status, sizeof(char) );
    if( !ptr ) return NULL;
    strcpy( ptr, buffer );
    result->elems[idx].type = ptr;

    poly = mhdf_isPolyElement( file_handle, elem_handle, status );
    if( mhdf_isError( status ) )
    {
        free( result );
        return NULL;
    }

    if( !poly )
    {
        id_pair[0] = mhdf_openConnectivity( file_handle, elem_handle, &result->elems[idx].desc.vals_per_ent,
                                            &result->elems[idx].desc.count, &result->elems[idx].desc.start_id, status );
        if( id_pair[0] < 0 )
        {
            free( result );
            return NULL;
        }
        mhdf_closeData( file_handle, id_pair[0], status );
    }
    else
    {
        result->elems[idx].desc.vals_per_ent = -1;
        mhdf_openPolyConnectivity( file_handle, elem_handle, &result->elems[idx].desc.count, &junk,
                                   &result->elems[idx].desc.start_id, id_pair, status );
        if( id_pair[0] < 0 )
        {
            free( result );
            return NULL;
        }
        mhdf_closeData( file_handle, id_pair[0], status );
        mhdf_closeData( file_handle, id_pair[1], status );
    }

    result->elems[idx].desc.dense_tag_indices = NULL;
    result->elems[idx].desc.num_dense_tags    = 0;
    result->elems[idx].have_adj               = mhdf_haveAdjacency( file_handle, result->elems[idx].handle, status );
    if( mhdf_isError( status ) )
    {
        free( result );
        return 0;
    }

    return result;
}
static unsigned get_file_id_size ( hid_t  file_id_type,
mhdf_Status status 
) [static]

Definition at line 192 of file file-desc.c.

References mhdf_setFail().

Referenced by get_tag_desc().

{
    if( H5Tget_class( file_id_type ) != H5T_INTEGER )
    {
        mhdf_setFail( status, "Invalid handle or type class for file ID type." );
        return 0;
    }

    return H5Tget_size( file_id_type );
}
static struct mhdf_FileDesc* get_tag_desc ( mhdf_FileHandle  file_handle,
struct mhdf_FileDesc result,
const char *  name,
int  idx,
hid_t  type,
mhdf_Status status 
) [static, read]

Definition at line 203 of file file-desc.c.

References mhdf_TagDesc::bytes, mhdf_TagDesc::default_value, mhdf_TagDesc::default_value_size, get_file_id_size(), mhdf_TagDesc::global_value, mhdf_TagDesc::global_value_size, mhdf_TagDesc::have_sparse, mhdf_BITFIELD, mhdf_BOOLEAN, mhdf_ENTITY_ID, mhdf_FLOAT, mhdf_getTagInfo(), mhdf_getTagValues(), mhdf_INTEGER, mhdf_isError(), mhdf_OPAQUE, mhdf_setFail(), mhdf_TagDesc::name, realloc_data(), size, mhdf_TagDesc::size, mhdf_TagDesc::storage, mhdf_FileDesc::tags, and mhdf_TagDesc::type.

Referenced by mhdf_getFileSummary().

{
    void* ptr;
    int have_default, have_global;
    int valsize, size, close_type = 0;
    hsize_t array_len;

    ptr = realloc_data( &result, strlen( name ) + 1, status, sizeof(char) );
    if( NULL == ptr ) return NULL;
    strcpy( ptr, name );
    result->tags[idx].name = ptr;

    mhdf_getTagInfo( file_handle, name, &result->tags[idx].type, &result->tags[idx].size, &result->tags[idx].storage,
                     &have_default, &have_global, &result->tags[idx].have_sparse, status );
    if( mhdf_isError( status ) )
    {
        free( result );
        return NULL;
    }

    /* For variable length tags, have_default and have_global will
       contain the size of the respective values.  For fixed-length
       tags, they are either zero or one.  Simplify later code by
       making them contain the size for both cases. */
    valsize = result->tags[idx].size;
    if( result->tags[idx].size >= 0 )
    {
        if( have_default ) have_default = valsize;
        if( have_global ) have_global = valsize;
    }

    result->tags[idx].default_value      = NULL;
    result->tags[idx].default_value_size = have_default;
    result->tags[idx].global_value       = NULL;
    result->tags[idx].global_value_size  = have_global;

    switch( result->tags[idx].type )
    {
        case mhdf_OPAQUE:
            type = 0;
            break;
        case mhdf_BOOLEAN:
            type = H5T_NATIVE_UCHAR;
            break;
        case mhdf_INTEGER:
            type = H5T_NATIVE_INT;
            have_default *= sizeof( int );
            have_global *= sizeof( int );
            valsize *= sizeof( int );
            break;
        case mhdf_FLOAT:
            type = H5T_NATIVE_DOUBLE;
            have_default *= sizeof( double );
            have_global *= sizeof( double );
            valsize *= sizeof( double );
            break;
        case mhdf_BITFIELD:
            have_default = ( have_default + 7 ) / 8;
            have_global  = ( have_global + 7 ) / 8;
            valsize      = ( valsize + 7 ) / 8;
            switch( valsize )
            {
                case 1:
                    type = H5Tcopy( H5T_NATIVE_B8 );
                    break;
                case 2:
                    type = H5Tcopy( H5T_NATIVE_B16 );
                    break;
                case 3:
                case 4:
                    valsize += 4 - valsize;  // to avoid fallthrough warning
                    type = H5Tcopy( H5T_NATIVE_B32 );
                    break;
                case 5:
                case 6:
                case 7:
                case 8:
                    valsize += 8 - valsize;  // to avoid fallthrough warning
                    type = H5Tcopy( H5T_NATIVE_B64 );
                    break;
                default:
                    free( result );
                    mhdf_setFail( status, "Cannot create a bit tag larger than 64-bits.  %d bits requested.\n",
                                  (int)valsize );
                    return NULL;
            }
            close_type = 1;
            break;
        case mhdf_ENTITY_ID:
            if( 0 == type ) type = H5T_NATIVE_ULONG;
            size = get_file_id_size( type, status );
            if( !size )
            {
                free( result );
                return NULL;
            }
            have_default *= size;
            have_global *= size;
            valsize *= size;
            break;
        default:
            mhdf_setFail( status, "Unknown mhdf_TagDataType value (%d) for tag (\"%s\")", (int)result->tags[idx].type,
                          name );
            free( result );
            return NULL;
    }
    result->tags[idx].bytes = valsize;

    if( result->tags[idx].type != mhdf_OPAQUE && result->tags[idx].type != mhdf_BITFIELD && result->tags[idx].size > 1 )
    {
        close_type = 1;
        array_len  = result->tags[idx].size;
#if defined( H5Tarray_create_vers ) && H5Tarray_create_vers > 1
        type = H5Tarray_create2( type, 1, &array_len );
#else
        type = H5Tarray_create( type, 1, &array_len, 0 );
#endif
        if( type < 0 )
        {
            mhdf_setFail( status, "H5Tarray_create failed for tag (\"%s\")", name );
            free( result );
            return NULL;
        }
    }

    if( have_default || have_global )
    {
        if( have_default )
        {
            ptr = realloc_data( &result, have_default, status, sizeof(int) );
            if( NULL == ptr )
            {
                if( close_type )
                {
                    H5Tclose( type );
                }
                return NULL;
            }
            result->tags[idx].default_value = ptr;
        }
        if( have_global )
        {
            ptr = realloc_data( &result, have_global, status, sizeof(int) );
            if( NULL == ptr )
            {
                if( close_type )
                {
                    H5Tclose( type );
                }
                return NULL;
            }
            result->tags[idx].global_value = ptr;
        }
        mhdf_getTagValues( file_handle, name, type, result->tags[idx].default_value, result->tags[idx].global_value,
                           status );
        if( close_type )
        {
            H5Tclose( type );
        }
        if( mhdf_isError( status ) )
        {
            free( result );
            return NULL;
        }
    }

    return result;
}
static void * realloc_data ( struct mhdf_FileDesc **  data,
size_t  append_bytes,
mhdf_Status status,
int  alignment 
) [static]

Definition at line 28 of file file-desc.c.

References mhdf_fixFileDesc(), mhdf_isError(), mhdf_realloc(), mhdf_FileDesc::offset, and mhdf_FileDesc::total_size.

Referenced by get_elem_desc(), get_tag_desc(), and mhdf_getFileSummary().

{
    void* result_ptr;
    struct mhdf_FileDesc* const input_ptr = *data;
    unsigned char* mem_ptr                = (unsigned char*)input_ptr;

    size_t new_size, occupied_size = input_ptr->offset - mem_ptr;

    int append_bytes_padded = append_bytes + alignment - 1;
    /* 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_padded )
    {
        if( append_bytes_padded < input_ptr->total_size )
            new_size = 2 * input_ptr->total_size;
        else
            new_size = input_ptr->total_size + append_bytes_padded;
        *data = (struct mhdf_FileDesc*)mhdf_realloc( *data, new_size, status );
        if( mhdf_isError( status ) ) return 0;

        /* 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 + occupied_size;
        }
        ( *data )->total_size = new_size;
    }

    result_ptr = ( *data )->offset;
    /* need to make this return pointer aligned */
    uintptr_t  addr  = (uintptr_t)(( *data )->offset);
    int pad = addr%alignment;
    if (pad > 0)
    {
        ( *data )->offset += (alignment-pad);
        result_ptr = ( *data )->offset;
    }
    /*printf("new address %p \n", result_ptr);*/
    ( *data )->offset += append_bytes;
    return result_ptr;
}

Variable Documentation

char buffer[512] [static]

Definition at line 13 of file file-desc.c.

Referenced by check_valid_adjacencies(), check_valid_elem_conn(), check_valid_poly_conn(), clock_to_string(), ent_from_handle(), file_exists(), get_elem_desc(), moab::ReadHDF5::get_tagged_entities(), moab::INT_IO_ERROR(), is_acis_bin_file(), is_acis_txt_file(), moab::ReadCGM::is_cubit_file(), is_cubit_file(), moab::ReadCGM::is_facet_file(), moab::ReadCGM::is_iges_file(), is_iges_file(), moab::ReadCGM::is_occ_brep_file(), is_occ_brep_file(), moab::ReadCGM::is_step_file(), is_step_file(), main(), mem_to_string(), moab::mem_to_string(), memory_use(), mhdf_getElemHandles(), mhdf_name_to_path_cat(), mhdf_name_to_path_copy(), moab::mpi_err_str(), moab::WriteHDF5::ExportSet::name(), print_error(), print_iGeom_error(), print_iMesh_error(), print_partitioned_entities(), moab::ErrorOutput::print_real(), moab::DebugOutput::print_real(), moab::ReadHDF5::read_adjacencies(), moab::ReadHDF5::read_elems(), moab::ReadHDF5::read_node_adj_elems(), moab::ReadHDF5::read_nodes(), moab::ReadHDF5VarLen::read_offsets(), moab::ReadHDF5::read_poly(), moab::ReadHDF5::read_set_data(), moab::ReadHDF5::read_tag_values_partial(), moab::ReadHDF5::read_var_len_tag(), moab::ParCommGraph::receive_mesh(), moab::ParCommGraph::receive_tag_values(), moab::ReorderTool::reorder_tag_data(), moab::ReadHDF5::search_tag_values(), moab::ParCommGraph::send_graph_partition(), moab::ParCommGraph::send_mesh_parts(), moab::ParCommGraph::send_tag_values(), moab::ReadHDF5::store_file_ids(), moab::ReadHDF5::store_sets_file_ids(), string_tag_value(), test_string_rank_subst(), tprint(), moab::WriteHDF5::write_adjacencies(), moab::WriteHDF5::write_elems(), moab::WriteHDF5::write_nodes(), moab::WriteHDF5::write_set_data(), moab::WriteHDF5::write_sets(), and moab::WriteHDF5::write_var_len_data().

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines