chunked_output_stream.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "chunked_output_stream.h"
  2. #include <util/system/sanitizers.h>
  3. namespace NYT {
  4. ////////////////////////////////////////////////////////////////////////////////
  5. TChunkedOutputStream::TChunkedOutputStream(
  6. TRefCountedTypeCookie tagCookie,
  7. size_t initialReserveSize,
  8. size_t maxReserveSize)
  9. : MaxReserveSize_(RoundUpToPage(maxReserveSize))
  10. , CurrentReserveSize_(RoundUpToPage(initialReserveSize))
  11. , CurrentChunk_(tagCookie, /*size*/ 0)
  12. {
  13. YT_VERIFY(MaxReserveSize_ > 0);
  14. if (CurrentReserveSize_ > MaxReserveSize_) {
  15. CurrentReserveSize_ = MaxReserveSize_;
  16. }
  17. }
  18. std::vector<TSharedRef> TChunkedOutputStream::Finish()
  19. {
  20. FinishedChunks_.push_back(TSharedRef::FromBlob(std::move(CurrentChunk_)));
  21. YT_ASSERT(CurrentChunk_.IsEmpty());
  22. FinishedSize_ = 0;
  23. for (const auto& chunk : FinishedChunks_) {
  24. NSan::CheckMemIsInitialized(chunk.Begin(), chunk.Size());
  25. }
  26. return std::move(FinishedChunks_);
  27. }
  28. size_t TChunkedOutputStream::GetSize() const
  29. {
  30. return FinishedSize_ + CurrentChunk_.Size();
  31. }
  32. size_t TChunkedOutputStream::GetCapacity() const
  33. {
  34. return FinishedSize_ + CurrentChunk_.Capacity();
  35. }
  36. void TChunkedOutputStream::ReserveNewChunk(size_t spaceRequired)
  37. {
  38. YT_ASSERT(CurrentChunk_.Size() == CurrentChunk_.Capacity());
  39. FinishedSize_ += CurrentChunk_.Size();
  40. FinishedChunks_.push_back(TSharedRef::FromBlob(std::move(CurrentChunk_)));
  41. CurrentReserveSize_ = std::min(2 * CurrentReserveSize_, MaxReserveSize_);
  42. CurrentChunk_.Reserve(std::max(RoundUpToPage(spaceRequired), CurrentReserveSize_));
  43. }
  44. void TChunkedOutputStream::DoWrite(const void* buffer, size_t length)
  45. {
  46. if (CurrentChunk_.Capacity() == 0) {
  47. CurrentChunk_.Reserve(CurrentReserveSize_);
  48. }
  49. auto spaceAvailable = std::min(length, CurrentChunk_.Capacity() - CurrentChunk_.Size());
  50. CurrentChunk_.Append(buffer, spaceAvailable);
  51. auto spaceRequired = length - spaceAvailable;
  52. if (spaceRequired > 0) {
  53. ReserveNewChunk(spaceRequired);
  54. CurrentChunk_.Append(static_cast<const char*>(buffer) + spaceAvailable, spaceRequired);
  55. }
  56. }
  57. size_t TChunkedOutputStream::DoNext(void** ptr)
  58. {
  59. if (CurrentChunk_.Size() == CurrentChunk_.Capacity()) {
  60. if (CurrentChunk_.Capacity() == 0) {
  61. CurrentChunk_.Reserve(CurrentReserveSize_);
  62. } else {
  63. ReserveNewChunk(0);
  64. }
  65. }
  66. auto spaceAvailable = CurrentChunk_.Capacity() - CurrentChunk_.Size();
  67. YT_ASSERT(spaceAvailable > 0);
  68. *ptr = CurrentChunk_.End();
  69. CurrentChunk_.Resize(CurrentChunk_.Capacity(), /*initializeStorage*/ false);
  70. return spaceAvailable;
  71. }
  72. void TChunkedOutputStream::DoUndo(size_t len)
  73. {
  74. YT_VERIFY(CurrentChunk_.Size() >= len);
  75. CurrentChunk_.Resize(CurrentChunk_.Size() - len);
  76. }
  77. char* TChunkedOutputStream::Preallocate(size_t size)
  78. {
  79. size_t available = CurrentChunk_.Capacity() - CurrentChunk_.Size();
  80. if (available < size) {
  81. FinishedSize_ += CurrentChunk_.Size();
  82. FinishedChunks_.push_back(TSharedRef::FromBlob(std::move(CurrentChunk_)));
  83. CurrentReserveSize_ = std::min(2 * CurrentReserveSize_, MaxReserveSize_);
  84. CurrentChunk_.Reserve(std::max(RoundUpToPage(size), CurrentReserveSize_));
  85. }
  86. return CurrentChunk_.End();
  87. }
  88. void TChunkedOutputStream::Advance(size_t size)
  89. {
  90. YT_ASSERT(CurrentChunk_.Size() + size <= CurrentChunk_.Capacity());
  91. CurrentChunk_.Resize(CurrentChunk_.Size() + size, false);
  92. }
  93. ////////////////////////////////////////////////////////////////////////////////
  94. } // namespace NYT