- if (t <= 0)
- t = 1;
- pnew = callfree;
- if (pnew == NULL)
- panic("timeout table overflow");
- callfree = pnew->c_next;
- pnew->c_arg = arg;
- pnew->c_func = func;
- for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2)
- if (p2->c_time > 0)
- t -= p2->c_time;
- p1->c_next = pnew;
- pnew->c_next = p2;
- pnew->c_time = t;
- if (p2)
- p2->c_time -= t;
+
+ /* Fill in the next free callout structure. */
+ if (callfree == NULL)
+ panic("timeout table full");
+ new = callfree;
+ callfree = new->c_next;
+ new->c_arg = arg;
+ new->c_func = ftn;
+
+ /*
+ * The time for each event is stored as a difference from the time
+ * of the previous event on the queue. Walk the queue, correcting
+ * the ticks argument for queue entries passed. Correct the ticks
+ * value for the queue entry immediately after the insertion point
+ * as well.
+ */
+ for (p = &calltodo;
+ (t = p->c_next) != NULL && ticks > t->c_time; p = t)
+ ticks -= t->c_time;
+ new->c_time = ticks;
+ if (t != NULL)
+ t->c_time -= ticks;
+
+ /* Insert the new entry into the queue. */
+ p->c_next = new;
+ new->c_next = t;