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.

137 lines
3.4 KiB

  1. //
  2. // memcache.h
  3. //
  4. #ifndef MEMCACHE_H
  5. #define MEMCACHE_H
  6. #include "mem.h"
  7. #include "globals.h"
  8. extern LONG g_lMemCacheMutex;
  9. typedef struct _MEMCACHE
  10. {
  11. ULONG uMaxPtrs;
  12. ULONG iNextFree;
  13. void *rgPtrs[1]; // 1 or more...
  14. } MEMCACHE;
  15. MEMCACHE *MemCache_New(ULONG uMaxPtrs);
  16. void MemCache_Delete(MEMCACHE *pMemCache);
  17. //+---------------------------------------------------------------------------
  18. //
  19. // MemCache_Add
  20. //
  21. //----------------------------------------------------------------------------
  22. inline BOOL MemCache_Add(MEMCACHE *pMemCache, void *pv)
  23. {
  24. BOOL fRet = FALSE;
  25. if (InterlockedIncrement(&g_lMemCacheMutex) != 0)
  26. goto Exit;
  27. if (pMemCache->iNextFree >= pMemCache->uMaxPtrs)
  28. goto Exit; // cache is full!
  29. pMemCache->rgPtrs[pMemCache->iNextFree++] = pv;
  30. fRet = TRUE;
  31. Exit:
  32. InterlockedDecrement(&g_lMemCacheMutex);
  33. return fRet;
  34. }
  35. //+---------------------------------------------------------------------------
  36. //
  37. // MemCache_Remove
  38. //
  39. //----------------------------------------------------------------------------
  40. inline void *MemCache_Remove(MEMCACHE *pMemCache)
  41. {
  42. void *pv = NULL;
  43. if (InterlockedIncrement(&g_lMemCacheMutex) != 0)
  44. goto Exit;
  45. if (pMemCache->iNextFree == 0)
  46. goto Exit;
  47. pv = pMemCache->rgPtrs[--pMemCache->iNextFree];
  48. Exit:
  49. InterlockedDecrement(&g_lMemCacheMutex);
  50. return pv;
  51. }
  52. //+---------------------------------------------------------------------------
  53. //
  54. // MemCache_NewOp
  55. //
  56. //----------------------------------------------------------------------------
  57. #ifdef DEBUG
  58. inline void *MemCache_NewOp(MEMCACHE *pMemCache, size_t nSize, const TCHAR *pszFile, int iLine)
  59. #else
  60. inline void *MemCache_NewOp(MEMCACHE *pMemCache, size_t nSize)
  61. #endif
  62. {
  63. void *pv = NULL;
  64. if (pMemCache != NULL)
  65. {
  66. if (pv = MemCache_Remove(pMemCache))
  67. { // Issue: update debug mem track info
  68. memset(pv, 0, nSize);
  69. }
  70. }
  71. if (pv == NULL)
  72. {
  73. #ifdef DEBUG
  74. pv = Dbg_MemAllocClear(nSize, pszFile, iLine);
  75. #else
  76. pv = cicMemAllocClear(nSize);
  77. #endif
  78. }
  79. return pv;
  80. }
  81. //+---------------------------------------------------------------------------
  82. //
  83. // MemCache_DeleteOp
  84. //
  85. //----------------------------------------------------------------------------
  86. inline void MemCache_DeleteOp(MEMCACHE *pMemCache, void *pv)
  87. {
  88. if (pMemCache == NULL ||
  89. !MemCache_Add(pMemCache, pv))
  90. {
  91. cicMemFree(pv);
  92. }
  93. }
  94. #ifdef DEBUG
  95. #define DECLARE_CACHED_NEW \
  96. void *operator new(size_t nSize, const TCHAR *pszFile, int iLine) { return MemCache_NewOp(_s_pMemCache, nSize, pszFile, iLine); } \
  97. void operator delete(void *pv) { MemCache_DeleteOp(_s_pMemCache, pv); } \
  98. static _MEMCACHE *_s_pMemCache;
  99. #else // !DEBUG
  100. #define DECLARE_CACHED_NEW \
  101. void *operator new(size_t nSize) { return MemCache_NewOp(_s_pMemCache, nSize); } \
  102. void operator delete(void *pv) { MemCache_DeleteOp(_s_pMemCache, pv); } \
  103. static _MEMCACHE *_s_pMemCache;
  104. #endif
  105. #define DECLARE_CACHED_NEW_STATIC(the_class) \
  106. MEMCACHE *the_class::_s_pMemCache = NULL;
  107. #endif // MEMCACHE_H