源码来自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);//找到未设置的index
kfree(inuse);
return (index >= BR_MAX_PORTS) ? -EXFULL : index;
}