summaryrefslogtreecommitdiff
path: root/open-vm-tools/lib/include/util_shared.h
blob: e669683d027d8b4d21f5cc7c8c747f8601c5094c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*********************************************************
 * Copyright (C) 2002 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

#define INCLUDE_ALLOW_USERLEVEL

#define INCLUDE_ALLOW_VMCORE
#define INCLUDE_ALLOW_VMKERNEL
#include "includeCheck.h"

#include "vm_assert.h"
#include "vm_basic_types.h"

/*
 * Defines util functions shared between the userlevel code and the monitor.
 */

/*
 *----------------------------------------------------------------------
 *
 * Util_Throttle --
 * 
 *   Use for throttling of warnings. 
 *
 * Results:
 *    Will return TRUE for an increasingly sparse set of counter values: 
 *    1, 2, ..., 100, 200, 300, ..., 10000, 20000, 30000, ..., . 
 *
 * Side effects:
 *   None.
 *
 *----------------------------------------------------------------------
 */

Bool
Util_Throttle(uint32 count)  // IN:
{
   return count <     100                          ||
         (count <   10000 && count %     100 == 0) ||
         (count < 1000000 && count %   10000 == 0) ||
                             count % 1000000 == 0;
}


/*
 *----------------------------------------------------------------------
 *
 * Util_FastRand --
 *
 *	Generates the next random number in the pseudo-random sequence 
 *	defined by the multiplicative linear congruential generator 
 *	S' = 16807 * S mod (2^31 - 1).
 *	This is the ACM "minimal standard random number generator".  
 *	Based on method described by D.G. Carta in CACM, January 1990. 
 *	Usage: provide previous random number as the seed for next one.
 *
 * Precondition:
 *      0 < seed && seed < UTIL_FASTRAND_SEED_MAX
 *
 * Results:
 *	A random number.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

uint32
Util_FastRand(uint32 seed)
{
   uint64 product    = 33614 * (uint64)seed;
   uint32 product_lo = (uint32)(product & 0xffffffff) >> 1;
   uint32 product_hi = product >> 32;
   int32  test       = product_lo + product_hi;

   ASSERT(0 < seed && seed < UTIL_FASTRAND_SEED_MAX);

   return (test > 0) ? test : (test & UTIL_FASTRAND_SEED_MAX) + 1;
}


#if defined(USERLEVEL) || defined(VMX86_DEBUG)
static uint32 crcTable[256];

static void 
UtilCRCMakeTable(void)
{
   uint32 c;
   int n, k;
   
   for (n = 0; n < 256; n++) {
      c = (uint32) n;
      for (k = 0; k < 8; k++) {
         if (c & 1) {
	    c = 0xedb88320L ^ (c >> 1);
	 } else {
	    c = c >> 1;
	 }
      }
      crcTable[n] = c;
   }
}
   
static INLINE_SINGLE_CALLER uint32 
UtilCRCUpdate(uint32 crc,
              const uint8 *buf,
              int len)
{
   uint32 c = crc;
   int n;
   static int crcTableComputed = 0;
   
   if (!crcTableComputed) {
      UtilCRCMakeTable();

      crcTableComputed = 1;
  }
    
   for (n = 0; n < len; n++) {
      c = crcTable[(c ^ buf[n]) & 0xff] ^ (c >> 8);
   }

   return c;
}

   
/*
 *----------------------------------------------------------------------
 *
 *  CRC_Compute --
 *
 *      computes the CRC of a block of data
 *
 * Results:
 *      
 *      CRC code
 *
 * Side effects:
 *      Sets up the crc table if it hasn't already been computed.
 *
 *----------------------------------------------------------------------
 */

uint32 
CRC_Compute(const uint8 *buf,
            int len)
{
   return UtilCRCUpdate(0xffffffffL, buf, len) ^ 0xffffffffL;
}

#endif /* defined (USERLEVEL) || defined(VMX86_DEBUG) */