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
|
/*
* Copyright (c) 2019, 2025, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.0,
* as published by the Free Software Foundation.
*
* This program is designed to work with certain software (including
* but not limited to OpenSSL) that is licensed under separate terms,
* as designated in a particular file or component or in included license
* documentation. The authors of MySQL hereby grant you an additional
* permission to link the program and your derivative works with the
* separately licensed software that they have either included with
* the program or referenced in the documentation.
*
* This program 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, version 2.0, for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef PLUGIN_X_PROTOCOL_ENCODERS_ENCODING_BUFFER_H_
#define PLUGIN_X_PROTOCOL_ENCODERS_ENCODING_BUFFER_H_
#include <assert.h>
#include <cassert>
#include <cstdint>
#include <vector>
// NOLINT(build/include_subdir)
#include "plugin/x/protocol/encoders/encoding_pool.h"
#include "plugin/x/src/ngs/protocol/page_pool.h"
namespace protocol {
class Encoding_buffer final {
public:
constexpr static uint32_t k_page_size = 4096;
public:
explicit Encoding_buffer(Encoding_pool *local_pool)
: m_local_pool(local_pool) {
assert(k_page_size ==
static_cast<uint32_t>(
local_pool->get_pool()->get_config()->m_page_size));
m_front = m_current = m_local_pool->alloc_page();
}
~Encoding_buffer() { remove_page_list(m_front); }
Page *get_next_page() {
auto page = m_local_pool->alloc_page();
m_current->m_next_page = page;
m_current = page;
return m_current;
}
void remove_page_list(Page *page) {
while (page) {
auto page_to_delete = page;
page = page_to_delete->m_next_page;
m_local_pool->release_page(page_to_delete);
}
}
template <uint32_t size>
void ensure_buffer_size() {
static_assert(size < k_page_size,
"Page size might be too small to put those data in.");
if (!m_current->is_at_least(size)) get_next_page();
}
bool ensure_buffer_size(const uint32_t size) {
if (!m_current->is_at_least(size)) {
get_next_page();
// Page size limits the number of bytes that
// user can acquire in single call
assert(m_current->is_at_least(size));
return true;
}
return false;
}
void reset() {
remove_page_list(m_front->m_next_page);
m_front->m_next_page = nullptr;
m_front->m_current_data = m_front->m_begin_data;
m_current = m_front;
}
Page *m_front;
Page *m_current;
private:
Encoding_pool *m_local_pool;
};
} // namespace protocol
#endif // PLUGIN_X_PROTOCOL_ENCODERS_ENCODING_BUFFER_H_
|