我是C的新手。 我想写一个通用的C代码。
以下是现有代码的示例:
typedef struct mem_mapped_struct1 {
...
...
...
} mem_mapped_struct1;
boolean call_do_upgrade(int arg)
{
do_upgrade1(arg);
}
boolean do_upgrade1(int arg) {
mem_mapped_struct1 *mmap1 = func_get_mmap_addr1(arg);
...
/* Some operations on mmap1 by accessing it's elements */
...
}
现在要升级其他一些设备,我引入了一个新的mem映射结构(mem_mapped_struct2),它有不同的元素和一个新的函数来获取地址(func_get_mmap_addr2)。所以我的问题是,我是否需要编写一个单独的函数来使用新结构进行升级,还是应该在相同的函数中编写代码来处理upgarde?升级的步骤是相同的。但结构和要素不同。
方法1:
boolean call_do_upgrade(int arg)
{
do_upgrade1(arg);
}
boolean do_upgrade1(int arg)
{
mem_mapped_struct1 *mmap1 = NULL;
mem_mapped_struct1 *mmap2 = NULL;
if (arg == 1)
mmap1 = func_to_get_mmap_addr1(arg);
else
mmap2 = func_to_get_mmap_addr2(arg);
...
if (arg == 1)
/* Some operations on mmap1 by accessing it's elements */
else
/* Same operations on mmap2 by accessing it's elements */
...
}
方法2:
boolean call_do_upgrade(int arg)
{
if (arg == 1)
do_upgrade1(arg);
else
do_upgrade2(arg);
}
boolean do_upgrade1(int arg)
{
mem_mapped_struct1 *mmap1 = NULL;
mmap1 = func_to_get_mmap_addr1(arg);
...
/* Some operations on mmap1 by accessing it's elements */
...
}
boolean do_upgrade2(int arg) {
mem_mapped_struct2 *mmap2 = NULL;
mmap2 = func_to_get_mmap_addr2(arg);
...
/* Some operations on mmap2 by accessing it's elements */
...
}
请建议哪种方法更好? 或者请建议更好地解决这个问题。