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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#ident "$Id$"
/*======
This file is part of PerconaFT.
Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
PerconaFT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2,
as published by the Free Software Foundation.
PerconaFT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
----------------------------------------
PerconaFT is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License, version 3,
as published by the Free Software Foundation.
PerconaFT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
======= */
#ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
#pragma once
#include <algorithm>
#include <cstdlib>
#include <memory>
namespace ftcxx {
/**
* Buffer implements a flat memory buffer intended for FIFO usage
* where allocations are piecemeal but consumption is total. That is,
* we repeatedly fill up the buffer with small allocations, and
* periodically consume all entries and clear the buffer.
*
* For now, the implementation uses a doubling array strategy,
* starting at 1kB growing to a maximum advised capacity of 256kB,
* never shrinking the buffer.
*
* However, we hope to find a better strategy.
*
* Facebook's FBVector claims that a reallocation growth factor of 1.5
* rather than 2 hits their sweet spot, and they claim to have
* additional improvements by integrating with jemalloc (which we use
* as well).
*
* Additionally, it may be advantageous to use some memarena-style
* tricks like allocating a separate overflow buffer to avoid
* memcpying when we're close to our intended maximum capacity, and
* also to avoid wasting extra memory if we overflow our maximum
* capacity once but never do so again.
*/
class Buffer {
public:
Buffer();
explicit Buffer(size_t capacity);
Buffer(const Buffer &) = delete;
Buffer& operator=(const Buffer &) = delete;
Buffer(Buffer&& other)
: _cur(0),
_end(0),
_capacity(0),
_buf(nullptr, &std::free)
{
std::swap(_cur, other._cur);
std::swap(_end, other._end);
std::swap(_capacity, other._capacity);
std::swap(_buf, other._buf);
}
Buffer& operator=(Buffer&& other) {
std::swap(_cur, other._cur);
std::swap(_end, other._end);
std::swap(_capacity, other._capacity);
std::swap(_buf, other._buf);
return *this;
}
// Producer API:
/**
* Allocate room for sz more bytes at the end, and return a
* pointer to the allocated space. This causes at most one
* realloc and memcpy of existing data.
*/
char *alloc(size_t sz);
/**
* Returns true if we're close to our maximum capacity. If so,
* the producer should stop and allow the consumer to clear the
* buffer.
*/
bool full() const;
// Consumer API:
/**
* Returns true if there are more unconsumed bytes in the buffer.
*/
bool more() const;
/**
* Returns a pointer to the next unconsumed byte in the buffer.
*/
char *current() const;
/**
* Advances the unconsumed position pointer by sz bytes.
*/
void advance(size_t sz);
/**
* Free all allocated space.
*/
void clear();
private:
size_t _cur;
size_t _end;
size_t _capacity;
std::unique_ptr<char, void (*)(void*)> _buf;
static const size_t INITIAL_CAPACITY;
static const size_t MAXIMUM_CAPACITY;
static const double FULLNESS_RATIO;
void init();
static size_t next_alloc_size(size_t sz);
void grow(size_t sz);
char *raw(size_t i=0) const {
return &(_buf.get()[i]);
}
};
} // namespace ftcxx
|