| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2013, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/dyn0buf.h |
| 29 |
The dynamically allocated buffer implementation |
| 30 |
|
| 31 |
Created 2013-03-16 Sunny Bains |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef dyn0buf_h |
| 35 |
#define dyn0buf_h |
| 36 |
|
| 37 |
#include "dyn0types.h" |
| 38 |
#include "mem0mem.h" |
| 39 |
#include "univ.i" |
| 40 |
#include "ut0lst.h" |
| 41 |
|
| 42 |
/** Class that manages dynamic buffers. It uses a UT_LIST of |
| 43 |
dyn_buf_t::block_t instances. We don't use STL containers in |
| 44 |
order to avoid the overhead of heap calls. Using a custom memory |
| 45 |
allocator doesn't solve the problem either because we have to get |
| 46 |
the memory from somewhere. We can't use the block_t::m_data as the |
| 47 |
backend for the custom allocator because we would like the data in |
| 48 |
the blocks to be contiguous. */ |
| 49 |
template <size_t SIZE = DYN_ARRAY_DATA_SIZE> |
| 50 |
class dyn_buf_t { |
| 51 |
public: |
| 52 |
class block_t; |
| 53 |
|
| 54 |
typedef UT_LIST_NODE_T(block_t) block_node_t; |
| 55 |
|
| 56 |
class block_t { |
| 57 |
public: |
| 58 |
block_t() { |
| 59 |
static_assert(MAX_DATA_SIZE <= (2 << 15)); |
| 60 |
init(); |
| 61 |
} |
| 62 |
|
| 63 |
~block_t() = default; |
| 64 |
|
| 65 |
/** |
| 66 |
Gets the number of used bytes in a block. |
| 67 |
@return number of bytes used */ |
| 68 |
[[nodiscard]] ulint used() const { |
| 69 |
return (static_cast<ulint>(m_used & ~DYN_BLOCK_FULL_FLAG)); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
Gets pointer to the start of data. |
| 74 |
@return pointer to data */ |
| 75 |
[[nodiscard]] byte *start() { return (m_data); } |
| 76 |
|
| 77 |
/** |
| 78 |
@return start of data - non const version */ |
| 79 |
[[nodiscard]] byte *begin() { return (m_data); } |
| 80 |
|
| 81 |
/** |
| 82 |
@return end of used data - non const version */ |
| 83 |
[[nodiscard]] byte *end() { return (begin() + m_used); } |
| 84 |
|
| 85 |
/** |
| 86 |
@return start of data - const version */ |
| 87 |
[[nodiscard]] const byte *begin() const { return (m_data); } |
| 88 |
|
| 89 |
/** |
| 90 |
@return end of used data - const version */ |
| 91 |
[[nodiscard]] const byte *end() const { return (begin() + m_used); } |
| 92 |
|
| 93 |
private: |
| 94 |
/** |
| 95 |
@return pointer to start of reserved space */ |
| 96 |
template <typename Type> |
| 97 |
Type push(uint32_t size) { |
| 98 |
Type ptr = reinterpret_cast<Type>(end()); |
| 99 |
|
| 100 |
m_used += size; |
| 101 |
ut_ad(m_used <= static_cast<uint32_t>(MAX_DATA_SIZE)); |
| 102 |
|
| 103 |
return (ptr); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
Grow the stack. */ |
| 108 |
void close(const byte *ptr) { |
| 109 |
/* Check that it is within bounds */ |
| 110 |
ut_ad(ptr >= begin()); |
| 111 |
ut_ad(ptr <= begin() + m_buf_end); |
| 112 |
|
| 113 |
/* We have done the boundary check above */ |
| 114 |
m_used = static_cast<uint32_t>(ptr - begin()); |
| 115 |
|
| 116 |
ut_ad(m_used <= MAX_DATA_SIZE); |
| 117 |
ut_d(m_buf_end = 0); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
Initialise the block */ |
| 122 |
void init() { |
| 123 |
m_used = 0; |
| 124 |
ut_d(m_buf_end = 0); |
| 125 |
ut_d(m_magic_n = DYN_BLOCK_MAGIC_N); |
| 126 |
} |
| 127 |
|
| 128 |
private: |
| 129 |
#ifdef UNIV_DEBUG |
| 130 |
/** If opened then this is the buffer end offset, else 0 */ |
| 131 |
ulint m_buf_end; |
| 132 |
|
| 133 |
/** Magic number (DYN_BLOCK_MAGIC_N) */ |
| 134 |
ulint m_magic_n; |
| 135 |
#endif /* UNIV_DEBUG */ |
| 136 |
|
| 137 |
/** SIZE - sizeof(m_node) + sizeof(m_used) */ |
| 138 |
static constexpr auto MAX_DATA_SIZE = SIZE; |
| 139 |
|
| 140 |
/** Storage */ |
| 141 |
byte m_data[MAX_DATA_SIZE]; |
| 142 |
|
| 143 |
/** Doubly linked list node. */ |
| 144 |
block_node_t m_node; |
| 145 |
|
| 146 |
/** number of data bytes used in this block; |
| 147 |
DYN_BLOCK_FULL_FLAG is set when the block becomes full */ |
| 148 |
uint32_t m_used; |
| 149 |
|
| 150 |
friend class dyn_buf_t; |
| 151 |
}; |
| 152 |
typedef UT_LIST_BASE_NODE_T(block_t, m_node) block_list_t; |
| 153 |
|
| 154 |
static constexpr auto MAX_DATA_SIZE = block_t::MAX_DATA_SIZE; |
| 155 |
|
| 156 |
/** Default constructor */ |
| 157 |
dyn_buf_t() : m_heap(), m_list(), m_size() { push_back(&m_first_block); } |
| 158 |
|
| 159 |
/** Destructor */ |
| 160 |
~dyn_buf_t() { erase(); } |
| 161 |
|
| 162 |
/** Reset the buffer vector */ |
| 163 |
void erase() { |
| 164 |
if (m_heap != nullptr) { |
| 165 |
mem_heap_free(m_heap); |
| 166 |
m_heap = nullptr; |
| 167 |
|
| 168 |
/* Initialise the list and add the first block. */ |
| 169 |
m_list.clear(); |
| 170 |
push_back(&m_first_block); |
| 171 |
} else { |
| 172 |
m_first_block.init(); |
| 173 |
ut_ad(UT_LIST_GET_LEN(m_list) == 1); |
| 174 |
} |
| 175 |
|
| 176 |
m_size = 0; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
Makes room on top and returns a pointer to a buffer in it. After |
| 181 |
copying the elements, the caller must close the buffer using close(). |
| 182 |
@param size in bytes of the buffer; MUST be <= MAX_DATA_SIZE! |
| 183 |
@return pointer to the buffer */ |
| 184 |
[[nodiscard]] byte *open(ulint size) { |
| 185 |
ut_ad(size > 0); |
| 186 |
ut_ad(size <= MAX_DATA_SIZE); |
| 187 |
|
| 188 |
block_t *block; |
| 189 |
|
| 190 |
block = has_space(size) ? back() : add_block(); |
| 191 |
|
| 192 |
ut_ad(block->m_used <= MAX_DATA_SIZE); |
| 193 |
ut_d(block->m_buf_end = block->m_used + size); |
| 194 |
|
| 195 |
return (block->end()); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
Closes the buffer returned by open. |
| 200 |
@param ptr end of used space */ |
| 201 |
void close(const byte *ptr) { |
| 202 |
ut_ad(UT_LIST_GET_LEN(m_list) > 0); |
| 203 |
block_t *block = back(); |
| 204 |
|
| 205 |
m_size -= block->used(); |
| 206 |
|
| 207 |
block->close(ptr); |
| 208 |
|
| 209 |
m_size += block->used(); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
Makes room on top and returns a pointer to the added element. |
| 214 |
The caller must copy the element to the pointer returned. |
| 215 |
@param size in bytes of the element |
| 216 |
@return pointer to the element */ |
| 217 |
template <typename Type> |
| 218 |
Type push(uint32_t size) { |
| 219 |
ut_ad(size > 0); |
| 220 |
ut_ad(size <= MAX_DATA_SIZE); |
| 221 |
|
| 222 |
block_t *block; |
| 223 |
|
| 224 |
block = has_space(size) ? back() : add_block(); |
| 225 |
|
| 226 |
m_size += size; |
| 227 |
|
| 228 |
/* See ISO C++03 14.2/4 for why "template" is required. */ |
| 229 |
|
| 230 |
return (block->template push<Type>(size)); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
Pushes n bytes. |
| 235 |
@param ptr string to write |
| 236 |
@param len string length */ |
| 237 |
void push(const byte *ptr, uint32_t len) { |
| 238 |
while (len > 0) { |
| 239 |
uint32_t n_copied; |
| 240 |
|
| 241 |
if (len >= MAX_DATA_SIZE) { |
| 242 |
n_copied = MAX_DATA_SIZE; |
| 243 |
} else { |
| 244 |
n_copied = len; |
| 245 |
} |
| 246 |
|
| 247 |
::memmove(push<byte *>(n_copied), ptr, n_copied); |
| 248 |
|
| 249 |
ptr += n_copied; |
| 250 |
len -= n_copied; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
Returns a pointer to an element in the buffer. const version. |
| 256 |
@param pos position of element in bytes from start |
| 257 |
@return pointer to element */ |
| 258 |
template <typename Type> |
| 259 |
const Type at(ulint pos) const { |
| 260 |
block_t *block = |
| 261 |
const_cast<block_t *>(const_cast<dyn_buf_t *>(this)->find(pos)); |
| 262 |
|
| 263 |
return (reinterpret_cast<Type>(block->begin() + pos)); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
Returns a pointer to an element in the buffer. non const version. |
| 268 |
@param pos position of element in bytes from start |
| 269 |
@return pointer to element */ |
| 270 |
template <typename Type> |
| 271 |
Type at(ulint pos) { |
| 272 |
block_t *block = const_cast<block_t *>(find(pos)); |
| 273 |
|
| 274 |
return (reinterpret_cast<Type>(block->begin() + pos)); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
Returns the size of the total stored data. |
| 279 |
@return data size in bytes */ |
| 280 |
[[nodiscard]] ulint size() const { |
| 281 |
#ifdef UNIV_DEBUG |
| 282 |
ulint total_size = 0; |
| 283 |
|
| 284 |
for (const block_t *block : m_list) { |
| 285 |
total_size += block->used(); |
| 286 |
} |
| 287 |
|
| 288 |
ut_ad(total_size == m_size); |
| 289 |
#endif /* UNIV_DEBUG */ |
| 290 |
return (m_size); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
Iterate over each block and call the functor. |
| 295 |
@return false if iteration was terminated. */ |
| 296 |
template <typename Functor> |
| 297 |
bool for_each_block(Functor &functor) const { |
| 298 |
for (const block_t *block : m_list) { |
| 299 |
if (!functor(block)) { |
| 300 |
return (false); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
return (true); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
Iterate over all the blocks in reverse and call the iterator |
| 309 |
@return false if iteration was terminated. */ |
| 310 |
template <typename Functor> |
| 311 |
bool for_each_block_in_reverse(Functor &functor) const { |
| 312 |
for (block_t *block = UT_LIST_GET_LAST(m_list); block != nullptr; |
| 313 |
block = UT_LIST_GET_PREV(m_node, block)) { |
| 314 |
if (!functor(block)) { |
| 315 |
return (false); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
return (true); |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
@return the first block */ |
| 324 |
[[nodiscard]] block_t *front() { |
| 325 |
ut_ad(UT_LIST_GET_LEN(m_list) > 0); |
| 326 |
return (UT_LIST_GET_FIRST(m_list)); |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
@return true if m_first_block block was not filled fully */ |
| 331 |
[[nodiscard]] bool is_small() const { return (m_heap == nullptr); } |
| 332 |
|
| 333 |
private: |
| 334 |
// Disable copying |
| 335 |
dyn_buf_t(dyn_buf_t &&) = delete; |
| 336 |
dyn_buf_t(const dyn_buf_t &) = delete; |
| 337 |
dyn_buf_t &operator=(dyn_buf_t &&) = delete; |
| 338 |
dyn_buf_t &operator=(const dyn_buf_t &) = delete; |
| 339 |
|
| 340 |
/** |
| 341 |
Add the block to the end of the list*/ |
| 342 |
void push_back(block_t *block) { |
| 343 |
block->init(); |
| 344 |
|
| 345 |
UT_LIST_ADD_LAST(m_list, block); |
| 346 |
} |
| 347 |
|
| 348 |
/** @return the last block in the list */ |
| 349 |
block_t *back() { return (UT_LIST_GET_LAST(m_list)); } |
| 350 |
|
| 351 |
/* |
| 352 |
@return true if request can be fulfilled */ |
| 353 |
bool has_space(ulint size) const { |
| 354 |
return (back()->m_used + size <= MAX_DATA_SIZE); |
| 355 |
} |
| 356 |
|
| 357 |
/* |
| 358 |
@return true if request can be fulfilled */ |
| 359 |
bool has_space(ulint size) { |
| 360 |
return (back()->m_used + size <= MAX_DATA_SIZE); |
| 361 |
} |
| 362 |
|
| 363 |
/** Find the block that contains the pos. |
| 364 |
@param pos absolute offset, it is updated to make it relative |
| 365 |
to the block |
| 366 |
@return the block containing the pos. */ |
| 367 |
block_t *find(ulint &pos) { |
| 368 |
ut_ad(UT_LIST_GET_LEN(m_list) > 0); |
| 369 |
|
| 370 |
for (auto block : m_list) { |
| 371 |
if (pos < block->used()) { |
| 372 |
return block; |
| 373 |
} |
| 374 |
|
| 375 |
pos -= block->used(); |
| 376 |
} |
| 377 |
|
| 378 |
ut_d(ut_error); |
| 379 |
ut_o(return nullptr); |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
Allocate and add a new block to m_list */ |
| 384 |
block_t *add_block() { |
| 385 |
block_t *block; |
| 386 |
|
| 387 |
if (m_heap == nullptr) { |
| 388 |
m_heap = mem_heap_create(sizeof(*block), UT_LOCATION_HERE); |
| 389 |
} |
| 390 |
|
| 391 |
block = reinterpret_cast<block_t *>(mem_heap_alloc(m_heap, sizeof(*block))); |
| 392 |
|
| 393 |
push_back(block); |
| 394 |
|
| 395 |
return (block); |
| 396 |
} |
| 397 |
|
| 398 |
private: |
| 399 |
/** Heap to use for memory allocation */ |
| 400 |
mem_heap_t *m_heap; |
| 401 |
|
| 402 |
/** Allocated blocks */ |
| 403 |
block_list_t m_list; |
| 404 |
|
| 405 |
/** Total size used by all blocks */ |
| 406 |
ulint m_size; |
| 407 |
|
| 408 |
/** The default block, should always be the first element. This |
| 409 |
is for backwards compatibility and to avoid an extra heap allocation |
| 410 |
for small REDO log records */ |
| 411 |
block_t m_first_block; |
| 412 |
}; |
| 413 |
|
| 414 |
typedef dyn_buf_t<DYN_ARRAY_DATA_SIZE> mtr_buf_t; |
| 415 |
|
| 416 |
/** mtr_buf_t copier */ |
| 417 |
struct mtr_buf_copy_t { |
| 418 |
/** The copied buffer */ |
| 419 |
mtr_buf_t m_buf; |
| 420 |
|
| 421 |
/** Append a block to the redo log buffer. |
| 422 |
@return whether the appending should continue (always true here) */ |
| 423 |
bool operator()(const mtr_buf_t::block_t *block) { |
| 424 |
byte *buf = m_buf.open(block->used()); |
| 425 |
memcpy(buf, block->begin(), block->used()); |
| 426 |
m_buf.close(buf + block->used()); |
| 427 |
return (true); |
| 428 |
} |
| 429 |
}; |
| 430 |
|
| 431 |
#endif /* dyn0buf_h */ |