tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

prinrval.c (3256B)


      1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 /*
      7 * file:            prinrval.c
      8 * description:     implementation for the kernel interval timing functions
      9 */
     10 
     11 #include "primpl.h"
     12 
     13 /*
     14 *-----------------------------------------------------------------------
     15 *
     16 * _PR_InitClock --
     17 *
     18 *
     19 *-----------------------------------------------------------------------
     20 */
     21 
     22 void _PR_InitClock(void) {
     23  _PR_MD_INTERVAL_INIT();
     24 #ifdef DEBUG
     25  {
     26    PRIntervalTime ticksPerSec = PR_TicksPerSecond();
     27 
     28    PR_ASSERT(ticksPerSec >= PR_INTERVAL_MIN);
     29    PR_ASSERT(ticksPerSec <= PR_INTERVAL_MAX);
     30  }
     31 #endif /* DEBUG */
     32 }
     33 
     34 PR_IMPLEMENT(PRIntervalTime) PR_IntervalNow(void) {
     35  if (!_pr_initialized) {
     36    _PR_ImplicitInitialization();
     37  }
     38  return _PR_MD_GET_INTERVAL();
     39 } /* PR_IntervalNow */
     40 
     41 PR_EXTERN(PRUint32) PR_TicksPerSecond(void) {
     42  if (!_pr_initialized) {
     43    _PR_ImplicitInitialization();
     44  }
     45  return _PR_MD_INTERVAL_PER_SEC();
     46 } /* PR_TicksPerSecond */
     47 
     48 PR_IMPLEMENT(PRIntervalTime) PR_SecondsToInterval(PRUint32 seconds) {
     49  return seconds * PR_TicksPerSecond();
     50 } /* PR_SecondsToInterval */
     51 
     52 PR_IMPLEMENT(PRIntervalTime) PR_MillisecondsToInterval(PRUint32 milli) {
     53  PRIntervalTime ticks;
     54  PRUint64 tock, tps, msecPerSec, rounding;
     55  LL_UI2L(tock, milli);
     56  LL_I2L(msecPerSec, PR_MSEC_PER_SEC);
     57  LL_I2L(rounding, (PR_MSEC_PER_SEC >> 1));
     58  LL_I2L(tps, PR_TicksPerSecond());
     59  LL_MUL(tock, tock, tps);
     60  LL_ADD(tock, tock, rounding);
     61  LL_DIV(tock, tock, msecPerSec);
     62  LL_L2UI(ticks, tock);
     63  return ticks;
     64 } /* PR_MillisecondsToInterval */
     65 
     66 PR_IMPLEMENT(PRIntervalTime) PR_MicrosecondsToInterval(PRUint32 micro) {
     67  PRIntervalTime ticks;
     68  PRUint64 tock, tps, usecPerSec, rounding;
     69  LL_UI2L(tock, micro);
     70  LL_I2L(usecPerSec, PR_USEC_PER_SEC);
     71  LL_I2L(rounding, (PR_USEC_PER_SEC >> 1));
     72  LL_I2L(tps, PR_TicksPerSecond());
     73  LL_MUL(tock, tock, tps);
     74  LL_ADD(tock, tock, rounding);
     75  LL_DIV(tock, tock, usecPerSec);
     76  LL_L2UI(ticks, tock);
     77  return ticks;
     78 } /* PR_MicrosecondsToInterval */
     79 
     80 PR_IMPLEMENT(PRUint32) PR_IntervalToSeconds(PRIntervalTime ticks) {
     81  return ticks / PR_TicksPerSecond();
     82 } /* PR_IntervalToSeconds */
     83 
     84 PR_IMPLEMENT(PRUint32) PR_IntervalToMilliseconds(PRIntervalTime ticks) {
     85  PRUint32 milli;
     86  PRUint64 tock, tps, msecPerSec, rounding;
     87  LL_UI2L(tock, ticks);
     88  LL_I2L(msecPerSec, PR_MSEC_PER_SEC);
     89  LL_I2L(tps, PR_TicksPerSecond());
     90  LL_USHR(rounding, tps, 1);
     91  LL_MUL(tock, tock, msecPerSec);
     92  LL_ADD(tock, tock, rounding);
     93  LL_DIV(tock, tock, tps);
     94  LL_L2UI(milli, tock);
     95  return milli;
     96 } /* PR_IntervalToMilliseconds */
     97 
     98 PR_IMPLEMENT(PRUint32) PR_IntervalToMicroseconds(PRIntervalTime ticks) {
     99  PRUint32 micro;
    100  PRUint64 tock, tps, usecPerSec, rounding;
    101  LL_UI2L(tock, ticks);
    102  LL_I2L(usecPerSec, PR_USEC_PER_SEC);
    103  LL_I2L(tps, PR_TicksPerSecond());
    104  LL_USHR(rounding, tps, 1);
    105  LL_MUL(tock, tock, usecPerSec);
    106  LL_ADD(tock, tock, rounding);
    107  LL_DIV(tock, tock, tps);
    108  LL_L2UI(micro, tock);
    109  return micro;
    110 } /* PR_IntervalToMicroseconds */
    111 
    112 /* prinrval.c */