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.

115 lines
2.7 KiB

  1. /*++
  2. Copyright (c) 2000-2001 Microsoft Corporation
  3. Module Name:
  4. bsfixalloc.h
  5. Abstract:
  6. Adapted rom MFC fixalloc.h
  7. Author:
  8. Stefan R. Steiner [ssteiner] 04-10-2000
  9. Revision History:
  10. --*/
  11. #ifndef __H_BSFIXALLOC_
  12. #define __H_BSFIXALLOC_
  13. // fixalloc.h - declarations for fixed block allocator
  14. #pragma pack(push, 8)
  15. struct CBsPlex // warning variable length structure
  16. {
  17. CBsPlex* pNext;
  18. DWORD dwReserved[1]; // align on 8 byte boundary
  19. // BYTE data[maxNum*elementSize];
  20. void* data() { return this+1; }
  21. static CBsPlex* PASCAL Create(CBsPlex*& head, UINT nMax, UINT cbElement);
  22. // like 'calloc' but no zero fill
  23. // may throw memory exceptions
  24. void FreeDataChain(); // free this one and links
  25. };
  26. #pragma pack(pop)
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CBsFixedAlloc
  29. class CBsFixedAlloc
  30. {
  31. // Constructors
  32. public:
  33. CBsFixedAlloc(UINT nAllocSize, UINT nBlockSize = 64);
  34. // Attributes
  35. UINT GetAllocSize() { return m_nAllocSize; }
  36. // Operations
  37. public:
  38. void* Alloc(); // return a chunk of memory of nAllocSize
  39. void Free(void* p); // free chunk of memory returned from Alloc
  40. void FreeAll(); // free everything allocated from this allocator
  41. // Implementation
  42. public:
  43. ~CBsFixedAlloc();
  44. protected:
  45. struct CNode
  46. {
  47. CNode* pNext; // only valid when in free list
  48. };
  49. UINT m_nAllocSize; // size of each block from Alloc
  50. UINT m_nBlockSize; // number of blocks to get at a time
  51. CBsPlex* m_pBlocks; // linked list of blocks (is nBlocks*nAllocSize)
  52. CNode* m_pNodeFree; // first free node (NULL if no free nodes)
  53. CRITICAL_SECTION m_protect;
  54. };
  55. #ifndef _DEBUG
  56. // DECLARE_FIXED_ALLOC -- used in class definition
  57. #define DECLARE_FIXED_ALLOC(class_name) \
  58. public: \
  59. void* operator new(size_t size) \
  60. { \
  61. ASSERT(size == s_alloc.GetAllocSize()); \
  62. UNUSED(size); \
  63. return s_alloc.Alloc(); \
  64. } \
  65. void* operator new(size_t, void* p) \
  66. { return p; } \
  67. void operator delete(void* p) { s_alloc.Free(p); } \
  68. void* operator new(size_t size, LPCSTR, int) \
  69. { \
  70. ASSERT(size == s_alloc.GetAllocSize()); \
  71. UNUSED(size); \
  72. return s_alloc.Alloc(); \
  73. } \
  74. protected: \
  75. static CBsFixedAlloc s_alloc \
  76. // IMPLEMENT_FIXED_ALLOC -- used in class implementation file
  77. #define IMPLEMENT_FIXED_ALLOC(class_name, block_size) \
  78. CBsFixedAlloc class_name::s_alloc(sizeof(class_name), block_size) \
  79. #else //!_DEBUG
  80. #define DECLARE_FIXED_ALLOC(class_name) // nothing in debug
  81. #define IMPLEMENT_FIXED_ALLOC(class_name, block_size) // nothing in debug
  82. #endif //!_DEBUG
  83. #endif // __H_BSFIXALLOC_