我在看Linux源代码关于定时器的部分时感觉跟我的理解有出入
我在看Linux源代码关于定时器的部分时感觉跟我的理解有出入,特地在此请教大家/* The following codes are in the /kernel/timer.c */
static inline void internal_add_timer(struct timer_list *timer)
{
/*
* must be cli-ed when calling this
*/
unsigned long expires = timer->expires;
unsigned long idx = expires - timer_jiffies;
struct list_head * vec;
if (run_timer_list_running)
vec = run_timer_list_running;
else if (idx < TVR_SIZE) {
int i = expires & TVR_MASK;
vec = tv1.vec + i;
} else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
int i = (expires >> TVR_BITS) & TVN_MASK;
vec = tv2.vec + i;
} else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
vec =tv3.vec + i;
} else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
vec = tv4.vec + i;
} else if ((signed long) idx < 0) {
/* can happen if you add a timer with expires == jiffies,
* or you set a timer to go off in the past
*/
vec = tv1.vec + tv1.index;
} else if (idx <= 0xffffffffUL) {
int i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
vec = tv5.vec + i;
} else {
/* Can only get here on architectures with 64-bit jiffies */
INIT_LIST_HEAD(&timer->list);
return;
}
/*
* Timers are FIFO!
*/
list_add(&timer->list, vec->prev);
}
这段代码为什么不写成如下方式:
else if (idx < TVR_SIZE) {
vec = tv1.vec + idx;
} else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
idx = (expires >> TVR_BITS) & TVN_MASK;
vec = tv2.vec + idx;
} else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
idx = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
vec =tv3.vec + idx;
} else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
idx = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
vec = tv4.vec + idx;
} else if ((signed long) idx < 0) {
/* can happen if you add a timer with expires == jiffies,
* or you set a timer to go off in the past
*/
vec = tv1.vec + tv1.index;
} else if (idx <= 0xffffffffUL) {
idx = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
vec = tv5.vec + idx;
还要我经常看到如下代码
#define TVN_BITS 6
#define TVR_BITS 8
#define TVN_SIZE (1 << TVN_BITS)
#define TVR_SIZE (1 << TVR_BITS)
为什么不直接写成
#define TVN_SIZE 64
#define TVR_SIZE 256 首先说宏的定义把,宏本身有个很好的地方是便于全局修改,如果定义为
#define TVN_SIZE 64
那么TVN_SIZE和TVN_BITS之间的联系明确吗?而且当你需要修改连表头数时,
你是不是必须修改两处呢?TVR_BITSTVR_SIZE 都要修改!!
在说说你改的函数,这样改是可以,不过不觉的变量的意义被你破坏了吗?
int i的代价很小的,所以没必要为了这样的代价而破坏变量意义的。 :-D
页:
[1]