pool.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "pool.h"
  2. TMemoryPool::IGrowPolicy* TMemoryPool::TLinearGrow::Instance() noexcept {
  3. return SingletonWithPriority<TLinearGrow, 0>();
  4. }
  5. TMemoryPool::IGrowPolicy* TMemoryPool::TExpGrow::Instance() noexcept {
  6. return SingletonWithPriority<TExpGrow, 0>();
  7. }
  8. void TMemoryPool::AddChunk(size_t hint) {
  9. const size_t dataLen = Max(BlockSize_, hint);
  10. size_t allocSize = dataLen + sizeof(TChunk);
  11. if (Options_.RoundUpToNextPowerOfTwo) {
  12. allocSize = FastClp2(allocSize);
  13. }
  14. TBlock nb = Alloc_->Allocate(allocSize);
  15. // Add previous chunk's stats
  16. if (Current_ != &Empty_) {
  17. MemoryAllocatedBeforeCurrent_ += Current_->Used();
  18. MemoryWasteBeforeCurrent_ += Current_->Left();
  19. }
  20. BlockSize_ = GrowPolicy_->Next(dataLen);
  21. Current_ = new (nb.Data) TChunk(nb.Len - sizeof(TChunk));
  22. Chunks_.PushBack(Current_);
  23. }
  24. void TMemoryPool::DoClear(bool keepfirst) noexcept {
  25. while (!Chunks_.Empty()) {
  26. TChunk* c = Chunks_.PopBack();
  27. if (keepfirst && Chunks_.Empty()) {
  28. c->ResetChunk();
  29. Chunks_.PushBack(c);
  30. Current_ = c;
  31. BlockSize_ = c->BlockLength() - sizeof(TChunk);
  32. MemoryAllocatedBeforeCurrent_ = 0;
  33. MemoryWasteBeforeCurrent_ = 0;
  34. return;
  35. }
  36. TBlock b = {c, c->BlockLength()};
  37. c->~TChunk();
  38. Alloc_->Release(b);
  39. }
  40. Current_ = &Empty_;
  41. BlockSize_ = Origin_;
  42. MemoryAllocatedBeforeCurrent_ = 0;
  43. MemoryWasteBeforeCurrent_ = 0;
  44. }