static u64 extend_obj_stats(struct zram *zram, u16 gid, int type) { if (!CHECK(zram->zgrp, "zram extend is not enable!\n")) return 0; if (!CHECK_BOUND(gid, 0, zram->zgrp->nr_grp - 1)) return 0; if (type == CACHE_SIZE) return atomic64_read(&zram->zgrp->stats[gid].zram_size); else if (type == CACHE_PAGE) return atomic_read(&zram->zgrp->stats[gid].zram_pages); else if (type == CACHE_FAULT) return atomic64_read(&zram->zgrp->stats[gid].zram_fault); #ifdef CONFIG_ZRAM_EXTEND_WRITEBACK else if (type == SWAP_SIZE) return atomic64_read(&zram->zgrp->stats[gid].wb_size); else if (type == SWAP_PAGE) return atomic_read(&zram->zgrp->stats[gid].wb_pages); else if (type == READ_SIZE) return atomic64_read(&zram->zgrp->stats[gid].read_size); else if (type == WRITE_SIZE) return atomic64_read(&zram->zgrp->stats[gid].write_size); else if (type == SWAP_FAULT) return atomic64_read(&zram->zgrp->stats[gid].wb_fault); BUG(); #endif return 0; } 这个函数做一下流程优化
时间: 2024-02-05 20:11:06 浏览: 106
可以做如下的流程优化:
```c
static u64 extend_obj_stats(struct zram *zram, u16 gid, int type) {
if (!CHECK(zram->zgrp, "zram extend is not enable!\n"))
return 0;
if (!CHECK_BOUND(gid, 0, zram->zgrp->nr_grp - 1))
return 0;
switch (type) {
case CACHE_SIZE:
return atomic64_read(&zram->zgrp->stats[gid].zram_size);
case CACHE_PAGE:
return atomic_read(&zram->zgrp->stats[gid].zram_pages);
case CACHE_FAULT:
return atomic64_read(&zram->zgrp->stats[gid].zram_fault);
#ifdef CONFIG_ZRAM_EXTEND_WRITEBACK
case SWAP_SIZE:
return atomic64_read(&zram->zgrp->stats[gid].wb_size);
case SWAP_PAGE:
return atomic_read(&zram->zgrp->stats[gid].wb_pages);
case READ_SIZE:
return atomic64_read(&zram->zgrp->stats[gid].read_size);
case WRITE_SIZE:
return atomic64_read(&zram->zgrp->stats[gid].write_size);
case SWAP_FAULT:
return atomic64_read(&zram->zgrp->stats[gid].wb_fault);
#endif
default:
break;
}
BUG();
return 0;
}
```
这里使用 `switch-case` 替换了原来的 `if-else` 结构,使代码更加简洁易读。同时,由于 `BUG()` 表示代码中存在错误,因此可以将其放在 `default` 分支中,避免出现无法预料的错误。
阅读全文