Files
scylladb/utils/large_bitset.cc
Lakshmi Narayanan Sreethar 7072f7e706 utils/chunked_vector::reserve_partial: fix usage in callers
The method reserve_partial(), when used as documented, quits before the
intended capacity can be reserved fully. This can lead to overallocation
of memory in the last chunk when data is inserted to the chunked vector.
The method itself doesn't have any bug but the way it is being used by
the callers needs to be updated to get the desired behaviour.

Instead of calling it repeatedly with the value returned from the
previous call until it returns zero, it should be repeatedly called with
the intended size until the vector's capacity reaches that size.

This commit updates the method comment and all the callers to use the
right way.

Fixes #19254

Signed-off-by: Lakshmi Narayanan Sreethar <lakshmi.sreethar@scylladb.com>
(cherry picked from commit 64768b58e5)
2024-06-14 15:48:56 +00:00

46 lines
956 B
C++

/*
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include "large_bitset.hh"
#include <algorithm>
#include <seastar/core/align.hh>
#include <seastar/core/thread.hh>
#include "seastarx.hh"
using namespace seastar;
large_bitset::large_bitset(size_t nr_bits) : _nr_bits(nr_bits) {
assert(thread::running_in_thread());
size_t nr_ints = align_up(nr_bits, bits_per_int()) / bits_per_int();
while (_storage.capacity() != nr_ints) {
_storage.reserve_partial(nr_ints);
if (need_preempt()) {
thread::yield();
}
}
while (nr_ints) {
_storage.push_back(0);
--nr_ints;
if (need_preempt()) {
thread::yield();
}
}
}
void
large_bitset::clear() {
assert(thread::running_in_thread());
for (auto&& pos: _storage) {
pos = 0;
if (need_preempt()) {
thread::yield();
}
}
}