removed C99 constructs
[libfirm] / ir / ana2 / timing.c
1 /* -*- c -*- */
2
3 /*
4  * Time-stamp: <26.10.2004 11:57:13h liekweg>
5  * Project:     libFIRM
6  * File name:   ir/ana2/timing.c
7  * Purpose:     generic timing routines
8  * Author:      Florian
9  * Modified by:
10  * Created:     Mon 18 Oct 2004
11  * CVS-ID:      $Id$
12  * Copyright:   (c) 1999-2004 Universität Karlsruhe
13  * Licence:     This file is protected by GPL -  GNU GENERAL PUBLIC LICENSE.
14  */
15
16 /*
17   Timing stuff.  Not really part of ana2, but where else should it go.
18 */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/time.h>
23
24 # include "timing.h"
25
26 \f
27 /*
28 Helpers
29 */
30 static int
31 timeval_subtract (struct timeval *x, struct timeval *y)
32 {
33   /* Perform the carry for the later subtraction by updating Y. */
34   if (x->tv_usec < y->tv_usec) {
35     int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
36     y->tv_usec -= 1000000 * nsec;
37     y->tv_sec += nsec;
38   }
39
40   if (x->tv_usec - y->tv_usec > 1000000) {
41     int nsec = (x->tv_usec - y->tv_usec) / 1000000;
42     y->tv_usec += 1000000 * nsec;
43     y->tv_sec -= nsec;
44   }
45
46   return ((x->tv_sec - y->tv_sec) * 1000000 + (x->tv_usec - y->tv_usec));
47 }
48
49 \f
50 /*
51   Public Interface
52 */
53 timing_t *
54 start_timing ()
55 {
56   timing_t *t = (timing_t*) malloc (sizeof (timing_t));
57
58   t->start = (struct timeval*) malloc (sizeof (struct timeval));
59   t->end   = (struct timeval*) malloc (sizeof (struct timeval));
60
61   gettimeofday (t->start, NULL);
62
63   return (t);
64 }
65
66 int
67 end_timing (timing_t *t)
68 {
69   int time;
70
71   gettimeofday (t->end, NULL);
72
73   time = timeval_subtract (t->end, t->start);
74
75   memset (t->start, 0x0, sizeof (struct timeval));
76   free (t->start);
77
78   memset (t->end,   0x0, sizeof (struct timeval));
79   free (t->end);
80
81   memset (t, 0x00, sizeof (timing_t));
82   free (t);
83
84   return (time);
85 }
86
87 \f
88 /*
89   $Log$
90   Revision 1.1  2004/10/29 18:55:52  liekweg
91   (mostly) generic timimg
92
93
94 */