kernel经典工具函数
一、内存分配
1. 分配可变长结构体大小对应的内存
//linux/overflow.h #define struct_size(p, member, n) \ __ab_c_size(n, sizeof(*(p)->member) + __must_be_array((p)->member), sizeof(*(p)))
示例:
struct swap_info_struct { ... struct plist_node avail_lists[0]; }; alloc_swap_info //swapfile.c struct swap_info_struct *p; p = kvzalloc(struct_size(p, avail_lists, nr_node_ids), GFP_KERNEL);
二、圆整
1. x向y圆整
//include/linux/kernel.h #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) #define round_down(x, y) ((x) & ~__round_mask(x, y))
@x 分别向上和向下圆整的 @y 的整数倍, 其中 @y 必须是2的整数次幂。
示例:
setup_swap_map_and_extents //swapfile.c unsigned long maxpages; round_up(maxpages, SWAPFILE_CLUSTER)
posted on 2026-04-04 16:10 Hello-World3 阅读(1) 评论(0) 收藏 举报
浙公网安备 33010602011771号