aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/common/growablebitvector.h
blob: 587008f92db3a07372cf3b6498285abb323cef40 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "allocatedbitvector.h"
#include <vespa/vespalib/util/atomic.h>
#include <vespa/vespalib/util/generationholder.h>

namespace search {

class GrowableBitVector
{
public:
    using Alloc = vespalib::alloc::Alloc;
    using GenerationHolder = vespalib::GenerationHolder;
    using GenerationHeldBase = vespalib::GenerationHeldBase;
    GrowableBitVector(BitWord::Index newSize, BitWord::Index newCapacity,
                      GenerationHolder &generationHolder, const Alloc *init_alloc = nullptr);

    const BitVector &reader() const { return acquire_self(); }
    AllocatedBitVector &writer() { return *_stored; }

    BitWord::Index extraByteSize() const {
        return sizeof(AllocatedBitVector) + acquire_self().extraByteSize();
    }

    /** Will return true if a a buffer is held */
    bool reserve(BitWord::Index newCapacity);
    bool shrink(BitWord::Index newCapacity);
    bool extend(BitWord::Index newCapacity);
private:
    GenerationHeldBase::UP grow(BitWord::Index newLength, BitWord::Index newCapacity);

    AllocatedBitVector &acquire_self() const { return *(_self.load(std::memory_order_acquire)); }

    VESPA_DLL_LOCAL bool hold(GenerationHeldBase::UP v);
    std::unique_ptr<AllocatedBitVector> _stored;
    std::atomic<AllocatedBitVector *> _self;
    GenerationHolder &_generationHolder;
};

} // namespace search