fix Compact_container::reserve, with timestamps

This commit is contained in:
Laurent Rineau 2025-02-10 18:07:32 +01:00
parent 094507cfaa
commit fa83d0bd45
1 changed files with 35 additions and 33 deletions

View File

@ -571,25 +571,7 @@ public:
while ( capacity_<n )
{ // Pb because the order of free list is no more the order of
// allocate_new_block();
pointer new_block = alloc.allocate(block_size + 2);
all_items.push_back(std::make_pair(new_block, block_size + 2));
capacity_ += block_size;
// We insert this new block at the end.
if (last_item == nullptr) // First time
{
first_item = new_block;
last_item = new_block + block_size + 1;
set_type(first_item, nullptr, START_END);
}
else
{
set_type(last_item, new_block, BLOCK_BOUNDARY);
set_type(new_block, last_item, BLOCK_BOUNDARY);
last_item = new_block + block_size + 1;
}
set_type(last_item, nullptr, START_END);
// Increase the block_size for the next time.
Increment_policy::increase_size(*this);
push_back_new_block();
}
// Now we put all the new elements on freelist, starting from the last block
@ -600,16 +582,18 @@ public:
do
{
--curblock; // We are sure we have at least create a new block
pointer new_block = all_items[curblock].first;
for (size_type i = all_items[curblock].second-2; i >= 1; --i)
put_on_free_list(new_block + i);
auto [new_block, block_size] = all_items[curblock];
put_block_on_free_list(new_block, block_size - 2);
}
while ( curblock>lastblock );
}
private:
void allocate_new_block();
std::pair<pointer, size_type> push_back_new_block();
void put_block_on_free_list(pointer new_block, size_type block_size);
void allocate_new_block();
void put_on_free_list(pointer x)
{
@ -776,20 +760,13 @@ void Compact_container<T, Allocator, Increment_policy, TimeStamper>::clear()
}
template < class T, class Allocator, class Increment_policy, class TimeStamper >
void Compact_container<T, Allocator, Increment_policy, TimeStamper>::allocate_new_block()
auto Compact_container<T, Allocator, Increment_policy, TimeStamper>::push_back_new_block()
-> std::pair<pointer, size_type>
{
pointer new_block = alloc.allocate(block_size + 2);
std::pair<pointer, size_type> result{new_block, block_size};
all_items.push_back(std::make_pair(new_block, block_size + 2));
capacity_ += block_size;
// We don't touch the first and the last one.
// We mark them free in reverse order, so that the insertion order
// will correspond to the iterator order...
for (size_type i = block_size; i >= 1; --i)
{
EraseCounterStrategy<T>::set_erase_counter(*(new_block + i), 0);
Time_stamper::initialize_time_stamp(new_block + i);
put_on_free_list(new_block + i);
}
// We insert this new block at the end.
if (last_item == nullptr) // First time
{
@ -806,8 +783,33 @@ void Compact_container<T, Allocator, Increment_policy, TimeStamper>::allocate_ne
set_type(last_item, nullptr, START_END);
// Increase the block_size for the next time.
Increment_policy::increase_size(*this);
return result;
}
template < class T, class Allocator, class Increment_policy, class TimeStamper >
void Compact_container<T, Allocator, Increment_policy, TimeStamper>::
put_block_on_free_list(pointer new_block, size_type block_size)
{
// The block actually has a size==block_size+2.
// We don't touch the first and the last one.
// We mark them free in reverse order, so that the insertion order
// will correspond to the iterator order...
for (size_type i = block_size; i >= 1; --i)
{
EraseCounterStrategy<T>::set_erase_counter(*(new_block + i), 0);
Time_stamper::initialize_time_stamp(new_block + i);
put_on_free_list(new_block + i);
}
}
template < class T, class Allocator, class Increment_policy, class TimeStamper >
void Compact_container<T, Allocator, Increment_policy, TimeStamper>::allocate_new_block()
{
auto [new_block, block_size] = push_back_new_block();
put_block_on_free_list(new_block, block_size);
}
template < class T, class Allocator, class Increment_policy, class TimeStamper >
inline
bool operator==(const Compact_container<T, Allocator, Increment_policy, TimeStamper> &lhs,