Source code of Windows XP (NT5)
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.

197 lines
6.0 KiB

  1. /*===================================================================
  2. Microsoft Denali
  3. Microsoft Confidential.
  4. Copyright 1996 Microsoft Corporation. All Rights Reserved.
  5. Component: Per-Class Memory Management
  6. File: Memcls.h
  7. Owner: dmitryr
  8. This file contains #defines to access ATQ memory cache on per
  9. class basis
  10. ===================================================================*/
  11. #ifndef MEMCLS_H
  12. #define MEMCLS_H
  13. // ATQ memory cache
  14. #include <acache.hxx>
  15. // To resolve Assert()
  16. #include "debug.h"
  17. // Prototypes
  18. HRESULT InitMemCls();
  19. HRESULT UnInitMemCls();
  20. /*===================================================================
  21. M A C R O S to make a class use ACACHE allocator
  22. ===================================================================*/
  23. /*===================================================================
  24. I n s t r u c t i o n s
  25. To add a class named CFoo to the per-class basis follow these
  26. four simple steps:
  27. 1) Inside the class definition include ACACHE_INCLASS_DEFINITIONS():
  28. class CFoo
  29. {
  30. ...
  31. ACACHE_INCLASS_DEFINITIONS() // <-add this line
  32. };
  33. 2) In a source file add the ACACHE_CODE macro outside
  34. any function body:
  35. ACACHE_CODE(CFoo) // <-add this line
  36. 3) In a DLL initialization routine add ACACHE_INIT macro:
  37. ACACHE_INIT(CFoo, 13, hr) // <-add this line
  38. where 13 is the threshold. Use the desired number instead.
  39. 4) In a DLL uninitialization routine add ACACHE_UNINIT macro:
  40. ACACHE_UNINIT(CFoo) // <-add this line
  41. ===================================================================*/
  42. /*
  43. The following macro should be used inside class definition to
  44. enable per-class caching.
  45. The second operator new is needed in case memchk.h [#define]'s new
  46. to this expanded form.
  47. */
  48. #define ACACHE_INCLASS_DEFINITIONS() \
  49. public: \
  50. static void * operator new(size_t); \
  51. static void * operator new(size_t, const char *, int); \
  52. static void operator delete(void *); \
  53. static ALLOC_CACHE_HANDLER *sm_pach;
  54. /*
  55. The following macro should be used once per class in a source
  56. file outside of any functions. The argument is the class name.
  57. */
  58. #define ACACHE_CODE(C) \
  59. ALLOC_CACHE_HANDLER *C::sm_pach; \
  60. void *C::operator new(size_t s) \
  61. { Assert(s == sizeof(C)); Assert(sm_pach); \
  62. return sm_pach->Alloc(); } \
  63. void *C::operator new(size_t s, const char *, int) \
  64. { Assert(s == sizeof(C)); Assert(sm_pach); \
  65. return sm_pach->Alloc(); } \
  66. void C::operator delete(void *pv) \
  67. { Assert(pv); if (sm_pach) sm_pach->Free(pv); }
  68. /*
  69. The following macro should be used once per class in the
  70. DLL initialization routine.
  71. Arguments: class name, cache size, HRESULT var name
  72. */
  73. #define ACACHE_INIT(C, T, hr) \
  74. { if (SUCCEEDED(hr)) { Assert(!C::sm_pach); \
  75. ALLOC_CACHE_CONFIGURATION acc = { 1, T, sizeof(C) }; \
  76. C::sm_pach = new ALLOC_CACHE_HANDLER("ASP:" #C, &acc); \
  77. hr = C::sm_pach ? S_OK : E_OUTOFMEMORY; } }
  78. #define ACACHE_INIT_EX(C, T, F, hr) \
  79. { if (SUCCEEDED(hr)) { Assert(!C::sm_pach); \
  80. ALLOC_CACHE_CONFIGURATION acc = { 1, T, sizeof(C) }; \
  81. C::sm_pach = new ALLOC_CACHE_HANDLER("ASP:" #C, &acc, F); \
  82. hr = C::sm_pach ? S_OK : E_OUTOFMEMORY; } }
  83. /*
  84. The following macro should be used once per class in the
  85. DLL uninitialization routine. The argument is the class name.
  86. */
  87. #define ACACHE_UNINIT(C) \
  88. { if (C::sm_pach) { delete C::sm_pach; C::sm_pach = NULL; } }
  89. /*===================================================================
  90. M A C R O S to create a fixes size allocator
  91. ===================================================================*/
  92. /*===================================================================
  93. I n s t r u c t i o n s
  94. To add a fixed size allocator for 1K buffers named Foo
  95. to the code follow these simple steps:
  96. 1) In a header file include extern definition
  97. ACACHE_FSA_EXTERN(Foo)
  98. 2) In a source file the actual definition outside
  99. any function body:
  100. ACACHE_FSA_DEFINITION(Foo)
  101. 3) In a DLL initialization routine add INIT macro:
  102. ACACHE_FSA_INIT(Foo, 1024, 13, hr)
  103. where 1024 is the size and 13 is the threshold.
  104. Use the desired numbers instead.
  105. 4) In a DLL uninitialization routine add UNINIT macro:
  106. ACACHE_FSA_UNINIT(CFoo)
  107. 5) To allocate, do:
  108. void *pv = ACACHE_FSA_ALLOC(Foo)
  109. 6) To free, do:
  110. ACACHE_FSA_FREE(Foo, pv)
  111. ===================================================================*/
  112. #define ACACHE_FSA_EXTERN(C) \
  113. extern ALLOC_CACHE_HANDLER *g_pach##C;
  114. #define ACACHE_FSA_DEFINITION(C) \
  115. ALLOC_CACHE_HANDLER *g_pach##C = NULL;
  116. #define ACACHE_FSA_INIT(C, S, T, hr) \
  117. { if (SUCCEEDED(hr)) { Assert(!g_pach##C); \
  118. ALLOC_CACHE_CONFIGURATION acc = { 1, T, S }; \
  119. g_pach##C = new ALLOC_CACHE_HANDLER("ASP:" #C, &acc); \
  120. hr = g_pach##C ? S_OK : E_OUTOFMEMORY; } }
  121. #define ACACHE_FSA_UNINIT(C) \
  122. { if (g_pach##C) { delete g_pach##C; g_pach##C = NULL; } }
  123. #define ACACHE_FSA_ALLOC(C) \
  124. ( g_pach##C ? g_pach##C->Alloc() : NULL )
  125. #define ACACHE_FSA_FREE(C, pv) \
  126. { Assert(pv); if (g_pach##C) g_pach##C->Free(pv); }
  127. #endif // MEMCLS_H