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