summaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/generationholder.cpp
blob: f2b10ff3af14ecc4d4b4f183af743706fdc2e1bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "generationholder.h"
#include <cassert>

namespace vespalib {

GenerationHeldBase::~GenerationHeldBase() = default;

GenerationHolder::GenerationHolder()
    : _hold1List(),
      _hold2List(),
      _heldBytes(0)
{ }

GenerationHolder::~GenerationHolder()
{
    assert(_hold1List.empty());
    assert(_hold2List.empty());
    assert(getHeldBytes() == 0);
}

void
GenerationHolder::hold(GenerationHeldBase::UP data)
{
    _hold1List.push_back(std::move(data));
    _heldBytes.store(getHeldBytes() + _hold1List.back()->byte_size(), std::memory_order_relaxed);
}

void
GenerationHolder::transferHoldListsSlow(generation_t generation)
{
    HoldList::iterator it(_hold1List.begin());
    HoldList::iterator ite(_hold1List.end());
    HoldList &hold2List = _hold2List;
    for (; it != ite; ++it) {
        assert((*it)->_generation == 0u);
        (*it)->_generation = generation;
        hold2List.push_back(std::move(*it));
    }
    _hold1List.clear();
}

void
GenerationHolder::trimHoldListsSlow(generation_t usedGen)
{
    for (;;) {
        if (_hold2List.empty())
            break;
        GenerationHeldBase &first = *_hold2List.front();
        if (static_cast<sgeneration_t>(first._generation - usedGen) >= 0)
            break;
        _heldBytes.store(getHeldBytes() - first.byte_size(), std::memory_order_relaxed);
        _hold2List.erase(_hold2List.begin());
    }
}

void
GenerationHolder::clearHoldLists()
{
    _hold1List.clear();
    _hold2List.clear();
    _heldBytes = 0;
}

}