44
55namespace duckdb {
66
7- MemoryStream::MemoryStream (idx_t capacity) : position(0 ), capacity(capacity), owns_data(true ) {
7+ MemoryStream::MemoryStream (Allocator &allocator_p, idx_t capacity)
8+ : allocator(&allocator_p), position(0 ), capacity(capacity) {
89 D_ASSERT (capacity != 0 && IsPowerOfTwo (capacity));
9- data = Allocator::DefaultAllocatorReference ()-> AllocateData (capacity);
10+ data = allocator_p. AllocateData (capacity);
1011}
1112
12- MemoryStream::MemoryStream (data_ptr_t buffer, idx_t capacity)
13- : position(0 ), capacity(capacity), owns_data(false ), data(buffer) {
13+ MemoryStream::MemoryStream (idx_t capacity) : MemoryStream(Allocator::DefaultAllocator(), capacity) {
14+ }
15+
16+ MemoryStream::MemoryStream (data_ptr_t buffer, idx_t capacity) : position(0 ), capacity(capacity), data(buffer) {
1417}
1518
1619MemoryStream::~MemoryStream () {
17- if (owns_data ) {
18- Allocator::DefaultAllocatorReference () ->FreeData (data, capacity);
20+ if (allocator && data ) {
21+ allocator ->FreeData (data, capacity);
1922 }
2023}
2124
@@ -24,48 +27,48 @@ MemoryStream::MemoryStream(MemoryStream &&other) noexcept {
2427 data = other.data ;
2528 position = other.position ;
2629 capacity = other.capacity ;
27- owns_data = other.owns_data ;
30+ allocator = other.allocator ;
2831
2932 // Reset the other stream
3033 other.data = nullptr ;
3134 other.position = 0 ;
3235 other.capacity = 0 ;
33- other.owns_data = false ;
36+ other.allocator = nullptr ;
3437}
3538
3639MemoryStream &MemoryStream::operator =(MemoryStream &&other) noexcept {
3740 if (this != &other) {
3841 // Free the current data
39- if (owns_data ) {
40- Allocator::DefaultAllocatorReference () ->FreeData (data, capacity);
42+ if (allocator ) {
43+ allocator ->FreeData (data, capacity);
4144 }
4245
4346 // Move the data from the other stream into this stream
4447 data = other.data ;
4548 position = other.position ;
4649 capacity = other.capacity ;
47- owns_data = other.owns_data ;
50+ allocator = other.allocator ;
4851
4952 // Reset the other stream
5053 other.data = nullptr ;
5154 other.position = 0 ;
5255 other.capacity = 0 ;
53- other.owns_data = false ;
56+ other.allocator = nullptr ;
5457 }
5558 return *this ;
5659}
5760
5861void MemoryStream::WriteData (const_data_ptr_t source, idx_t write_size) {
5962 const auto old_capacity = capacity;
6063 while (position + write_size > capacity) {
61- if (owns_data ) {
64+ if (allocator ) {
6265 capacity *= 2 ;
6366 } else {
6467 throw SerializationException (" Failed to serialize: not enough space in buffer to fulfill write request" );
6568 }
6669 }
6770 if (capacity != old_capacity) {
68- data = Allocator::DefaultAllocatorReference () ->ReallocateData (data, old_capacity, capacity);
71+ data = allocator ->ReallocateData (data, old_capacity, capacity);
6972 }
7073 memcpy (data + position, source, write_size);
7174 position += write_size;
@@ -84,7 +87,7 @@ void MemoryStream::Rewind() {
8487}
8588
8689void MemoryStream::Release () {
87- owns_data = false ;
90+ allocator = nullptr ;
8891}
8992
9093data_ptr_t MemoryStream::GetData () const {
0 commit comments