找到一个可用的数字

来源:互联网 发布:印度种姓制度现状知乎 编辑:程序博客网 时间:2024/06/07 10:11

源码来自Linux 桥模块

/* find an available port number */static int find_portno(struct net_bridge *br){int index;struct net_bridge_port *p;unsigned long *inuse;inuse = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),GFP_KERNEL);if (!inuse)return -ENOMEM;set_bit(0, inuse);/* zero is reserved */list_for_each_entry(p, &br->port_list, list) {set_bit(p->port_no, inuse);//当前可用的端口号进行位设置}index = find_first_zero_bit(inuse, BR_MAX_PORTS);//找到未设置的indexkfree(inuse);return (index >= BR_MAX_PORTS) ? -EXFULL : index;}


0 0