抽象出这个过程是否值得?

时间:2013-08-04 04:43:41

标签: c serialization memory-management macros bytearray

我有以下内存布局:

typedef struct map_obj_s
{
    thing_t**       things;

    linedef_t**     linedefs;

    sidedef_t**     sidedefs;

    vertex_t**      vertices;

    segment_t**     segments;

    ssector_t**     subsectors;

    node_t*         node_tree;

    sector_t**      sectors;

    int32_t         lump_counts[ MAP_LUMP_COUNT ];
}
map_obj_t;

问题在于,我基本上为每种数据类型重复完全相同的过程,这里除了node_tree和lump_counts成员之外。

以下是重复的结果:

map_obj_t* Map_Read( lumpbuffer_t* map_lump )
{
    int32_t lump_counts[ MAP_LUMP_COUNT ];

    __GetLumpCounts( map_lump, lump_counts );

    // laziness
    const lumpinfo_t* const mlumps = map_lump->lumps;

    FILE* mapfile   = Wad_GetFilePtr();

    map_obj_t* map  = Mem_Alloc( 1, sizeof( map_obj_t ) );

    // allocate buffers

    map->things     = Mem_Alloc( lump_counts[ LUMP_THINGS ],   sizeof( thing_t* ) );
    map->linedefs   = Mem_Alloc( lump_counts[ LUMP_LINEDEFS ], sizeof( linedef_t* ) );
    map->sidedefs   = Mem_Alloc( lump_counts[ LUMP_SIDEDEFS ], sizeof( sidedef_t* ) );
    map->vertices   = Mem_Alloc( lump_counts[ LUMP_VERTICES ], sizeof( vertex_t* ) );
    map->segments   = Mem_Alloc( lump_counts[ LUMP_SEGMENTS ], sizeof( segment_t* ) );
    map->subsectors = Mem_Alloc( lump_counts[ LUMP_SSECTORS ], sizeof( ssector_t* ) );
    map->node_tree  = Mem_Alloc( lump_counts[ LUMP_NODES ], sizeof( node_t ) );
    map->sectors    = Mem_Alloc( lump_counts[ LUMP_SECTORS ], sizeof( sector_t* ) );

    // parse things
    PARSE_LUMP( mapfile,
                map->things,
                sizeof( thing_t ),
                lump_counts[ LUMP_THINGS ],
                mlumps,
                LUMP_THINGS );

    // parse linedefs
    PARSE_LUMP( mapfile,
                map->linedefs,
                sizeof( linedef_t ),
                lump_counts[ LUMP_LINEDEFS ],
                mlumps,
                LUMP_LINEDEFS );


    // parse sidedefs
    PARSE_LUMP( mapfile,
                map->sidedefs,
                sizeof( sidedef_t ),
                lump_counts[ LUMP_SIDEDEFS ],
                mlumps,
                LUMP_SIDEDEFS );

    // parse vertices
    PARSE_LUMP( mapfile,
                map->vertices,
                sizeof( vertex_t ),
                lump_counts[ LUMP_VERTICES ],
                mlumps,
                LUMP_VERTICES );

    // parse segments
    PARSE_LUMP( mapfile,
                map->segments,
                sizeof( vertex_t ),
                lump_counts[ LUMP_SEGMENTS ],
                mlumps,
                LUMP_SEGMENTS );


    // parse subsectors
    PARSE_LUMP( mapfile,
                map->subsectors,
                sizeof( ssector_t ),
                lump_counts[ LUMP_SSECTORS ],
                mlumps,
                LUMP_SSECTORS );

    // parse nodes
    PARSE_LUMP( mapfile,
                map->node_tree,
                sizeof( node_t ),
                lump_counts[ LUMP_NODES ],
                mlumps,
                LUMP_NODES );


    // parse sectors
    PARSE_LUMP( mapfile,
                map->sectors,
                sizeof( sector_t ),
                lump_counts[ LUMP_SECTORS ],
                mlumps,
                LUMP_SECTORS );

    memcpy( map->lump_counts, lump_counts, sizeof( int32_t ) * MAP_LUMP_COUNT );

    return map;
}

PARSE_LUMP宏:

#define PARSE_LUMP( wad_fileptr, data, data_size, count, lumps_ptr, lump_type ) \
    do {                                                                        \
                                                                                \
        Mem_AllocBuffer( ( generic_buffer_t ) ( data ), ( data_size ), ( count ) ); \
                                                                                \
        fseek( ( wad_fileptr ),                                                 \
               ( lumps_ptr )[ ( lump_type ) ].address_offset,                   \
               SEEK_SET );                                                      \
                                                                                \
        for ( int32_t i = 0; i < count; ++i )                                   \
        {                                                                       \
            fread( ( data )[ i ], ( data_size ), 1, ( wad_fileptr ) );          \
        }                                                                       \
                                                                                \
    } while( 0 )                                                                \

The Point

我想抽象出来是不对的?它是可读的,当然,但它的想法包含很多代码。我不是一个很棒的C程序员(这是我的第一个真正/认真的项目),但我有使用C ++的经验。在C ++方面,使用模板很容易,但在C中我只限于void*和宏函数。序列化似乎是一种可能性,但所有这些问题似乎都指向了我的缓冲区有指针指针的事实。 是否有任何关于此问题的感觉,或者我只是浪费我的时间甚至打扰它?更不用说我甚至确定如何从序列化结构动态分配内存。

1 个答案:

答案 0 :(得分:2)

我猜你有什么工作,但我认为没有理由分配,然后分别阅读每一组肿块,如果你事先知道肿块的数量和它们的大小,那么你可以分配你需要的所有内存和一次读取整个文件,然后你只需将每个指针设置为各自的一组块的开始(偏移),如下所示:

//first set of lumps
map->things = map_data;
//increment pointer
map_data += lump_counts[ LUMP_THINGS ] * sizeof( thing_t );

//second set of lumps
map->linedefs = map_data;
map_data += lump_counts[ LUMP_LINEDEFS ] * sizeof( linedefs_t );
...