[or-cvs] Factor out timeval-related functions.

Nick Mathewson nickm at seul.org
Wed Apr 16 17:05:00 UTC 2003


Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv1720/src/common

Modified Files:
	Makefile.am 
Added Files:
	util.c util.h 
Log Message:
Factor out timeval-related functions.

--- NEW FILE: util.c ---
/* Copyright 2003 Roger Dingledine */
/* See LICENSE for licensing information */
/* $Id: util.c,v 1.1 2003/04/16 17:04:57 nickm Exp $ */

#include <stdlib.h>
#include "util.h"
#include "log.h"

void 
my_gettimeofday(struct timeval *timeval) 
{
  if (gettimeofday(timeval, NULL)) {
    log(LOG_ERR, "my_gettimeofday: gettimeofday failed.");
    /* If gettimeofday dies, we have either given a bad timezone (we didn't),
       or segfaulted.*/
    exit(1);
  }
  return;
}

long
tv_udiff(struct timeval *start, struct timeval *end)
{
  long secdiff = end->tv_sec - start->tv_sec;
  if (secdiff+1 > LONG_MAX/1000000) {
    log(LOG_NOTICE, "tv_udiff(): comparing times too far apart.");
    return LONG_MAX;
  }
  if (end->tv_usec < start->tv_usec) {
    end->tv_sec--;
    end->tv_usec += 1000000L;
  }
  return secdiff*1000000L + (end->tv_usec - start->tv_usec);
}

int tv_cmp(struct timeval *a, struct timeval *b) {
  if (a->tv_sec > b->tv_sec)
    return 1;
  if (a->tv_sec < b->tv_sec)
    return -1;
  if (a->tv_usec > b->tv_usec)
    return 1;
  if (a->tv_usec < b->tv_usec)
    return -1;
  return 0;
}

void tv_add(struct timeval *a, struct timeval *b) {
  a->tv_usec += b->tv_usec;
  a->tv_sec += b->tv_sec + (a->tv_usec / 1000000);
  a->tv_usec %= 1000000;
}

void tv_addms(struct timeval *a, long ms) {
  a->tv_usec += (ms * 1000) % 1000000;
  a->tv_sec += ((ms * 1000) / 1000000) + (a->tv_usec / 1000000);
  a->tv_usec %= 1000000;
}

--- NEW FILE: util.h ---
/* Copyright 2003 Roger Dingledine */
/* See LICENSE for licensing information */
/* $Id: util.h,v 1.1 2003/04/16 17:04:57 nickm Exp $ */

#ifndef __UTIL_H
#define __UTIL_H

#include <sys/time.h>

/* Same as gettimeofday, but no need to check exit value. */
void my_gettimeofday(struct timeval *timeval);
/* Returns the number of microseconds between start and end.  Requires that
 * end >= start, and that the number of microseconds < LONG_MAX. */
long tv_udiff(struct timeval *start, struct timeval *end);

void tv_addms(struct timeval *a, long ms);
void tv_add(struct timeval *a, struct timeval *b);
int tv_cmp(struct timeval *a, struct timeval *b);

#endif

Index: Makefile.am
===================================================================
RCS file: /home/or/cvsroot/src/common/Makefile.am,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- Makefile.am	8 Apr 2003 22:09:18 -0000	1.9
+++ Makefile.am	16 Apr 2003 17:04:57 -0000	1.10
@@ -3,7 +3,7 @@
 
 #CFLAGS  = -Wall -Wpointer-arith -O2
 
-libor_a_SOURCES = log.c crypto.c fakepoll.c
+libor_a_SOURCES = log.c crypto.c fakepoll.c util.c
 
-noinst_HEADERS = log.h ss.h version.h crypto.h fakepoll.h test.h
+noinst_HEADERS = log.h ss.h version.h crypto.h fakepoll.h test.h util.h
 



More information about the tor-commits mailing list