1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright (c) 1985, 1997 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate * All rights reserved.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate * Vuid (Virtual User Input Device) queue management.
29*7c478bd9Sstevel@tonic-gate */
30*7c478bd9Sstevel@tonic-gate
31*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/vuid_event.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/vuid_queue.h>
35*7c478bd9Sstevel@tonic-gate
36*7c478bd9Sstevel@tonic-gate static Vuid_q_node *vq_alloc_node(Vuid_queue *vq);
37*7c478bd9Sstevel@tonic-gate static void vq_free_node(Vuid_queue *vq, Vuid_q_node *vqn);
38*7c478bd9Sstevel@tonic-gate
39*7c478bd9Sstevel@tonic-gate static int tv_equal(struct timeval32 a, struct timeval32 b);
40*7c478bd9Sstevel@tonic-gate static struct timeval32 tv_subt(struct timeval32 atv, struct timeval32 btv);
41*7c478bd9Sstevel@tonic-gate static struct timeval32 tv_divide(struct timeval32 tv, int dividend);
42*7c478bd9Sstevel@tonic-gate static struct timeval32 tv_mult(struct timeval32 tv, int multiplier);
43*7c478bd9Sstevel@tonic-gate #define tv_to_usec(tv) (((tv).tv_sec * 1000000) + (tv).tv_usec)
44*7c478bd9Sstevel@tonic-gate /* Works for small numbers (< 1000) of seconds */
45*7c478bd9Sstevel@tonic-gate static struct timeval32 usec_to_tv(int usec);
46*7c478bd9Sstevel@tonic-gate
47*7c478bd9Sstevel@tonic-gate void
vq_initialize(Vuid_queue * vq,caddr_t data,u_int bytes)48*7c478bd9Sstevel@tonic-gate vq_initialize(Vuid_queue *vq, caddr_t data, u_int bytes)
49*7c478bd9Sstevel@tonic-gate {
50*7c478bd9Sstevel@tonic-gate Vuid_q_node *new_vqns, *vqn;
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate /* Initialize vq */
53*7c478bd9Sstevel@tonic-gate vq->top = vq->bottom = vq->free = VUID_Q_NODE_NULL;
54*7c478bd9Sstevel@tonic-gate vq->size = 1 + (bytes - sizeof (Vuid_q_node)) / sizeof (Vuid_q_node);
55*7c478bd9Sstevel@tonic-gate /* Place in pool by freeing all nodes (fudge vq->num for this) */
56*7c478bd9Sstevel@tonic-gate new_vqns = (Vuid_q_node *)data;
57*7c478bd9Sstevel@tonic-gate vq->num = vq->size;
58*7c478bd9Sstevel@tonic-gate for (vqn = new_vqns; vqn < new_vqns + vq->size; vqn++)
59*7c478bd9Sstevel@tonic-gate vq_free_node(vq, vqn);
60*7c478bd9Sstevel@tonic-gate }
61*7c478bd9Sstevel@tonic-gate
62*7c478bd9Sstevel@tonic-gate Vuid_q_code
vq_put(Vuid_queue * vq,Firm_event * firm_event)63*7c478bd9Sstevel@tonic-gate vq_put(Vuid_queue *vq, Firm_event *firm_event)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate Vuid_q_node *vp;
66*7c478bd9Sstevel@tonic-gate
67*7c478bd9Sstevel@tonic-gate /* Merge into existing events based on time stamp */
68*7c478bd9Sstevel@tonic-gate for (vp = vq->bottom; vp; vp = vp->prev) {
69*7c478bd9Sstevel@tonic-gate /* Put later times closer to the bottom than earlier ones */
70*7c478bd9Sstevel@tonic-gate if (timercmp(&vp->firm_event.time, &firm_event->time,
71*7c478bd9Sstevel@tonic-gate < /* comment to make cstyle happy - heinous! */) ||
72*7c478bd9Sstevel@tonic-gate tv_equal(vp->firm_event.time, firm_event->time)) {
73*7c478bd9Sstevel@tonic-gate Vuid_q_node *vqn = vq_alloc_node(vq);
74*7c478bd9Sstevel@tonic-gate
75*7c478bd9Sstevel@tonic-gate if (vqn == VUID_Q_NODE_NULL)
76*7c478bd9Sstevel@tonic-gate return (VUID_Q_OVERFLOW);
77*7c478bd9Sstevel@tonic-gate vqn->firm_event = *firm_event;
78*7c478bd9Sstevel@tonic-gate /* Insert vqn before vq (neither are null) */
79*7c478bd9Sstevel@tonic-gate /* Initialize vqn's next and prev */
80*7c478bd9Sstevel@tonic-gate vqn->next = vp->next;
81*7c478bd9Sstevel@tonic-gate vqn->prev = vp;
82*7c478bd9Sstevel@tonic-gate /* Fix up vp next's prev */
83*7c478bd9Sstevel@tonic-gate if (vp->next != VUID_Q_NODE_NULL)
84*7c478bd9Sstevel@tonic-gate vp->next->prev = vqn;
85*7c478bd9Sstevel@tonic-gate /* Fix up vp's next */
86*7c478bd9Sstevel@tonic-gate vp->next = vqn;
87*7c478bd9Sstevel@tonic-gate /* Change bottom */
88*7c478bd9Sstevel@tonic-gate if (vp == vq->bottom)
89*7c478bd9Sstevel@tonic-gate vq->bottom = vqn;
90*7c478bd9Sstevel@tonic-gate /* Change top */
91*7c478bd9Sstevel@tonic-gate if (vq->top == VUID_Q_NODE_NULL)
92*7c478bd9Sstevel@tonic-gate vq->top = vqn;
93*7c478bd9Sstevel@tonic-gate return (VUID_Q_OK);
94*7c478bd9Sstevel@tonic-gate }
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate /* Place at top of queue */
97*7c478bd9Sstevel@tonic-gate return (vq_putback(vq, firm_event));
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate
100*7c478bd9Sstevel@tonic-gate Vuid_q_code
vq_get(Vuid_queue * vq,Firm_event * firm_event)101*7c478bd9Sstevel@tonic-gate vq_get(Vuid_queue *vq, Firm_event *firm_event)
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate Vuid_q_node *vqn = vq->top;
104*7c478bd9Sstevel@tonic-gate
105*7c478bd9Sstevel@tonic-gate if (vqn == VUID_Q_NODE_NULL)
106*7c478bd9Sstevel@tonic-gate return (VUID_Q_EMPTY);
107*7c478bd9Sstevel@tonic-gate /* Conditionally copy data */
108*7c478bd9Sstevel@tonic-gate if (firm_event != FIRM_EVENT_NULL)
109*7c478bd9Sstevel@tonic-gate *firm_event = vqn->firm_event;
110*7c478bd9Sstevel@tonic-gate /* Change top */
111*7c478bd9Sstevel@tonic-gate vq->top = vqn->next;
112*7c478bd9Sstevel@tonic-gate /* Null new top's prev */
113*7c478bd9Sstevel@tonic-gate if (vq->top != VUID_Q_NODE_NULL)
114*7c478bd9Sstevel@tonic-gate vq->top->prev = VUID_Q_NODE_NULL;
115*7c478bd9Sstevel@tonic-gate /* Change bottom */
116*7c478bd9Sstevel@tonic-gate if (vq->bottom == vqn)
117*7c478bd9Sstevel@tonic-gate vq->bottom = VUID_Q_NODE_NULL;
118*7c478bd9Sstevel@tonic-gate /* Release storage */
119*7c478bd9Sstevel@tonic-gate vq_free_node(vq, vqn);
120*7c478bd9Sstevel@tonic-gate return (VUID_Q_OK);
121*7c478bd9Sstevel@tonic-gate }
122*7c478bd9Sstevel@tonic-gate
123*7c478bd9Sstevel@tonic-gate Vuid_q_code
vq_peek(Vuid_queue * vq,Firm_event * firm_event)124*7c478bd9Sstevel@tonic-gate vq_peek(Vuid_queue *vq, Firm_event *firm_event)
125*7c478bd9Sstevel@tonic-gate {
126*7c478bd9Sstevel@tonic-gate if (vq->top == VUID_Q_NODE_NULL)
127*7c478bd9Sstevel@tonic-gate return (VUID_Q_EMPTY);
128*7c478bd9Sstevel@tonic-gate *firm_event = vq->top->firm_event;
129*7c478bd9Sstevel@tonic-gate return (VUID_Q_OK);
130*7c478bd9Sstevel@tonic-gate }
131*7c478bd9Sstevel@tonic-gate
132*7c478bd9Sstevel@tonic-gate Vuid_q_code
vq_putback(Vuid_queue * vq,Firm_event * firm_event)133*7c478bd9Sstevel@tonic-gate vq_putback(Vuid_queue *vq, Firm_event *firm_event)
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate Vuid_q_node *vqn = vq_alloc_node(vq);
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate if (vqn == VUID_Q_NODE_NULL)
138*7c478bd9Sstevel@tonic-gate return (VUID_Q_OVERFLOW);
139*7c478bd9Sstevel@tonic-gate vqn->firm_event = *firm_event;
140*7c478bd9Sstevel@tonic-gate /* Change new top's next */
141*7c478bd9Sstevel@tonic-gate vqn->next = vq->top;
142*7c478bd9Sstevel@tonic-gate /* Null new top's prev */
143*7c478bd9Sstevel@tonic-gate vqn->prev = VUID_Q_NODE_NULL;
144*7c478bd9Sstevel@tonic-gate /* Change old top's prev */
145*7c478bd9Sstevel@tonic-gate if (vq->top != VUID_Q_NODE_NULL)
146*7c478bd9Sstevel@tonic-gate vq->top->prev = vqn;
147*7c478bd9Sstevel@tonic-gate /* Change top */
148*7c478bd9Sstevel@tonic-gate vq->top = vqn;
149*7c478bd9Sstevel@tonic-gate /* Change bottom */
150*7c478bd9Sstevel@tonic-gate if (vq->bottom == VUID_Q_NODE_NULL)
151*7c478bd9Sstevel@tonic-gate vq->bottom = vqn;
152*7c478bd9Sstevel@tonic-gate return (VUID_Q_OK);
153*7c478bd9Sstevel@tonic-gate }
154*7c478bd9Sstevel@tonic-gate
155*7c478bd9Sstevel@tonic-gate int
vq_compress(Vuid_queue * vq,int factor)156*7c478bd9Sstevel@tonic-gate vq_compress(Vuid_queue *vq, int factor)
157*7c478bd9Sstevel@tonic-gate {
158*7c478bd9Sstevel@tonic-gate struct timeval32 tv_interval, tv_avg_diff, tv_diff; /* Intermediates */
159*7c478bd9Sstevel@tonic-gate struct timeval32 tv_threshold;
160*7c478bd9Sstevel@tonic-gate Vuid_q_node *base, *victim;
161*7c478bd9Sstevel@tonic-gate Vuid_q_node *victim_next;
162*7c478bd9Sstevel@tonic-gate int num_start;
163*7c478bd9Sstevel@tonic-gate
164*7c478bd9Sstevel@tonic-gate if (vq->top == VUID_Q_NODE_NULL)
165*7c478bd9Sstevel@tonic-gate return (0);
166*7c478bd9Sstevel@tonic-gate num_start = vq->num;
167*7c478bd9Sstevel@tonic-gate /*
168*7c478bd9Sstevel@tonic-gate * Determine the threshold time interval under which events of
169*7c478bd9Sstevel@tonic-gate * the same type (valuator only) are collapsed.
170*7c478bd9Sstevel@tonic-gate * min_time_betvqnen_values = ((first_entry_time - last_entry_time) /
171*7c478bd9Sstevel@tonic-gate * max_number_of_queue_entries) * factor_by_which_to_compress_queue;
172*7c478bd9Sstevel@tonic-gate */
173*7c478bd9Sstevel@tonic-gate tv_interval = tv_subt(vq->bottom->firm_event.time,
174*7c478bd9Sstevel@tonic-gate vq->top->firm_event.time);
175*7c478bd9Sstevel@tonic-gate tv_avg_diff = tv_divide(tv_interval, vq->num);
176*7c478bd9Sstevel@tonic-gate tv_threshold = tv_mult(tv_avg_diff, factor);
177*7c478bd9Sstevel@tonic-gate /* March down list */
178*7c478bd9Sstevel@tonic-gate for (base = vq->top; base; base = base->next) {
179*7c478bd9Sstevel@tonic-gate /* See if valuator event */
180*7c478bd9Sstevel@tonic-gate if (!vq_is_valuator(base))
181*7c478bd9Sstevel@tonic-gate continue;
182*7c478bd9Sstevel@tonic-gate /* Run down list looking for a collapse victim */
183*7c478bd9Sstevel@tonic-gate for (victim = base->next; victim; victim = victim_next) {
184*7c478bd9Sstevel@tonic-gate /* Remember next victim incase axe victim below */
185*7c478bd9Sstevel@tonic-gate victim_next = victim->next;
186*7c478bd9Sstevel@tonic-gate /* Fail if not valuator event */
187*7c478bd9Sstevel@tonic-gate if (!vq_is_valuator(victim))
188*7c478bd9Sstevel@tonic-gate goto Advance_Base;
189*7c478bd9Sstevel@tonic-gate /*
190*7c478bd9Sstevel@tonic-gate * May peek ahead and do the collapse as long as the
191*7c478bd9Sstevel@tonic-gate * intervening times of other valuator event types
192*7c478bd9Sstevel@tonic-gate * are the same. Fail if intervening event's time
193*7c478bd9Sstevel@tonic-gate * differs from victim's.
194*7c478bd9Sstevel@tonic-gate */
195*7c478bd9Sstevel@tonic-gate if (victim->prev != base) {
196*7c478bd9Sstevel@tonic-gate if (!tv_equal(victim->prev->firm_event.time,
197*7c478bd9Sstevel@tonic-gate victim->firm_event.time))
198*7c478bd9Sstevel@tonic-gate goto Advance_Base;
199*7c478bd9Sstevel@tonic-gate }
200*7c478bd9Sstevel@tonic-gate /* Fail if time difference is above threshold */
201*7c478bd9Sstevel@tonic-gate tv_diff = tv_subt(victim->firm_event.time,
202*7c478bd9Sstevel@tonic-gate base->firm_event.time);
203*7c478bd9Sstevel@tonic-gate /* Zero factor means collapse regardless of threshold */
204*7c478bd9Sstevel@tonic-gate if ((factor > 0) &&
205*7c478bd9Sstevel@tonic-gate (timercmp(&tv_diff, &tv_threshold, > /* cstyle */)))
206*7c478bd9Sstevel@tonic-gate goto Advance_Base;
207*7c478bd9Sstevel@tonic-gate /* Do collapse if same event id */
208*7c478bd9Sstevel@tonic-gate if (victim->firm_event.id == base->firm_event.id) {
209*7c478bd9Sstevel@tonic-gate /* Collapse value into base event */
210*7c478bd9Sstevel@tonic-gate switch (base->firm_event.pair_type) {
211*7c478bd9Sstevel@tonic-gate case FE_PAIR_ABSOLUTE:
212*7c478bd9Sstevel@tonic-gate /* id is delta */
213*7c478bd9Sstevel@tonic-gate base->firm_event.value +=
214*7c478bd9Sstevel@tonic-gate victim->firm_event.value;
215*7c478bd9Sstevel@tonic-gate break;
216*7c478bd9Sstevel@tonic-gate case FE_PAIR_DELTA:
217*7c478bd9Sstevel@tonic-gate /* id is absolute */
218*7c478bd9Sstevel@tonic-gate /* Fall through */
219*7c478bd9Sstevel@tonic-gate default:
220*7c478bd9Sstevel@tonic-gate /* Assume id is absolute */
221*7c478bd9Sstevel@tonic-gate base->firm_event.value =
222*7c478bd9Sstevel@tonic-gate victim->firm_event.value;
223*7c478bd9Sstevel@tonic-gate break;
224*7c478bd9Sstevel@tonic-gate }
225*7c478bd9Sstevel@tonic-gate /* Remove victim node */
226*7c478bd9Sstevel@tonic-gate vq_delete_node(vq, victim);
227*7c478bd9Sstevel@tonic-gate }
228*7c478bd9Sstevel@tonic-gate }
229*7c478bd9Sstevel@tonic-gate Advance_Base:
230*7c478bd9Sstevel@tonic-gate {}
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate return (num_start - vq->num);
233*7c478bd9Sstevel@tonic-gate }
234*7c478bd9Sstevel@tonic-gate
235*7c478bd9Sstevel@tonic-gate int
vq_is_valuator(Vuid_q_node * vqn)236*7c478bd9Sstevel@tonic-gate vq_is_valuator(Vuid_q_node *vqn)
237*7c478bd9Sstevel@tonic-gate {
238*7c478bd9Sstevel@tonic-gate return ((vqn->firm_event.value < 1 && vqn->firm_event.value > -1) ||
239*7c478bd9Sstevel@tonic-gate (vqn->firm_event.pair_type == FE_PAIR_DELTA) ||
240*7c478bd9Sstevel@tonic-gate (vqn->firm_event.pair_type == FE_PAIR_ABSOLUTE));
241*7c478bd9Sstevel@tonic-gate }
242*7c478bd9Sstevel@tonic-gate
243*7c478bd9Sstevel@tonic-gate void
vq_delete_node(Vuid_queue * vq,Vuid_q_node * vqn)244*7c478bd9Sstevel@tonic-gate vq_delete_node(Vuid_queue *vq, Vuid_q_node *vqn)
245*7c478bd9Sstevel@tonic-gate {
246*7c478bd9Sstevel@tonic-gate /* Use get if removing off top of queue */
247*7c478bd9Sstevel@tonic-gate if (vqn == vq->top) {
248*7c478bd9Sstevel@tonic-gate (void) vq_get(vq, FIRM_EVENT_NULL);
249*7c478bd9Sstevel@tonic-gate return;
250*7c478bd9Sstevel@tonic-gate }
251*7c478bd9Sstevel@tonic-gate /* Update previous next link (not null else vqn would be top) */
252*7c478bd9Sstevel@tonic-gate vqn->prev->next = vqn->next;
253*7c478bd9Sstevel@tonic-gate /* Change bottom */
254*7c478bd9Sstevel@tonic-gate if (vq->bottom == vqn)
255*7c478bd9Sstevel@tonic-gate vq->bottom = vqn->prev;
256*7c478bd9Sstevel@tonic-gate else
257*7c478bd9Sstevel@tonic-gate /* Update next previous link (if null else vqn is bottom) */
258*7c478bd9Sstevel@tonic-gate vqn->next->prev = vqn->prev;
259*7c478bd9Sstevel@tonic-gate /* Release storage */
260*7c478bd9Sstevel@tonic-gate vq_free_node(vq, vqn);
261*7c478bd9Sstevel@tonic-gate }
262*7c478bd9Sstevel@tonic-gate
263*7c478bd9Sstevel@tonic-gate /*
264*7c478bd9Sstevel@tonic-gate * Caller must initialize data returned from vq_alloc_node.
265*7c478bd9Sstevel@tonic-gate * VUID_Q_NODE_NULL is possible.
266*7c478bd9Sstevel@tonic-gate */
267*7c478bd9Sstevel@tonic-gate static Vuid_q_node *
vq_alloc_node(Vuid_queue * vq)268*7c478bd9Sstevel@tonic-gate vq_alloc_node(Vuid_queue *vq)
269*7c478bd9Sstevel@tonic-gate {
270*7c478bd9Sstevel@tonic-gate Vuid_q_node *vqn;
271*7c478bd9Sstevel@tonic-gate
272*7c478bd9Sstevel@tonic-gate if (vq->free == VUID_Q_NODE_NULL)
273*7c478bd9Sstevel@tonic-gate return (VUID_Q_NODE_NULL);
274*7c478bd9Sstevel@tonic-gate vqn = vq->free;
275*7c478bd9Sstevel@tonic-gate vq->free = vq->free->next;
276*7c478bd9Sstevel@tonic-gate vq->num++;
277*7c478bd9Sstevel@tonic-gate vqn->next = VUID_Q_NODE_NULL;
278*7c478bd9Sstevel@tonic-gate return (vqn);
279*7c478bd9Sstevel@tonic-gate }
280*7c478bd9Sstevel@tonic-gate
281*7c478bd9Sstevel@tonic-gate static void
vq_free_node(Vuid_queue * vq,Vuid_q_node * vqn)282*7c478bd9Sstevel@tonic-gate vq_free_node(Vuid_queue *vq, Vuid_q_node *vqn)
283*7c478bd9Sstevel@tonic-gate {
284*7c478bd9Sstevel@tonic-gate vqn->next = vq->free;
285*7c478bd9Sstevel@tonic-gate vqn->prev = VUID_Q_NODE_NULL;
286*7c478bd9Sstevel@tonic-gate vq->free = vqn;
287*7c478bd9Sstevel@tonic-gate vq->num--;
288*7c478bd9Sstevel@tonic-gate }
289*7c478bd9Sstevel@tonic-gate
290*7c478bd9Sstevel@tonic-gate static int
tv_equal(struct timeval32 a,struct timeval32 b)291*7c478bd9Sstevel@tonic-gate tv_equal(struct timeval32 a, struct timeval32 b)
292*7c478bd9Sstevel@tonic-gate {
293*7c478bd9Sstevel@tonic-gate return (a.tv_sec == b.tv_sec && a.tv_usec == b.tv_usec);
294*7c478bd9Sstevel@tonic-gate }
295*7c478bd9Sstevel@tonic-gate
296*7c478bd9Sstevel@tonic-gate /* atv-btv */
297*7c478bd9Sstevel@tonic-gate static struct timeval32
tv_subt(struct timeval32 atv,struct timeval32 btv)298*7c478bd9Sstevel@tonic-gate tv_subt(struct timeval32 atv, struct timeval32 btv)
299*7c478bd9Sstevel@tonic-gate {
300*7c478bd9Sstevel@tonic-gate if ((atv.tv_usec < btv.tv_usec) && atv.tv_sec) {
301*7c478bd9Sstevel@tonic-gate atv.tv_usec += 1000000;
302*7c478bd9Sstevel@tonic-gate atv.tv_sec--;
303*7c478bd9Sstevel@tonic-gate }
304*7c478bd9Sstevel@tonic-gate if (atv.tv_usec > btv.tv_usec)
305*7c478bd9Sstevel@tonic-gate atv.tv_usec -= btv.tv_usec;
306*7c478bd9Sstevel@tonic-gate else
307*7c478bd9Sstevel@tonic-gate atv.tv_usec = 0;
308*7c478bd9Sstevel@tonic-gate if (atv.tv_sec > btv.tv_sec)
309*7c478bd9Sstevel@tonic-gate atv.tv_sec -= btv.tv_sec;
310*7c478bd9Sstevel@tonic-gate else {
311*7c478bd9Sstevel@tonic-gate if (atv.tv_sec < btv.tv_sec)
312*7c478bd9Sstevel@tonic-gate atv.tv_usec = 0;
313*7c478bd9Sstevel@tonic-gate atv.tv_sec = 0;
314*7c478bd9Sstevel@tonic-gate }
315*7c478bd9Sstevel@tonic-gate return (atv);
316*7c478bd9Sstevel@tonic-gate }
317*7c478bd9Sstevel@tonic-gate
318*7c478bd9Sstevel@tonic-gate /* tv / dividend */
319*7c478bd9Sstevel@tonic-gate static struct timeval32
tv_divide(struct timeval32 tv,int dividend)320*7c478bd9Sstevel@tonic-gate tv_divide(struct timeval32 tv, int dividend)
321*7c478bd9Sstevel@tonic-gate {
322*7c478bd9Sstevel@tonic-gate int usecs;
323*7c478bd9Sstevel@tonic-gate
324*7c478bd9Sstevel@tonic-gate if (dividend == 0)
325*7c478bd9Sstevel@tonic-gate return (tv);
326*7c478bd9Sstevel@tonic-gate usecs = tv_to_usec(tv);
327*7c478bd9Sstevel@tonic-gate usecs /= dividend;
328*7c478bd9Sstevel@tonic-gate tv = usec_to_tv(usecs);
329*7c478bd9Sstevel@tonic-gate return (tv);
330*7c478bd9Sstevel@tonic-gate }
331*7c478bd9Sstevel@tonic-gate
332*7c478bd9Sstevel@tonic-gate /* tv * multiplier (works for small multipliers * seconds, < 1000) */
333*7c478bd9Sstevel@tonic-gate static struct timeval32
tv_mult(struct timeval32 tv,int multiplier)334*7c478bd9Sstevel@tonic-gate tv_mult(struct timeval32 tv, int multiplier)
335*7c478bd9Sstevel@tonic-gate {
336*7c478bd9Sstevel@tonic-gate int usecs;
337*7c478bd9Sstevel@tonic-gate
338*7c478bd9Sstevel@tonic-gate usecs = tv_to_usec(tv);
339*7c478bd9Sstevel@tonic-gate usecs *= multiplier;
340*7c478bd9Sstevel@tonic-gate tv = usec_to_tv(usecs);
341*7c478bd9Sstevel@tonic-gate return (tv);
342*7c478bd9Sstevel@tonic-gate }
343*7c478bd9Sstevel@tonic-gate
344*7c478bd9Sstevel@tonic-gate static struct timeval32
usec_to_tv(int usec)345*7c478bd9Sstevel@tonic-gate usec_to_tv(int usec)
346*7c478bd9Sstevel@tonic-gate {
347*7c478bd9Sstevel@tonic-gate struct timeval32 tv;
348*7c478bd9Sstevel@tonic-gate
349*7c478bd9Sstevel@tonic-gate tv.tv_sec = usec / 1000000;
350*7c478bd9Sstevel@tonic-gate tv.tv_usec = usec % 1000000;
351*7c478bd9Sstevel@tonic-gate return (tv);
352*7c478bd9Sstevel@tonic-gate }
353