diff --git a/cpp/src/arrow/buffer.cc b/cpp/src/arrow/buffer.cc index 6d55f88af1e32..2e64ffd75c263 100644 --- a/cpp/src/arrow/buffer.cc +++ b/cpp/src/arrow/buffer.cc @@ -92,7 +92,25 @@ Status PoolBuffer::Reserve(int64_t new_capacity) { } Status PoolBuffer::Resize(int64_t new_size) { - RETURN_NOT_OK(Reserve(new_size)); + if (new_size > size_) { + RETURN_NOT_OK(Reserve(new_size)); + } else { + // Buffer is not growing, so shrink to the requested size without + // excess space. + if (capacity_ != new_size) { + // Buffer hasn't got yet the requested size. + if (new_size == 0) { + pool_->Free(mutable_data_, capacity_); + capacity_ = 0; + mutable_data_ = nullptr; + data_ = nullptr; + } else { + RETURN_NOT_OK(pool_->Reallocate(capacity_, new_size, &mutable_data_)); + data_ = mutable_data_; + capacity_ = new_size; + } + } + } size_ = new_size; return Status::OK(); } diff --git a/cpp/src/arrow/test-util.h b/cpp/src/arrow/test-util.h index e5957490e0733..f2da824084775 100644 --- a/cpp/src/arrow/test-util.h +++ b/cpp/src/arrow/test-util.h @@ -184,7 +184,7 @@ static inline void random_ascii(int n, uint32_t seed, uint8_t* out) { template void rand_uniform_int(int n, uint32_t seed, T min_value, T max_value, T* out) { - DCHECK(out); + DCHECK(out || (n == 0)); std::mt19937 gen(seed); std::uniform_int_distribution d(min_value, max_value); for (int i = 0; i < n; ++i) {