aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/util/rawbuf.cpp
blob: 6b8efb58b60c0f6f1dd064f5b2cdc6d811311292 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "rawbuf.h"
#include <cassert>
#include <cstdlib>

namespace search {

/**
 * Allocate a new buffer at least as large as the parameter value,
 * move any content to the new and delete the old buffer.
 */
void
RawBuf::expandBuf(size_t needlen)
{
    size_t  size = (_bufEnd - _bufStart) * 2;
    if (size < 1)
        size = 2;
    needlen += _bufEnd - _bufStart;
    while (size < needlen)
        size *= 2;
    char*  nbuf = static_cast<char *>(malloc(size));
    if (_bufFillPos != _bufDrainPos)
        memcpy(nbuf, _bufDrainPos, _bufFillPos - _bufDrainPos);
    _bufFillPos = _bufFillPos - _bufDrainPos + nbuf;
    _bufDrainPos = nbuf;
    free(_bufStart);
    _bufStart = nbuf;
    _bufEnd = _bufStart + size;
}

/**
 * Compact any free space from the beginning of the buffer, by
 * copying the contents to the start of the buffer.
 * If the resulting buffer doesn't have room for 'len' more
 * bytes of contents, make it large enough.
 */
void
RawBuf::preAlloc(size_t len)
{
    size_t curfree = _bufEnd - _bufFillPos;
    if (curfree >= len)
        return;
    if (_bufEnd - _bufStart < len + _bufFillPos - _bufDrainPos) {
        expandBuf(len);
        assert(_bufEnd - _bufStart >= len + _bufFillPos - _bufDrainPos);
        curfree = _bufEnd - _bufFillPos;
        if (curfree >= len)
            return;
    }
    memmove(_bufStart, _bufDrainPos, _bufFillPos - _bufDrainPos);
    _bufFillPos -= (_bufDrainPos - _bufStart);
    _bufDrainPos = _bufStart;
    assert(static_cast<size_t>(_bufEnd -_bufFillPos) >= len);
}

void
RawBuf::ensureSizeInternal(size_t size) {
    expandBuf(size);
    assert(static_cast<size_t>(_bufEnd - _bufFillPos) >= size);
}

}