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.

118 lines
2.6 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Specialized allocators.
  4. //
  5. // Copyright (C) Microsoft Corporation, 2001.
  6. //
  7. //----------------------------------------------------------------------------
  8. #include "pch.hpp"
  9. #ifdef NT_NATIVE
  10. #include "ntnative.h"
  11. #endif
  12. #include "cmnutil.hpp"
  13. #include "alloc.hpp"
  14. //----------------------------------------------------------------------------
  15. //
  16. // FixedSizeStackAllocator.
  17. //
  18. //----------------------------------------------------------------------------
  19. FixedSizeStackAllocator::FixedSizeStackAllocator(ULONG ChunkSize,
  20. ULONG ChunksPerBlock,
  21. BOOL KeepLastBlock)
  22. {
  23. C_ASSERT((sizeof(FixedSizeStackBlock) & 7) == 0);
  24. if (ChunkSize == 0)
  25. {
  26. ChunkSize = sizeof(ULONG64);
  27. }
  28. else
  29. {
  30. ChunkSize = (ChunkSize + 7) & ~7;
  31. }
  32. if (ChunksPerBlock == 0)
  33. {
  34. ChunksPerBlock = 32;
  35. }
  36. m_ChunkSize = ChunkSize;
  37. m_KeepLastBlock = KeepLastBlock;
  38. m_BlockSize = ChunkSize * ChunksPerBlock + sizeof(FixedSizeStackBlock);
  39. m_NumAllocs = 0;
  40. m_Blocks = NULL;
  41. }
  42. FixedSizeStackAllocator::~FixedSizeStackAllocator(void)
  43. {
  44. m_KeepLastBlock = FALSE;
  45. FreeAll();
  46. }
  47. void
  48. FixedSizeStackAllocator::FreeAll(void)
  49. {
  50. FixedSizeStackBlock* Block;
  51. while (m_Blocks)
  52. {
  53. Block = m_Blocks;
  54. if (!Block->Next && m_KeepLastBlock)
  55. {
  56. Block->MemLimit = (PUCHAR)(Block + 1);
  57. break;
  58. }
  59. m_Blocks = m_Blocks->Next;
  60. RawFree(Block);
  61. }
  62. m_NumAllocs = 0;
  63. }
  64. void*
  65. FixedSizeStackAllocator::RawAlloc(ULONG Bytes)
  66. {
  67. return malloc(Bytes);
  68. }
  69. void
  70. FixedSizeStackAllocator::RawFree(void* Mem)
  71. {
  72. free(Mem);
  73. }
  74. FixedSizeStackBlock*
  75. FixedSizeStackAllocator::AllocBlock(void)
  76. {
  77. FixedSizeStackBlock* Block = (FixedSizeStackBlock*)RawAlloc(m_BlockSize);
  78. if (!Block)
  79. {
  80. return NULL;
  81. }
  82. Block->Next = m_Blocks;
  83. Block->MemLimit = (PUCHAR)(Block + 1);
  84. m_Blocks = Block;
  85. return Block;
  86. }
  87. void
  88. FixedSizeStackAllocator::FreeBlock(void)
  89. {
  90. FixedSizeStackBlock* Block = m_Blocks;
  91. if (!Block->Next && m_KeepLastBlock)
  92. {
  93. // Sometimes it's desirable to keep the last block
  94. // around to avoid forcing an alloc the next time
  95. // the allocator is used.
  96. return;
  97. }
  98. m_Blocks = Block->Next;
  99. RawFree(Block);
  100. }