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.

173 lines
3.6 KiB

  1. /*++
  2. Copyright(c) 1999-2000 Microsoft Corporation
  3. Module Name:
  4. brdgcach.h
  5. Abstract:
  6. Ethernet MAC level bridge.
  7. Cache implementation header
  8. Author:
  9. Mark Aiken
  10. Environment:
  11. Kernel mode driver
  12. Revision History:
  13. December 2000 - Original version
  14. --*/
  15. // ===========================================================================
  16. //
  17. // DECLARATIONS
  18. //
  19. // ===========================================================================
  20. typedef struct _CACHE_ENTRY
  21. {
  22. UINT32 key;
  23. UINT32 data;
  24. UINT64 hits;
  25. UINT64 misses;
  26. } CACHE_ENTRY, *PCACHE_ENTRY;
  27. typedef struct _CACHE
  28. {
  29. // lock protects all cache fields
  30. NDIS_SPIN_LOCK lock;
  31. // stats
  32. UINT64 hits;
  33. UINT64 misses;
  34. // 2^shiftFactor is the number of entries
  35. USHORT shiftFactor;
  36. // Pointer to the array of entries
  37. PCACHE_ENTRY pEntries;
  38. } CACHE, *PCACHE;
  39. //
  40. // Determines the cache slot for key k in cache c. The slot is determined
  41. // as the bottom bits of k.
  42. //
  43. #define CACHE_INDEX(c, k) (k & ((1 << c->shiftFactor) - 1))
  44. // ===========================================================================
  45. //
  46. // INLINES
  47. //
  48. // ===========================================================================
  49. __inline
  50. VOID
  51. BrdgClearCache(
  52. IN PCACHE pCache
  53. )
  54. {
  55. NdisAcquireSpinLock( &pCache->lock );
  56. memset( pCache->pEntries, 0, sizeof(CACHE_ENTRY) * (1 << pCache->shiftFactor) );
  57. NdisReleaseSpinLock( &pCache->lock );
  58. }
  59. __inline
  60. NDIS_STATUS
  61. BrdgInitializeCache(
  62. IN PCACHE pCache,
  63. IN USHORT shiftFactor
  64. )
  65. {
  66. ULONG numEntries = 1 << shiftFactor;
  67. NDIS_STATUS status;
  68. pCache->shiftFactor = shiftFactor;
  69. NdisAllocateSpinLock( &pCache->lock );
  70. status = NdisAllocateMemoryWithTag( &pCache->pEntries, sizeof(CACHE_ENTRY) * numEntries, 'gdrB' );
  71. pCache->hits = 0L;
  72. pCache->misses = 0L;
  73. if( status != NDIS_STATUS_SUCCESS )
  74. {
  75. return status;
  76. }
  77. // Zero out the array of entries
  78. memset( pCache->pEntries, 0, sizeof(CACHE_ENTRY) * (1 << pCache->shiftFactor) );
  79. return NDIS_STATUS_SUCCESS;
  80. }
  81. __inline
  82. VOID
  83. BrdgFreeCache(
  84. IN PCACHE pCache
  85. )
  86. {
  87. NdisFreeMemory( pCache->pEntries, sizeof(CACHE_ENTRY) * (1 << pCache->shiftFactor), 0 );
  88. }
  89. __inline
  90. UINT32
  91. BrdgProbeCache(
  92. IN PCACHE pCache,
  93. IN UINT32 key
  94. )
  95. {
  96. UINT32 index = CACHE_INDEX(pCache, key);
  97. PCACHE_ENTRY pEntry = &pCache->pEntries[index];
  98. UINT32 data = 0L;
  99. NdisAcquireSpinLock( &pCache->lock );
  100. if( pEntry->key == key )
  101. {
  102. data = pEntry->data;
  103. pEntry->hits++;
  104. pCache->hits++;
  105. }
  106. else
  107. {
  108. pEntry->misses++;
  109. pCache->misses++;
  110. }
  111. NdisReleaseSpinLock( &pCache->lock );
  112. return data;
  113. }
  114. __inline
  115. BOOLEAN
  116. BrdgUpdateCache(
  117. IN PCACHE pCache,
  118. IN UINT32 key,
  119. IN UINT32 data
  120. )
  121. {
  122. UINT32 index = CACHE_INDEX(pCache, key);
  123. PCACHE_ENTRY pEntry = &pCache->pEntries[index];
  124. BOOLEAN bUpdated = FALSE;
  125. NdisAcquireSpinLock( &pCache->lock );
  126. if( pEntry->key != key &&
  127. (pEntry->hits < pEntry->misses) )
  128. {
  129. pEntry->key = key;
  130. pEntry->data = data;
  131. pEntry->hits = 0L;
  132. pEntry->misses = 0L;
  133. bUpdated = TRUE;
  134. }
  135. NdisReleaseSpinLock( &pCache->lock );
  136. return bUpdated;
  137. }