PageAllocatorPool.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. class PageAllocatorPool
  7. {
  8. friend class AutoReturnPageAllocator;
  9. public:
  10. PageAllocatorPool();
  11. ~PageAllocatorPool();
  12. void RemoveAll();
  13. static void Initialize();
  14. static void Shutdown();
  15. static void IdleCleanup();
  16. private:
  17. static VOID CALLBACK IdleCleanupRoutine(
  18. _Inout_ PTP_CALLBACK_INSTANCE Instance,
  19. _Inout_opt_ PVOID Context,
  20. _Inout_ PTP_TIMER Timer);
  21. PageAllocator* GetPageAllocator();
  22. void ReturnPageAllocator(PageAllocator* pageAllocator);
  23. unsigned int GetInactivePageAllocatorCount();
  24. SList<PageAllocator*, NoThrowHeapAllocator, RealCount> pageAllocators;
  25. static CriticalSection cs;
  26. static PageAllocatorPool* Instance;
  27. PTP_TIMER idleCleanupTimer;
  28. volatile unsigned long long activePageAllocatorCount;
  29. };
  30. class AutoReturnPageAllocator
  31. {
  32. public:
  33. AutoReturnPageAllocator() :pageAllocator(nullptr) {}
  34. ~AutoReturnPageAllocator()
  35. {
  36. if (pageAllocator)
  37. {
  38. PageAllocatorPool::Instance->ReturnPageAllocator(pageAllocator);
  39. }
  40. }
  41. PageAllocator* GetPageAllocator()
  42. {
  43. if (pageAllocator == nullptr)
  44. {
  45. pageAllocator = PageAllocatorPool::Instance->GetPageAllocator();
  46. }
  47. return pageAllocator;
  48. }
  49. private:
  50. PageAllocator* pageAllocator;
  51. };