chunked_memory_allocator.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #include "ref.h"
  3. namespace NYT {
  4. ////////////////////////////////////////////////////////////////////////////////
  5. struct TDefaultChunkedMemoryAllocatorTag
  6. { };
  7. //! Mimics TChunkedMemoryPool but acts as an allocator returning shared refs.
  8. class TChunkedMemoryAllocator
  9. : private TNonCopyable
  10. {
  11. public:
  12. static const i64 DefaultChunkSize;
  13. static const double DefaultMaxSmallBlockSizeRatio;
  14. explicit TChunkedMemoryAllocator(
  15. i64 chunkSize = DefaultChunkSize,
  16. double maxSmallBlockSizeRatio = DefaultMaxSmallBlockSizeRatio,
  17. TRefCountedTypeCookie tagCookie = GetRefCountedTypeCookie<TDefaultChunkedMemoryAllocatorTag>());
  18. template <class TTag>
  19. explicit TChunkedMemoryAllocator(
  20. TTag /*tag*/ = TTag(),
  21. i64 chunkSize = DefaultChunkSize,
  22. double maxSmallBlockSizeRatio = DefaultMaxSmallBlockSizeRatio)
  23. : TChunkedMemoryAllocator(
  24. chunkSize,
  25. maxSmallBlockSizeRatio,
  26. GetRefCountedTypeCookie<TTag>())
  27. {
  28. static_assert(IsEmptyClass<TTag>());
  29. }
  30. //! Allocates #sizes bytes without any alignment.
  31. TSharedMutableRef AllocateUnaligned(i64 size);
  32. //! Allocates #size bytes aligned with 8-byte granularity.
  33. TSharedMutableRef AllocateAligned(i64 size, int align = 8);
  34. private:
  35. const i64 ChunkSize_;
  36. const i64 MaxSmallBlockSize_;
  37. const TRefCountedTypeCookie TagCookie_;
  38. // Zero-sized arrays are prohibited by C++ standard
  39. // Also even if supported they actually occupy some space (usually 1 byte)
  40. char EmptyBuf_[1];
  41. // Chunk memory layout:
  42. // |AAAA|....|UUUU|
  43. // Legend:
  44. // A aligned allocations
  45. // U unaligned allocations
  46. // . free zone
  47. char* FreeZoneBegin_ = EmptyBuf_;
  48. char* FreeZoneEnd_ = EmptyBuf_;
  49. TSharedMutableRef Chunk_;
  50. TSharedMutableRef AllocateUnalignedSlow(i64 size);
  51. TSharedMutableRef AllocateAlignedSlow(i64 size, int align);
  52. TSharedMutableRef AllocateSlowCore(i64 size);
  53. };
  54. ////////////////////////////////////////////////////////////////////////////////
  55. } // namespace NYT
  56. #define CHUNKED_MEMORY_ALLOCATOR_INL_H_
  57. #include "chunked_memory_allocator-inl.h"
  58. #undef CHUNKED_MEMORY_ALLOCATOR_INL_H_