mkql_buffer.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "mkql_buffer.h"
  2. namespace NKikimr {
  3. namespace NMiniKQL {
  4. const size_t TBufferPage::PageCapacity = TBufferPage::PageAllocSize - sizeof(TBufferPage);
  5. TBufferPage* TBufferPage::Allocate() {
  6. static_assert(PageAllocSize <= std::numeric_limits<ui32>::max());
  7. static_assert(sizeof(TBufferPage) < PageAllocSize, "Page allocation size is too small");
  8. void* ptr = malloc(PageAllocSize);
  9. if (!ptr) {
  10. throw std::bad_alloc();
  11. }
  12. TBufferPage* result = ::new (ptr) TBufferPage();
  13. return result;
  14. }
  15. void TBufferPage::Free(TBufferPage* page) {
  16. free(page);
  17. }
  18. void TPagedBuffer::AppendPage() {
  19. TBufferPage* page = nullptr;
  20. if (Tail_) {
  21. auto tailPage = TBufferPage::GetPage(Tail_);
  22. auto next = tailPage->Next();
  23. if (next) {
  24. page = next;
  25. page->Clear();
  26. } else {
  27. page = TBufferPage::Allocate();
  28. tailPage->Next_ = page;
  29. }
  30. tailPage->Size_ = TailSize_;
  31. ClosedPagesSize_ += TailSize_;
  32. } else {
  33. Y_DEBUG_ABORT_UNLESS(Head_ == nullptr);
  34. page = TBufferPage::Allocate();
  35. Head_ = page->Data();
  36. }
  37. TailSize_ = 0;
  38. Tail_ = page->Data();
  39. }
  40. using NYql::TChunkedBuffer;
  41. TChunkedBuffer TPagedBuffer::AsChunkedBuffer(const TConstPtr& buffer) {
  42. TChunkedBuffer result;
  43. buffer->ForEachPage([&](const char* data, size_t size) {
  44. result.Append(TStringBuf(data, size), buffer);
  45. });
  46. return result;
  47. }
  48. } // NMiniKQL
  49. } // NKikimr