summaryrefslogtreecommitdiffstats
path: root/vbench/src/vbench/core/simple_buffer.cpp
blob: 1e1d0a8e4aeee05127ec067c90a6fa491aa59d2d (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/fastos/fastos.h>
#include "simple_buffer.h"

namespace vbench {

SimpleBuffer::SimpleBuffer()
    : _data(),
      _used(0)
{
}

Memory
SimpleBuffer::obtain(size_t bytes, size_t)
{
    return Memory(&_data[0], std::min(bytes, _used));
}

Input &
SimpleBuffer::evict(size_t bytes)
{
    assert(bytes <= _used);
    _data.erase(_data.begin(), _data.begin() + bytes);
    _used -= bytes;
    return *this;
}

WritableMemory
SimpleBuffer::reserve(size_t bytes)
{
    _data.resize(_used + bytes, char(0x55));
    return WritableMemory(&_data[_used], bytes);
}

Output &
SimpleBuffer::commit(size_t bytes, size_t)
{
    assert(bytes <= (_data.size() - _used));
    _used += bytes;
    return *this;
}

bool
SimpleBuffer::operator==(const SimpleBuffer &rhs) const
{
    Memory a = get();
    Memory b = rhs.get();
    if (a.size != b.size) {
        return false;
    }
    for (size_t i = 0; i < a.size; ++i) {
        if (a.data[i] != b.data[i]) {
            return false;
        }
    }
    return true;
}

std::ostream &operator<<(std::ostream &os, const SimpleBuffer &buf) {
    Memory memory = buf.get();
    uint32_t written = 0;
    uint32_t hexCount = 25;
    os << "size: " << memory.size << "(bytes)" << std::endl;
    for (size_t i = 0; i < memory.size; ++i, ++written) {
        if (written > hexCount) {
            os << std::endl;
            written = 0;
        }
        os << strfmt("0x%02x ", memory.data[i] & 0xff);
    }
    if (written > 0) {
        os << std::endl;
    }
    return os;
}

} // namespace vbench