循环队列在libnet中的实现

libnet 是一个小型的接口函数库,主要用C语言写成,提供了低层网络数据报的构造、处理和发送功能(百度百科上有很详细的介绍哦)。

貌似我下载的版本与网上教程说的不太一致。我在sf上下载的最新版,但是却是0.10.11版——网上有说1.0版的,我到底该信谁的呢!

今天无意中看到该库的循环队列的实现,感觉真是简洁啊!有木有啊!!!

/* Queues. */
#define QUEUE_SIZE 2048

struct queue {
unsigned
char data[QUEUE_SIZE];
int head, tail;
};

#define queue_wrap(x) ((x) & (QUEUE_SIZE - 1))
#define queue_full(p) (queue_wrap (p.head + 1) == p.tail)
#define queue_empty(p) (p.head == p.tail)
#define queue_put(p, c) (p.data[p.head] = c, p.head = queue_wrap (p.head + 1))
#define queue_get(p, c) (c = p.data[p.tail], p.tail = queue_wrap (p.tail + 1))

注释:queque_wrap的功能类似于%操作!屏蔽掉x的高位。

原文地址:https://www.cnblogs.com/westfly/p/2006963.html