1
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
5
COPYING CONDITIONS NOTICE:
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of version 2 of the GNU General Public License as
9
published by the Free Software Foundation, and provided that the
10
following conditions are met:
12
* Redistributions of source code must retain this COPYING
13
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
14
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
15
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
18
* Redistributions in binary form must reproduce this COPYING
19
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
20
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
21
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
22
GRANT (below) in the documentation and/or other materials
23
provided with the distribution.
25
You should have received a copy of the GNU General Public License
26
along with this program; if not, write to the Free Software
27
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32
TokuDB, Tokutek Fractal Tree Indexing Library.
33
Copyright (C) 2007-2013 Tokutek, Inc.
37
This program is distributed in the hope that it will be useful, but
38
WITHOUT ANY WARRANTY; without even the implied warranty of
39
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
40
General Public License for more details.
42
UNIVERSITY PATENT NOTICE:
44
The technology is licensed by the Massachusetts Institute of
45
Technology, Rutgers State University of New Jersey, and the Research
46
Foundation of State University of New York at Stony Brook under
47
United States of America Serial No. 11/760379 and to the patents
48
and/or patent applications resulting from it.
50
PATENT MARKING NOTICE:
52
This software is covered by US Patent No. 8,185,551.
53
This software is covered by US Patent No. 8,489,638.
57
"THIS IMPLEMENTATION" means the copyrightable works distributed by
58
Tokutek as part of the Fractal Tree project.
60
"PATENT CLAIMS" means the claims of patents that are owned or
61
licensable by Tokutek, both currently or in the future; and that in
62
the absence of this license would be infringed by THIS
63
IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
65
"PATENT CHALLENGE" shall mean a challenge to the validity,
66
patentability, enforceability and/or non-infringement of any of the
67
PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
69
Tokutek hereby grants to you, for the term and geographical scope of
70
the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
71
irrevocable (except as stated in this section) patent license to
72
make, have made, use, offer to sell, sell, import, transfer, and
73
otherwise run, modify, and propagate the contents of THIS
74
IMPLEMENTATION, where such license applies only to the PATENT
75
CLAIMS. This grant does not include claims that would be infringed
76
only as a consequence of further modifications of THIS
77
IMPLEMENTATION. If you or your agent or licensee institute or order
78
or agree to the institution of patent litigation against any entity
79
(including a cross-claim or counterclaim in a lawsuit) alleging that
80
THIS IMPLEMENTATION constitutes direct or contributory patent
81
infringement, or inducement of patent infringement, then any rights
82
granted to you under this License shall terminate as of the date
83
such litigation is filed. If you or your agent or exclusive
84
licensee institute or order or agree to the institution of a PATENT
85
CHALLENGE, then Tokutek may terminate any rights granted to you
89
#ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved."
90
#ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
91
#if !defined(_TOKUDB_BUFFER_H)
92
#define _TOKUDB_BUFFER_H
94
#include "tokudb_vlq.h"
98
// A Buffer manages a contiguous chunk of memory and supports appending new data to the end of the buffer, and
99
// consuming chunks from the beginning of the buffer. The buffer will reallocate memory when appending
100
// new data to a full buffer.
104
buffer(void *the_data, size_t s, size_t l) : m_data(the_data), m_size(s), m_limit(l), m_is_static(true) {
106
buffer() : m_data(NULL), m_size(0), m_limit(0), m_is_static(false) {
113
// Return a pointer to the end of the buffer suitable for appending a fixed number of bytes.
114
void *append_ptr(size_t s) {
116
void *p = (char *) m_data + m_size;
121
// Append bytes to the buffer
122
void append(void *p, size_t s) {
123
memcpy(append_ptr(s), p, s);
126
// Append an unsigned int to the buffer.
127
// Returns the number of bytes used to encode the number.
128
// Returns 0 if the number could not be encoded.
129
template<class T> size_t append_ui(T n) {
130
maybe_realloc(10); // 10 bytes is big enough for up to 64 bit number
131
size_t s = tokudb::vlq_encode_ui<T>(n, (char *) m_data + m_size, 10);
136
// Return a pointer to the next location in the buffer where bytes are consumed from.
137
void *consume_ptr(size_t s) {
138
if (m_size + s > m_limit)
140
void *p = (char *) m_data + m_size;
145
// Consume bytes from the buffer.
146
void consume(void *p, size_t s) {
147
memcpy(p, consume_ptr(s), s);
150
// Consume an unsigned int from the buffer.
151
// Returns 0 if the unsigned int could not be decoded, probably because the buffer is too short.
152
// Otherwise return the number of bytes consumed, and stuffs the decoded number in *p.
153
template<class T> size_t consume_ui(T *p) {
154
size_t s = tokudb::vlq_decode_ui<T>(p, (char *) m_data + m_size, m_limit - m_size);
159
// Write p_length bytes at an offset in the buffer
160
void write(void *p, size_t p_length, size_t offset) {
161
assert(offset + p_length <= m_size);
162
memcpy((char *)m_data + offset, p, p_length);
165
// Read p_length bytes at an offset in the buffer
166
void read(void *p, size_t p_length, size_t offset) {
167
assert(offset + p_length <= m_size);
168
memcpy(p, (char *)m_data + offset, p_length);
171
// Replace a field in the buffer with new data. If the new data size is different, then readjust the
172
// size of the buffer and move things around.
173
void replace(size_t offset, size_t old_s, void *new_p, size_t new_s) {
174
assert(offset + old_s <= m_size);
176
maybe_realloc(new_s - old_s);
177
char *data_offset = (char *) m_data + offset;
178
if (new_s != old_s) {
179
size_t n = m_size - (offset + old_s);
180
assert(offset + new_s + n <= m_limit && offset + old_s + n <= m_limit);
181
memmove(data_offset + new_s, data_offset + old_s, n);
183
m_size += new_s - old_s;
185
m_size -= old_s - new_s;
186
assert(m_size <= m_limit);
188
memcpy(data_offset, new_p, new_s);
191
// Return a pointer to the data in the buffer
196
// Return the size of the data in the buffer
197
size_t size() const {
201
// Return the size of the underlying memory in the buffer
202
size_t limit() const {
207
// Maybe reallocate the buffer when it becomes full by doubling its size.
208
void maybe_realloc(size_t s) {
209
if (m_size + s > m_limit) {
210
size_t new_limit = m_limit * 2;
211
if (new_limit < m_size + s)
212
new_limit = m_size + s;
213
assert(!m_is_static);
214
void *new_data = realloc(m_data, new_limit);
215
assert(new_data != NULL);