aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/datastore/allocator.hpp
blob: fe425b6a45c2a5ef8844917fcb031957b1859097 (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
87
88
89
90
91
92
93
94
95
96
97
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "allocator.h"
#include "bufferstate.h"
#include <cassert>

namespace vespalib::datastore {

template <typename EntryT, typename RefT>
Allocator<EntryT, RefT>::Allocator(DataStoreBase &store, uint32_t typeId)
    : _store(store),
      _typeId(typeId)
{
}

template <typename EntryT, typename RefT>
template <typename ... Args>
typename Allocator<EntryT, RefT>::HandleType
Allocator<EntryT, RefT>::alloc(Args && ... args)
{
    _store.ensure_buffer_capacity(_typeId, 1);
    uint32_t buffer_id = _store.primary_buffer_id(_typeId);
    BufferState &state = _store.getBufferState(buffer_id);
    assert(state.isActive());
    size_t oldBufferSize = state.size();
    RefT ref(oldBufferSize, buffer_id);
    EntryT *entry = _store.getEntry<EntryT>(ref);
    new (static_cast<void *>(entry)) EntryT(std::forward<Args>(args)...);
    state.stats().pushed_back(1);
    return HandleType(ref, entry);
}

template <typename EntryT, typename RefT>
typename Allocator<EntryT, RefT>::HandleType
Allocator<EntryT, RefT>::allocArray(ConstArrayRef array)
{
    _store.ensure_buffer_capacity(_typeId, 1);
    uint32_t buffer_id = _store.primary_buffer_id(_typeId);
    BufferState &state = _store.getBufferState(buffer_id);
    assert(state.isActive());
    assert(state.getArraySize() == array.size());
    RefT ref(state.size(), buffer_id);
    EntryT *buf = _store.template getEntryArray<EntryT>(ref, array.size());
    for (size_t i = 0; i < array.size(); ++i) {
        new (static_cast<void *>(buf + i)) EntryT(array[i]);
    }
    state.stats().pushed_back(1);
    return HandleType(ref, buf);
}

template <typename EntryT, typename RefT>
typename Allocator<EntryT, RefT>::HandleType
Allocator<EntryT, RefT>::allocArray()
{
    _store.ensure_buffer_capacity(_typeId, 1);
    uint32_t buffer_id = _store.primary_buffer_id(_typeId);
    BufferState &state = _store.getBufferState(buffer_id);
    assert(state.isActive());
    RefT ref(state.size(), buffer_id);
    auto array_size = state.getArraySize();
    EntryT *buf = _store.template getEntryArray<EntryT>(ref, array_size);
    for (size_t i = 0; i < array_size; ++i) {
        new (static_cast<void *>(buf + i)) EntryT();
    }
    state.stats().pushed_back(1);
    return HandleType(ref, buf);
}

template <typename EntryT, typename RefT>
template <typename BufferType>
typename Allocator<EntryT, RefT>::HandleType
Allocator<EntryT, RefT>::alloc_dynamic_array(ConstArrayRef array)
{
    _store.ensure_buffer_capacity(_typeId, 1);
    uint32_t buffer_id = _store.primary_buffer_id(_typeId);
    BufferState &state = _store.getBufferState(buffer_id);
    assert(state.isActive());
    auto max_array_size = state.getArraySize();
    assert(max_array_size >= array.size());
    RefT ref(state.size(), buffer_id);
    auto entry_size = _store.get_entry_size(_typeId);
    EntryT* buf = BufferType::get_entry(_store.getBuffer(ref.bufferId()), ref.offset(), entry_size);
    for (size_t i = 0; i < array.size(); ++i) {
        new (static_cast<void *>(buf + i)) EntryT(array[i]);
    }
    for (size_t i = array.size(); i < max_array_size; ++i) {
        new (static_cast<void *>(buf + i)) EntryT();
    }
    BufferType::set_dynamic_array_size(buf, array.size());
     state.stats().pushed_back(1);
    return HandleType(ref, buf);
}

}