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.

106 lines
4.7 KiB

  1. // --------------------------------------------------------------------------------
  2. // MemCache.h
  3. // Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  4. // --------------------------------------------------------------------------------
  5. #ifndef __MEMCACHE_H
  6. #define __MEMCACHE_H
  7. // --------------------------------------------------------------------------------
  8. // Forward Decls
  9. // --------------------------------------------------------------------------------
  10. class CMemoryCache;
  11. typedef CMemoryCache *LPMEMORYCACHE;
  12. // --------------------------------------------------------------------------------
  13. // CACHECELLS
  14. // --------------------------------------------------------------------------------
  15. #define CACHECELLS 20480
  16. // --------------------------------------------------------------------------------
  17. // MEMCACHECELL
  18. // --------------------------------------------------------------------------------
  19. typedef struct tagMEMCACHECELL {
  20. #ifdef DEBUG
  21. ULONG cFree;
  22. ULONG cAlloc;
  23. #endif
  24. LPVOID pvItemHead; // Pointer to first block
  25. } MEMCACHECELL, *LPMEMCACHECELL;
  26. // --------------------------------------------------------------------------------
  27. // Memory Cache Debug Metrics
  28. // --------------------------------------------------------------------------------
  29. #ifdef DEBUG
  30. typedef struct tagMEMCACHEMETRIC {
  31. ULONG cAlloc;
  32. ULONG cAllocCache;
  33. ULONG cbAlloc;
  34. ULONG cbAllocCache;
  35. ULONG cFree;
  36. ULONG cbFree;
  37. ULONG cbCacheMax;
  38. ULONG cFreeFull;
  39. ULONG cLookAhead;
  40. ULONG cMostAlloc;
  41. ULONG cMostFree;
  42. ULONG cbMostAlloc;
  43. ULONG cbMostFree;
  44. } MEMCACHEMETRIC, *LPMEMCACHEMETRIC;
  45. #define INCMETRIC(_member, _amount) (m_rMetric.##_member += _amount)
  46. #else // DEBUG
  47. #define INCMETRIC(_member, _amount) 1 ? (void)0 : (void)
  48. #endif // DEBUG
  49. // --------------------------------------------------------------------------------
  50. // MEMCACHEITEM
  51. // --------------------------------------------------------------------------------
  52. typedef struct tagMEMCACHEITEM {
  53. LPVOID pvItemNext; // Pointer to next block of same size
  54. } MEMCACHEITEM, *LPMEMCACHEITEM;
  55. // --------------------------------------------------------------------------------
  56. // CMemoryCache
  57. // --------------------------------------------------------------------------------
  58. class CMemoryCache : public IMalloc
  59. {
  60. public:
  61. // ----------------------------------------------------------------------------
  62. // Construction
  63. // ----------------------------------------------------------------------------
  64. CMemoryCache(IMalloc *pMalloc, ULONG cbMin=0, ULONG cbCacheMax=131072);
  65. ~CMemoryCache(void);
  66. // ----------------------------------------------------------------------------
  67. // IUnknown Members
  68. // ----------------------------------------------------------------------------
  69. STDMETHODIMP QueryInterface(REFIID riid, LPVOID *ppv) { return TrapError(E_NOTIMPL); }
  70. STDMETHODIMP_(ULONG) AddRef(void);
  71. STDMETHODIMP_(ULONG) Release(void);
  72. // ----------------------------------------------------------------------------
  73. // CMemoryCache Members
  74. // ----------------------------------------------------------------------------
  75. STDMETHODIMP_(LPVOID) Alloc(ULONG cb);
  76. STDMETHODIMP_(LPVOID) Realloc(LPVOID pv, ULONG cb);
  77. STDMETHODIMP_(VOID) Free(LPVOID pv);
  78. STDMETHODIMP_(VOID) HeapMinimize(void);
  79. STDMETHODIMP_(INT) DidAlloc(LPVOID pv) { return(m_pMalloc->DidAlloc(pv)); }
  80. STDMETHODIMP_(ULONG) GetSize(LPVOID pv) { return(m_pMalloc->GetSize(pv)); }
  81. private:
  82. // ----------------------------------------------------------------------------
  83. // Private Data
  84. // ----------------------------------------------------------------------------
  85. ULONG m_cRef; // Reference Count
  86. ULONG m_cbMin; // Smallest size buffer to cache
  87. ULONG m_cbCacheMax; // Maximum size of the cache
  88. ULONG m_cbCacheCur; // Current Size of the cache
  89. IMalloc *m_pMalloc; // Memory Allocator
  90. MEMCACHECELL m_rgCell[CACHECELLS]; // Array of pointers to cell chains
  91. CRITICAL_SECTION m_cs; // Critical Section
  92. #ifdef DEBUG
  93. MEMCACHEMETRIC m_rMetric; // Debug Stats on Cache Usage
  94. #endif
  95. };
  96. #endif // __MEMCACHE_H