static int
_nas_timer_db_insert (
  timer_queue_t * entry)
{
  timer_queue_t                          *prev,
                                         *next; /* previous and next entry in the list  */

  /*
   * Search the list of timer entries for the first entry with an interval
   * timer value greater than the interval timer value of the new timer entry
   */
  for (prev = NULL, next = _nas_timer_db.head; next != NULL; next = next->next) {
    if (_nas_timer_cmp (&next->entry->tv, &entry->entry->tv) > 0) {
      break;
    }

    prev = next;
  }

  /*
   * Insert the new entry in the list of active timer entries
   */
  /*
   * prev <-- entry --> next
   */
  entry->prev = prev;
  entry->next = next;

  /*
   * Update the pointer from the previous entry
   */
  if (entry->next != NULL) {
    /*
     * prev <-- entry <--> next
     */
    entry->next->prev = entry;
  }

  /*
   * Update the pointer from the next entry
   */
  if (entry->prev != NULL) {
    /*
     * prev <--> entry <--> next
     */
    entry->prev->next = entry;
  } else {
    /*
     * The new entry is the first entry of the list
     */
    _nas_timer_db.head = entry;
    return TRUE;
  }

  /*
   * The new entry is NOT the first entry of the list
   */
  return FALSE;
}
/****************************************************************************
 **                                                                        **
 ** Name:    _nas_timer_sub()                                          **
 **                                                                        **
 ** Description: Performs timeval substraction                             **
 **                                                                        **
 ** Inputs:  a:     The first timeval structure                **
 **      b:     The second timeval structure               **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     result:    a >= b, result = timeval(a - b)            **
 **      Return:    -1 if a < b; 0 otherwise                   **
 **      Others:    None                                       **
 **                                                                        **
 ***************************************************************************/
static int _nas_timer_sub(const struct timeval *a, const struct timeval *b,
                          struct timeval *result)
{
  if (_nas_timer_cmp(a,b) > 0 ) {
    result->tv_sec = a->tv_sec - b->tv_sec;
    result->tv_usec = a->tv_usec - b->tv_usec;

    if (result->tv_usec < 0) {
      result->tv_sec--;
      result->tv_usec += 1000000;
    }

    return 0;
  }

  return -1;
}