3

我有以下内存布局:

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 )                                                                \

要点

我想把它抽象出来有错吗?当然,它是可读的,但它的想法由大量代码组成。我不是一个很棒的 C 程序员(这是我第一个真正/认真的项目),但我有 C++ 的经验。在 C++ 方面,使用模板很容易,但在 C 中,我仅限于void*宏函数。序列化似乎是一种可能性,但所有这些问题似乎都指向这样一个事实,即我的缓冲区有指向指针的指针。这样做有什么意义吗,还是我只是在浪费时间甚至为此烦恼?更不用说我什至确定如何从序列化结构中动态分配内存。

4

1 回答 1

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 );
...
于 2013-08-04T05:13:11.897 回答