Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
10 KiB

  1. // Ruler
  2. // 1 2 3 4 5 6 7 8
  3. //345678901234567890123456789012345678901234567890123456789012345678901234567890
  4. /********************************************************************/
  5. /* */
  6. /* The standard layout. */
  7. /* */
  8. /* The standard layout for 'cpp' files in this code is as */
  9. /* follows: */
  10. /* */
  11. /* 1. Include files. */
  12. /* 2. Constants local to the class. */
  13. /* 3. Data structures local to the class. */
  14. /* 4. Data initializations. */
  15. /* 5. Static functions. */
  16. /* 6. Class functions. */
  17. /* */
  18. /* The constructor is typically the first function, class */
  19. /* member functions appear in alphabetical order with the */
  20. /* destructor appearing at the end of the file. Any section */
  21. /* or function this is not required is simply omitted. */
  22. /* */
  23. /********************************************************************/
  24. #include "LibraryPCH.hpp"
  25. #include "Common.hpp"
  26. /********************************************************************/
  27. /* */
  28. /* Convert a divisor to a shift. */
  29. /* */
  30. /* We know that we can convert any divide operation into a */
  31. /* shift when the divisor is a power of two. This function */
  32. /* figures out whether we can do this and what the how far */
  33. /* we would need to shift. . */
  34. /* */
  35. /********************************************************************/
  36. BOOLEAN COMMON::ConvertDivideToShift( SBIT32 Divisor,SBIT32 *Shift )
  37. {
  38. if ( Divisor > 0 )
  39. {
  40. REGISTER SBIT32 Count;
  41. for ( Count=0;(Divisor & 1) == 0;Count ++ )
  42. { Divisor >>= 1; }
  43. if (Divisor == 1)
  44. {
  45. (*Shift) = Count;
  46. return True;
  47. }
  48. }
  49. return False;
  50. }
  51. /********************************************************************/
  52. /* */
  53. /* Force to the next power of two. */
  54. /* */
  55. /* We know that we can do certain optimizations if certain */
  56. /* values are a power of two. Here we force the issue by */
  57. /* rounding up the value to the next power of two. */
  58. /* */
  59. /********************************************************************/
  60. SBIT32 COMMON::ForceToPowerOfTwo( SBIT32 Value )
  61. {
  62. //
  63. // We ensure the value is positive if not we
  64. // simply return the identity value.
  65. //
  66. if ( Value > 1 )
  67. {
  68. //
  69. // We only have to compute the next power of
  70. // two if the value is not already a power
  71. // of two.
  72. //
  73. if ( ! PowerOfTwo( Value ) )
  74. {
  75. REGISTER SBIT32 Count;
  76. for ( Count=0;Value > 0;Count ++ )
  77. { Value >>= 1; }
  78. return (1 << Count);
  79. }
  80. else
  81. { return Value; }
  82. }
  83. else
  84. { return 1; }
  85. }
  86. /********************************************************************/
  87. /* */
  88. /* Convert to lower case. */
  89. /* */
  90. /* Convert all characters to lower case until we find the */
  91. /* end of the string. */
  92. /* */
  93. /********************************************************************/
  94. CHAR *COMMON::LowerCase( CHAR *Text )
  95. {
  96. REGISTER CHAR *Current = Text;
  97. for ( /* void */;(*Current) != '\0';Current ++ )
  98. {
  99. if ( isupper( (*Current) ) )
  100. { (*Current) = ((CHAR) tolower( (*Current) )); }
  101. }
  102. return Text;
  103. }
  104. /********************************************************************/
  105. /* */
  106. /* Convert to lower case. */
  107. /* */
  108. /* Convert a fixed number of characters to lower case. */
  109. /* */
  110. /********************************************************************/
  111. CHAR *COMMON::LowerCase( CHAR *Text,SBIT32 Size )
  112. {
  113. REGISTER CHAR *Current = Text;
  114. for ( /* void */;Size > 0;Current ++, Size -- )
  115. {
  116. if ( isupper( (*Current) ) )
  117. { (*Current) = ((CHAR) tolower( (*Current) )); }
  118. }
  119. return Text;
  120. }
  121. /********************************************************************/
  122. /* */
  123. /* Ensure value is a power of two. */
  124. /* */
  125. /* We need to ensure that certain values are an exact power */
  126. /* of two. If this is true then the value will be positive */
  127. /* and only 1 bit will be set. So we shift right until we */
  128. /* find the first bit on and then the value should be one. */
  129. /* */
  130. /********************************************************************/
  131. BOOLEAN COMMON::PowerOfTwo( SBIT32 Value )
  132. { return ((Value & (Value-1)) == 0); }
  133. #ifndef DISABLE_ATOMIC_FLAGS
  134. /********************************************************************/
  135. /* */
  136. /* Atomically set flags. */
  137. /* */
  138. /* We need to atomically set some flags to prevent them being */
  139. /* corrupted by concurrent updates. */
  140. /* */
  141. /********************************************************************/
  142. VOID COMMON::SetFlags( SBIT32 *CurrentFlags,SBIT32 NewFlags )
  143. {
  144. REGISTER SBIT32 StartFlags;
  145. REGISTER SBIT32 ResultFlags;
  146. do
  147. {
  148. StartFlags = (*CurrentFlags);
  149. ResultFlags =
  150. (
  151. AtomicCompareExchange
  152. (
  153. CurrentFlags,
  154. (StartFlags |= NewFlags),
  155. StartFlags
  156. )
  157. );
  158. }
  159. while ( StartFlags != ResultFlags );
  160. }
  161. /********************************************************************/
  162. /* */
  163. /* Atomically unset flags. */
  164. /* */
  165. /* We need to atomically unset some flags to prevent them being */
  166. /* corrupted by concurrent updates. */
  167. /* */
  168. /********************************************************************/
  169. VOID COMMON::UnsetFlags( SBIT32 *CurrentFlags,SBIT32 NewFlags )
  170. {
  171. REGISTER SBIT32 StartFlags;
  172. REGISTER SBIT32 ResultFlags;
  173. do
  174. {
  175. StartFlags = (*CurrentFlags);
  176. ResultFlags =
  177. (
  178. AtomicCompareExchange
  179. (
  180. CurrentFlags,
  181. (StartFlags &= ~NewFlags),
  182. StartFlags
  183. )
  184. );
  185. }
  186. while ( StartFlags != ResultFlags );
  187. }
  188. /********************************************************************/
  189. /* */
  190. /* Convert to upper case. */
  191. /* */
  192. /* Convert all characters to upper case until we find the */
  193. /* end of the string. */
  194. /* */
  195. /********************************************************************/
  196. CHAR *COMMON::UpperCase( CHAR *Text )
  197. {
  198. REGISTER CHAR *Current = Text;
  199. for ( /* void */;(*Current) != '\0';Current ++ )
  200. {
  201. if ( islower( (*Current) ) )
  202. { (*Current) = ((CHAR) toupper( (*Current) )); }
  203. }
  204. return Text;
  205. }
  206. /********************************************************************/
  207. /* */
  208. /* Convert to upper case. */
  209. /* */
  210. /* Convert a fixed number of characters to upper case. */
  211. /* */
  212. /********************************************************************/
  213. CHAR *COMMON::UpperCase( CHAR *Text,SBIT32 Size )
  214. {
  215. REGISTER CHAR *Current = Text;
  216. for ( /* void */;Size > 0;Current ++, Size -- )
  217. {
  218. if ( islower( (*Current) ) )
  219. { (*Current) = ((CHAR) toupper( (*Current) )); }
  220. }
  221. return Text;
  222. }
  223. #endif