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.

274 lines
9.2 KiB

  1. #ifndef _MAP_HPP_
  2. #define _MAP_HPP_
  3. // Ruler
  4. // 1 2 3 4 5 6 7 8
  5. //345678901234567890123456789012345678901234567890123456789012345678901234567890
  6. /********************************************************************/
  7. /* */
  8. /* The standard layout. */
  9. /* */
  10. /* The standard layout for 'hpp' files for this code is as */
  11. /* follows: */
  12. /* */
  13. /* 1. Include files. */
  14. /* 2. Constants exported from the class. */
  15. /* 3. Data structures exported from the class. */
  16. /* 4. Forward references to other data structures. */
  17. /* 5. Class specifications (including inline functions). */
  18. /* 6. Additional large inline functions. */
  19. /* */
  20. /* Any portion that is not required is simply omitted. */
  21. /* */
  22. /********************************************************************/
  23. #include "Global.hpp"
  24. #include "Lock.hpp"
  25. #include "Stack.hpp"
  26. #include "Vector.hpp"
  27. /********************************************************************/
  28. /* */
  29. /* Constants exported from the class. */
  30. /* */
  31. /* The map constants specify the initial size of the map. */
  32. /* */
  33. /********************************************************************/
  34. CONST SBIT32 MapSize = 128;
  35. /********************************************************************/
  36. /* */
  37. /* Maps and map management. */
  38. /* */
  39. /* This class provides general purpose mapping functions to */
  40. /* safely convert handles into some pointer or value. */
  41. /* */
  42. /********************************************************************/
  43. template <class TYPE,class LOCK=NO_LOCK> class MAP : public LOCK
  44. {
  45. //
  46. // Private structures.
  47. //
  48. typedef struct
  49. {
  50. BOOLEAN Available;
  51. TYPE Value;
  52. }
  53. VALUE;
  54. //
  55. // Private data.
  56. //
  57. SBIT32 MaxMap;
  58. SBIT32 MapUsed;
  59. STACK<SBIT32> FreeStack;
  60. VECTOR<VALUE> Map;
  61. public:
  62. //
  63. // Public functions.
  64. //
  65. MAP( SBIT32 NewMaxMap = MapSize, SBIT32 Alignment = 1 );
  66. SBIT32 NewHandle( CONST TYPE & Value );
  67. BOOLEAN FindHandle
  68. (
  69. SBIT32 Handle,
  70. TYPE *Value
  71. );
  72. VOID DeleteHandle( SBIT32 Handle );
  73. ~MAP( VOID );
  74. //
  75. // Public inline functions.
  76. //
  77. INLINE SBIT32 MaxHandles( VOID )
  78. { return MapUsed; }
  79. private:
  80. //
  81. // Disabled operations.
  82. //
  83. MAP( CONST MAP & Copy );
  84. VOID operator=( CONST MAP & Copy );
  85. };
  86. /********************************************************************/
  87. /* */
  88. /* Class constructor. */
  89. /* */
  90. /* Create a new map and prepare it for use. This call is */
  91. /* not thread safe and should only be made in a single thread */
  92. /* environment. */
  93. /* */
  94. /********************************************************************/
  95. template <class TYPE,class LOCK> MAP<TYPE,LOCK>::MAP
  96. (
  97. SBIT32 NewMaxMap,
  98. SBIT32 Alignment
  99. ) :
  100. //
  101. // Call the constructors for the contained classes.
  102. //
  103. FreeStack( NewMaxMap ),
  104. Map( NewMaxMap,Alignment,CacheLineSize )
  105. {
  106. #ifdef DEBUGGING
  107. if ( NewMaxMap > 0 )
  108. {
  109. #endif
  110. MaxMap = NewMaxMap;
  111. MapUsed = 0;
  112. #ifdef DEBUGGING
  113. }
  114. else
  115. { Failure( "Max map size in constructor for MAP" ); }
  116. #endif
  117. }
  118. /********************************************************************/
  119. /* */
  120. /* Create a new handle. */
  121. /* */
  122. /* We create a new handle for the supplied value. */
  123. /* */
  124. /********************************************************************/
  125. template <class TYPE,class LOCK> SBIT32 MAP<TYPE,LOCK>::NewHandle
  126. (
  127. CONST TYPE & Value
  128. )
  129. {
  130. AUTO SBIT32 Handle;
  131. REGISTER VALUE *NewValue;
  132. //
  133. // Claim an exclusive lock (if enabled).
  134. //
  135. ClaimExclusiveLock();
  136. //
  137. // Expand the mapping table if it is too
  138. // small be resizing it.
  139. //
  140. if ( ! FreeStack.PopStack( & Handle ) )
  141. {
  142. if ( (Handle = MapUsed ++) >= MaxMap )
  143. { Map.Resize( (MaxMap *= ExpandStore) ); }
  144. }
  145. //
  146. // Link in the new mapping.
  147. //
  148. NewValue = & Map[ Handle ];
  149. NewValue -> Available = True;
  150. NewValue -> Value = Value;
  151. //
  152. // Release any lock claimed earlier.
  153. //
  154. ReleaseExclusiveLock();
  155. return Handle;
  156. }
  157. /********************************************************************/
  158. /* */
  159. /* Find the value associated with a handle. */
  160. /* */
  161. /* We find the value associated with the supplied handle. */
  162. /* */
  163. /********************************************************************/
  164. template <class TYPE,class LOCK> BOOLEAN MAP<TYPE,LOCK>::FindHandle
  165. (
  166. SBIT32 Handle,
  167. TYPE *Value
  168. )
  169. {
  170. REGISTER BOOLEAN Result;
  171. //
  172. // Claim a shared lock (if enabled).
  173. //
  174. ClaimSharedLock();
  175. //
  176. // Find the handle and return the associated value.
  177. //
  178. Result =
  179. (
  180. ((Handle >= 0) && (Handle < MaxMap))
  181. &&
  182. (Map[ Handle ].Available)
  183. );
  184. if ( Result )
  185. { (*Value) = Map[ Handle ].Value; }
  186. //
  187. // Release any lock claimed earlier.
  188. //
  189. ReleaseSharedLock();
  190. return Result;
  191. }
  192. /********************************************************************/
  193. /* */
  194. /* Delete a handle. */
  195. /* */
  196. /* We delete the supplied handle and the associated value. */
  197. /* */
  198. /********************************************************************/
  199. template <class TYPE,class LOCK> VOID MAP<TYPE,LOCK>::DeleteHandle
  200. (
  201. SBIT32 Handle
  202. )
  203. {
  204. //
  205. // Claim an exclusive lock (if enabled).
  206. //
  207. ClaimExclusiveLock();
  208. //
  209. // If the handle is valid then delete any
  210. // associated value.
  211. //
  212. if ( (Handle >= 0) && (Handle < MaxMap) && (Map[ Handle ].Available) )
  213. {
  214. Map[ Handle ].Available = False;
  215. FreeStack.PushStack( Handle );
  216. }
  217. else
  218. { Failure( "No mapping in DeleteMapHandle()" ); }
  219. //
  220. // Release any lock claimed earlier.
  221. //
  222. ReleaseExclusiveLock();
  223. }
  224. /********************************************************************/
  225. /* */
  226. /* Class destructor. */
  227. /* */
  228. /* Destory the map. This call is not thread safe and should */
  229. /* only be made in a single thread environment. */
  230. /* */
  231. /********************************************************************/
  232. template <class TYPE,class LOCK> MAP<TYPE,LOCK>::~MAP( VOID )
  233. { /* void */ }
  234. #endif