89622cf38bc6feef44e4178c64353503d8e80d6c
[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 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 # include "timing.h"
27
28 struct timing_env
29 {
30   struct timeval *start;
31   struct timeval *end;
32 };
33
34 #ifdef _WIN32
35 /* no support yet */
36 timing_t *start_timing (void) {}
37 int end_timing (timing_t *t) {}
38
39 #else
40 #include <sys/time.h>
41
42 \f
43 /*
44 Helpers
45 */
46 static int
47 timeval_subtract (struct timeval *x, struct timeval *y)
48 {
49   /* Perform the carry for the later subtraction by updating Y. */
50   if (x->tv_usec < y->tv_usec) {
51     int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
52     y->tv_usec -= 1000000 * nsec;
53     y->tv_sec += nsec;
54   }
55
56   if (x->tv_usec - y->tv_usec > 1000000) {
57     int nsec = (x->tv_usec - y->tv_usec) / 1000000;
58     y->tv_usec += 1000000 * nsec;
59     y->tv_sec -= nsec;
60   }
61
62   return ((x->tv_sec - y->tv_sec) * 1000000 + (x->tv_usec - y->tv_usec));
63 }
64
65 \f
66 /*
67   Public Interface
68 */
69 timing_t *
70 start_timing (void)
71 {
72   timing_t *t = (timing_t*) xmalloc (sizeof (timing_t));
73
74   t->start = (struct timeval*) xmalloc (sizeof (struct timeval));
75   t->end   = (struct timeval*) xmalloc (sizeof (struct timeval));
76
77   gettimeofday (t->start, NULL);
78
79   return (t);
80 }
81
82 int
83 end_timing (timing_t *t)
84 {
85   int time;
86
87   gettimeofday (t->end, NULL);
88
89   time = timeval_subtract (t->end, t->start);
90
91   memset (t->start, 0x0, sizeof (struct timeval));
92   free (t->start);
93
94   memset (t->end,   0x0, sizeof (struct timeval));
95   free (t->end);
96
97   memset (t, 0x00, sizeof (timing_t));
98   free (t);
99
100   return (time);
101 }
102 #endif /* _WIN32 */
103
104 \f
105 /*
106   $Log$
107   Revision 1.4  2006/06/06 12:06:27  beck
108   use xmalloc instead of malloc
109
110   Revision 1.3  2005/01/05 14:25:38  beck
111   added Win32 "support"
112
113   Revision 1.2  2004/12/21 15:52:23  beck
114   moved struct timing_env to .c file, added config.h
115
116   Revision 1.1  2004/10/29 18:55:52  liekweg
117   (mostly) generic timimg
118
119
120 */