root/rat/trunk/time.c @ 1901

Revision 1901, 4.3 KB (checked in by ucaccsp, 16 years ago)

Split config.h into config_unix.h and config_win32.h
Converted all files to use these and removed all #include's of
system headers into these two files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * FILE:    time.c
3 * PROGRAM: RAT
4 * AUTHOR:  I.Kouvelas + O.Hodson
5 *
6 * $Revision$
7 * $Date$
8 *
9 * Copyright (c) 1995,1996 University College London
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, is permitted, for non-commercial use only, provided
14 * that the following conditions are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 *    must display the following acknowledgement:
22 *      This product includes software developed by the Computer Science
23 *      Department at University College London
24 * 4. Neither the name of the University nor of the Department may be used
25 *    to endorse or promote products derived from this software without
26 *    specific prior written permission.
27 * Use of this software for commercial purposes is explicitly forbidden
28 * unless prior written permission is obtained from the authors.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 */
42
43#include "config_unix.h"
44#include "config_win32.h"
45#include "assert.h"
46#include "rat_types.h"
47#include "rat_time.h"
48#include "util.h"
49
50#define HALF_TS_CYCLE   0x80000000
51
52typedef struct s_fast_time {
53        int freq;
54        u_int32 low;
55        u_int32 high;
56} ft_t;
57
58typedef struct s_time {
59        struct s_fast_time *ft;
60        int     freq;
61        int     scale;
62} frtime_t;
63
64ft_t *
65new_fast_time(int freq)
66{
67        ft_t *ft;
68
69        ft = (ft_t*)xmalloc(sizeof(ft_t));
70        ft->freq = freq;
71        ft->low = ft->high = 0;
72        return (ft);
73}
74
75void
76free_fast_time(ft_t *ft)
77{
78        xfree(ft);
79}
80
81__inline void
82time_advance(ft_t *ft, int freq, u_int32 time)
83{
84        u_int32 tmp = ft->low;
85        ft->low += time * ft->freq / freq;
86        if (ft->low < tmp)
87                ft->high++;
88}
89
90frtime_t *
91new_time(ft_t *ft, int freq)
92{
93        frtime_t        *tp;
94
95        tp = (frtime_t*)xmalloc(sizeof(frtime_t));
96        tp->freq = freq;
97        tp->ft = ft;
98        tp->scale = 1;
99        change_freq(tp, freq);
100        assert(tp->scale != 0);
101        return (tp);
102}
103
104void
105free_time(frtime_t *tp)
106{
107        xfree(tp);
108}
109
110void
111change_freq(frtime_t *tp, int freq)
112{
113        assert(freq <= tp->ft->freq);
114
115        tp->scale = tp->ft->freq/freq;
116        tp->freq = freq;
117}
118
119__inline int 
120get_freq(frtime_t *tp)
121{
122        return (tp->freq);
123}
124
125/* Calculates time scaled to tp->freq units */
126__inline u_int32
127get_time(frtime_t *tp)
128{
129        u_int32 t;
130        t = tp->ft->low / tp->scale + tp->ft->high * tp->scale;
131        return (t);
132}
133
134u_int32
135convert_time(u_int32 ts, frtime_t *from, frtime_t *to)
136{
137        assert(to->freq % from->freq == 0 || from->freq % to->freq == 0);
138
139        if (from->freq == to->freq) {
140                return ts;
141        } else if (from->freq < to->freq) {
142                return ts * (to->freq / from->freq);
143        } else {
144                return ts / (from->freq / to->freq);
145        }
146}
147
148/*
149 * Compare two timestamps and return TRUE if t1 > t2 Assume that they are
150 * close together (less than half a cycle) and handle wraparounds...
151 */
152__inline int
153ts_gt(u_int32 t1, u_int32 t2)
154{
155        u_int32         diff;
156
157        diff = t1 - t2;
158        return (diff < HALF_TS_CYCLE && diff != 0);
159}
160
161/*
162 * Return the abolute difference of two timestamps. As above assume they are
163 * close and handle wraprounds...
164 */
165__inline u_int32
166ts_abs_diff(u_int32 t1, u_int32 t2)
167{
168        u_int32         diff;
169
170        diff = t1 - t2;
171        if (diff > HALF_TS_CYCLE)
172                diff = t2 - t1;
173
174        return (diff);
175}
Note: See TracBrowser for help on using the browser.