chunked_memory_allocator.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "chunked_memory_allocator.h"
  2. #include "serialize.h"
  3. namespace NYT {
  4. ////////////////////////////////////////////////////////////////////////////////
  5. const i64 TChunkedMemoryAllocator::DefaultChunkSize = 4096;
  6. const double TChunkedMemoryAllocator::DefaultMaxSmallBlockSizeRatio = 0.25;
  7. ////////////////////////////////////////////////////////////////////////////////
  8. TChunkedMemoryAllocator::TChunkedMemoryAllocator(
  9. i64 chunkSize,
  10. double maxSmallBlockSizeRatio,
  11. TRefCountedTypeCookie tagCookie)
  12. : ChunkSize_(chunkSize)
  13. , MaxSmallBlockSize_(static_cast<i64>(ChunkSize_ * maxSmallBlockSizeRatio))
  14. , TagCookie_(tagCookie)
  15. { }
  16. TSharedMutableRef TChunkedMemoryAllocator::AllocateUnalignedSlow(i64 size)
  17. {
  18. auto large = AllocateSlowCore(size);
  19. if (large) {
  20. return large;
  21. }
  22. return AllocateUnaligned(size);
  23. }
  24. TSharedMutableRef TChunkedMemoryAllocator::AllocateAlignedSlow(i64 size, int align)
  25. {
  26. // NB: Do not rely on any particular alignment of chunks.
  27. auto large = AllocateSlowCore(size + align);
  28. if (large) {
  29. auto* alignedBegin = AlignUp(large.Begin(), align);
  30. return large.Slice(alignedBegin, alignedBegin + size);
  31. }
  32. return AllocateAligned(size, align);
  33. }
  34. TSharedMutableRef TChunkedMemoryAllocator::AllocateSlowCore(i64 size)
  35. {
  36. if (size > MaxSmallBlockSize_) {
  37. return TSharedMutableRef::Allocate(size, {.InitializeStorage = false}, TagCookie_);
  38. }
  39. Chunk_ = TSharedMutableRef::Allocate(ChunkSize_, {.InitializeStorage = false}, TagCookie_);
  40. FreeZoneBegin_ = Chunk_.Begin();
  41. FreeZoneEnd_ = Chunk_.End();
  42. return TSharedMutableRef();
  43. }
  44. ////////////////////////////////////////////////////////////////////////////////
  45. } // namespace NYT