aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/datastore/buffer_type.hpp
blob: 00d642be9bc4c75565330b2c8b22a0de33dfa488 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "buffer_type.h"

namespace vespalib::datastore {

template <typename ElemT, typename EmptyT>
BufferType<ElemT, EmptyT>::BufferType(uint32_t arraySize, uint32_t min_entries, uint32_t max_entries) noexcept
    : BufferTypeBase(arraySize * sizeof(ElemT), 0u, arraySize, min_entries, max_entries)
{ }

template <typename ElemT, typename EmptyT>
BufferType<ElemT, EmptyT>::BufferType(uint32_t arraySize, uint32_t min_entries, uint32_t max_entries,
                                  uint32_t num_entries_for_new_buffer, float allocGrowFactor) noexcept
    : BufferTypeBase(arraySize * sizeof(ElemT), 0u, arraySize, min_entries, max_entries, num_entries_for_new_buffer, allocGrowFactor)
{ }

template <typename ElemT, typename EmptyT>
BufferType<ElemT, EmptyT>::~BufferType() = default;

template <typename ElemT, typename EmptyT>
void
BufferType<ElemT, EmptyT>::destroy_entries(void *buffer, EntryCount num_entries)
{
    auto num_elems = num_entries * getArraySize();
    ElemType *e = static_cast<ElemType *>(buffer);
    for (size_t j = num_elems; j != 0; --j) {
        e->~ElemType();
        ++e;
    }
}

template <typename ElemT, typename EmptyT>
void
BufferType<ElemT, EmptyT>::fallback_copy(void *newBuffer, const void *oldBuffer, EntryCount num_entries)
{
    auto num_elems = num_entries * getArraySize();
    ElemType *d = static_cast<ElemType *>(newBuffer);
    const ElemType *s = static_cast<const ElemType *>(oldBuffer);
    for (size_t j = num_elems; j != 0; --j) {
        new (static_cast<void *>(d)) ElemType(*s);
        ++s;
        ++d;
    }
}

template <typename ElemT, typename EmptyT>
void
BufferType<ElemT, EmptyT>::initialize_reserved_entries(void *buffer, EntryCount reserved_entries)
{
    auto reserved_elems = reserved_entries * getArraySize();
    ElemType *e = static_cast<ElemType *>(buffer);
    const auto& empty = empty_entry();
    for (size_t j = reserved_elems; j != 0; --j) {
        new (static_cast<void *>(e)) ElemType(empty);
        ++e;
    }
}

template <typename ElemT, typename EmptyT>
void
BufferType<ElemT, EmptyT>::clean_hold(void *buffer, size_t offset, EntryCount num_entries, CleanContext)
{
    auto num_elems = num_entries * getArraySize();
    ElemType *e = static_cast<ElemType *>(buffer) + offset * getArraySize();
    const auto& empty = empty_entry();
    for (size_t j = num_elems; j != 0; --j) {
        *e = empty;
        ++e;
    }
}

template <typename ElemT, typename EmptyT>
const ElemT&
BufferType<ElemT, EmptyT>::empty_entry() noexcept
{
    // It's possible for ElemType to wrap e.g. an Alloc instance, which has a transitive
    // dependency on globally constructed allocator object(s). To avoid issues with global
    // construction order, initialize the sentinel on the first access.
    static ElemType empty = EmptyType();
    return empty;
}

}