stack_pool.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #pragma once
  2. #include "stack.h"
  3. #include "stack_common.h"
  4. #include <util/generic/noncopyable.h>
  5. #include <util/generic/ptr.h>
  6. #include <util/generic/vector.h>
  7. namespace NCoro::NStack {
  8. class IGuard;
  9. class TStorage;
  10. struct TPoolAllocatorSettings;
  11. template<typename TGuard>
  12. class TPool final : private TMoveOnly {
  13. struct TMemory {
  14. char* Raw = nullptr;
  15. char* Aligned = nullptr; // points to aligned memory, which includes space for first page guard
  16. };
  17. public:
  18. TPool(size_t stackSize, const TPoolAllocatorSettings& settings, const TGuard& guard);
  19. TPool(TPool&& other) noexcept;
  20. ~TPool();
  21. NDetails::TStack AllocStack(const char* name);
  22. void FreeStack(NDetails::TStack& stack);
  23. size_t GetReleasedSize() const noexcept;
  24. size_t GetFullSize() const noexcept;
  25. size_t GetNumOfAllocated() const noexcept { return NumOfAllocated_; }
  26. private:
  27. void AllocNewMemoryChunk();
  28. bool IsSmallStack() const noexcept;
  29. bool IsStackFromThisPool(const NDetails::TStack& stack) const noexcept;
  30. NDetails::TStack AllocNewStack(const char* name);
  31. private:
  32. const size_t StackSize_ = 0;
  33. size_t RssPagesToKeep_ = 0;
  34. const TGuard& Guard_;
  35. TVector<TMemory> Memory_; // memory chunks
  36. THolder<TStorage> Storage_;
  37. char* NextToAlloc_ = nullptr; // points to next available stack in the last memory chunk
  38. const size_t ChunkSize_ = 0;
  39. size_t NumOfAllocated_ = 0;
  40. };
  41. }
  42. #include "stack_pool.inl"