make tpop const
[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 #include <sys/time.h>
26
27 # include "timing.h"
28
29 struct timing_env
30 {
31   struct timeval *start;
32   struct timeval *end;
33 };
34
35 \f
36 /*
37 Helpers
38 */
39 static int
40 timeval_subtract (struct timeval *x, struct timeval *y)
41 {
42   /* Perform the carry for the later subtraction by updating Y. */
43   if (x->tv_usec < y->tv_usec) {
44     int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
45     y->tv_usec -= 1000000 * nsec;
46     y->tv_sec += nsec;
47   }
48
49   if (x->tv_usec - y->tv_usec > 1000000) {
50     int nsec = (x->tv_usec - y->tv_usec) / 1000000;
51     y->tv_usec += 1000000 * nsec;
52     y->tv_sec -= nsec;
53   }
54
55   return ((x->tv_sec - y->tv_sec) * 1000000 + (x->tv_usec - y->tv_usec));
56 }
57
58 \f
59 /*
60   Public Interface
61 */
62 timing_t *
63 start_timing (void)
64 {
65   timing_t *t = (timing_t*) malloc (sizeof (timing_t));
66
67   t->start = (struct timeval*) malloc (sizeof (struct timeval));
68   t->end   = (struct timeval*) malloc (sizeof (struct timeval));
69
70   gettimeofday (t->start, NULL);
71
72   return (t);
73 }
74
75 int
76 end_timing (timing_t *t)
77 {
78   int time;
79
80   gettimeofday (t->end, NULL);
81
82   time = timeval_subtract (t->end, t->start);
83
84   memset (t->start, 0x0, sizeof (struct timeval));
85   free (t->start);
86
87   memset (t->end,   0x0, sizeof (struct timeval));
88   free (t->end);
89
90   memset (t, 0x00, sizeof (timing_t));
91   free (t);
92
93   return (time);
94 }
95
96 \f
97 /*
98   $Log$
99   Revision 1.2  2004/12/21 15:52:23  beck
100   moved struct timing_env to .c file, added config.h
101
102   Revision 1.1  2004/10/29 18:55:52  liekweg
103   (mostly) generic timimg
104
105
106 */