fix
[libfirm] / ir / ana2 / timing.c
1 /* -*- c -*- */
2
3 /*
4  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
5  *
6  * This file is part of libFirm.
7  *
8  * This file may be distributed and/or modified under the terms of the
9  * GNU General Public License version 2 as published by the Free Software
10  * Foundation and appearing in the file LICENSE.GPL included in the
11  * packaging of this file.
12  *
13  * Licensees holding valid libFirm Professional Edition licenses may use
14  * this file in accordance with the libFirm Commercial License.
15  * Agreement provided with the Software.
16  *
17  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE.
20  */
21
22 /**
23  * @file
24  * @brief    generic timing routines
25  * @author   Florian
26  * @date     Mon 18 Oct 2004
27  * @version  $Id$
28  * @note
29  *   Timing stuff.  Not really part of ana2, but where else should it go.
30  */
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "timing.h"
39 #include "xmalloc.h"
40
41 struct timing_env
42 {
43   struct timeval *start;
44   struct timeval *end;
45 };
46
47 #ifdef _WIN32
48 /* no support yet */
49 timing_t *start_timing (void) {}
50 int end_timing (timing_t *t) {}
51
52 #else
53 #include <sys/time.h>
54
55
56 /*
57 Helpers
58 */
59 static int
60 timeval_subtract (struct timeval *x, struct timeval *y)
61 {
62   /* Perform the carry for the later subtraction by updating Y. */
63   if (x->tv_usec < y->tv_usec) {
64     int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
65     y->tv_usec -= 1000000 * nsec;
66     y->tv_sec += nsec;
67   }
68
69   if (x->tv_usec - y->tv_usec > 1000000) {
70     int nsec = (x->tv_usec - y->tv_usec) / 1000000;
71     y->tv_usec += 1000000 * nsec;
72     y->tv_sec -= nsec;
73   }
74
75   return ((x->tv_sec - y->tv_sec) * 1000000 + (x->tv_usec - y->tv_usec));
76 }
77
78
79 /*
80   Public Interface
81 */
82 timing_t *
83 start_timing (void)
84 {
85   timing_t *t = (timing_t*) xmalloc (sizeof (timing_t));
86
87   t->start = (struct timeval*) xmalloc (sizeof (struct timeval));
88   t->end   = (struct timeval*) xmalloc (sizeof (struct timeval));
89
90   gettimeofday (t->start, NULL);
91
92   return (t);
93 }
94
95 int
96 end_timing (timing_t *t)
97 {
98   int time;
99
100   gettimeofday (t->end, NULL);
101
102   time = timeval_subtract (t->end, t->start);
103
104   memset (t->start, 0x0, sizeof (struct timeval));
105   free (t->start);
106
107   memset (t->end,   0x0, sizeof (struct timeval));
108   free (t->end);
109
110   memset (t, 0x00, sizeof (timing_t));
111   free (t);
112
113   return (time);
114 }
115 #endif /* _WIN32 */
116
117
118 /*
119   $Log$
120   Revision 1.5  2006/09/12 12:17:37  matze
121   more warning fixes
122
123   Revision 1.4  2006/06/06 12:06:27  beck
124   use xmalloc instead of malloc
125
126   Revision 1.3  2005/01/05 14:25:38  beck
127   added Win32 "support"
128
129   Revision 1.2  2004/12/21 15:52:23  beck
130   moved struct timing_env to .c file, added config.h
131
132   Revision 1.1  2004/10/29 18:55:52  liekweg
133   (mostly) generic timimg
134 */