chunked_memory_allocator.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. //! Allocates #sizes bytes without any alignment.
  29. TSharedMutableRef AllocateUnaligned(i64 size);
  30. //! Allocates #size bytes aligned with 8-byte granularity.
  31. TSharedMutableRef AllocateAligned(i64 size, int align = 8);
  32. private:
  33. const i64 ChunkSize_;
  34. const i64 MaxSmallBlockSize_;
  35. const TRefCountedTypeCookie TagCookie_;
  36. // Zero-sized arrays are prohibited by C++ standard
  37. // Also even if supported they actually occupy some space (usually 1 byte)
  38. char EmptyBuf_[1];
  39. // Chunk memory layout:
  40. // |AAAA|....|UUUU|
  41. // Legend:
  42. // A aligned allocations
  43. // U unaligned allocations
  44. // . free zone
  45. char* FreeZoneBegin_ = EmptyBuf_;
  46. char* FreeZoneEnd_ = EmptyBuf_;
  47. TSharedMutableRef Chunk_;
  48. TSharedMutableRef AllocateUnalignedSlow(i64 size);
  49. TSharedMutableRef AllocateAlignedSlow(i64 size, int align);
  50. TSharedMutableRef AllocateSlowCore(i64 size);
  51. };
  52. ////////////////////////////////////////////////////////////////////////////////
  53. } // namespace NYT
  54. #define CHUNKED_MEMORY_ALLOCATOR_INL_H_
  55. #include "chunked_memory_allocator-inl.h"
  56. #undef CHUNKED_MEMORY_ALLOCATOR_INL_H_