29#ifndef MYSQL_CONTAINERS_BUFFERS_MANAGED_BUFFER_SEQUENCE_H
30#define MYSQL_CONTAINERS_BUFFERS_MANAGED_BUFFER_SEQUENCE_H
54namespace managed_buffer_sequence::unittest {
111template <
class Char_tp =
unsigned char,
112 template <
class Element_tp,
class Allocator_tp>
class Container_tp =
148 const Size_t default_buffer_count = 16)
152 grow_calculator, memory_resource) {}
184 if (requested_write_size > std::numeric_limits<Size_t>::max() - read_size)
216 auto [
error, new_capacity] =
227 <<
" " <<
BAPI_VAR(requested_total_size) <<
" "
254 Size_t keep_container_capacity = 16) {
262 Size_t kept_buffer_count = 0;
265 kept_buffer_count < keep_buffer_count;
268 kept_size += it->size();
278 keep_container_capacity =
279 std::max(keep_container_capacity, 2 + kept_buffer_count);
323 const auto *remaining_data = data;
324 auto remaining_size =
size;
326 while (remaining_size != 0) {
327 auto copy_size = std::min(buffer_it->size(), remaining_size);
328 std::memcpy(buffer_it->begin(), remaining_data, copy_size);
329 remaining_data += copy_size;
330 remaining_size -= copy_size;
347 [[maybe_unused]]
int indent)
const override {
353 sep = std::string(
",\n") +
354 std::string(
static_cast<std::string::size_type
>(indent * 2),
' ');
357 int next_indent = (indent != 0) ? indent + 1 : 0;
360 ss <<
"Managed_buffer_sequence(ptr=" << (
const void *)
this
363 << sep <<
"buffers.size=" <<
m_buffers.size()
375 std::string
debug_string([[maybe_unused]]
bool show_contents =
false)
const {
422 if (data ==
nullptr) {
423 BAPI_LOG(
"info",
"error: out of memory allocating " <<
size <<
" bytes");
428 BAPI_LOG(
"info",
"error: out of memory growing container of "
449 auto [write_begin, write_end, write_size] =
453 auto [read_begin, read_end, read_size] =
456 auto write_begin_offset =
std::distance(read_begin, write_begin);
457 auto write_end_offset =
std::distance(read_begin, write_end);
462 BAPI_LOG(
"info",
"error: out of memory growing container");
468 read_end = std::next(read_begin, read_end_offset);
469 write_begin = std::next(read_begin, write_begin_offset);
470 write_end = std::next(read_begin, write_end_offset + 1);
483 using List_t =
typename std::list<Buffer_view_t, Buffer_allocator_t>;
485 using Vector_t =
typename std::vector<Buffer_view_t, Buffer_allocator_t>;
501 Size_t keep_container_capacity) {
502 if (
container.capacity() > 2 * keep_container_capacity) {
503 container.resize(keep_container_capacity);
519 Size_t keep_container_capacity) {
520 if (
container.size() > keep_container_capacity)
521 container.resize(keep_container_capacity);
Class that groups a pointer+size as one object, without managing the memory for it.
constexpr void deallocate(T *p, size_type size)
Use the Memory_resource to deallocate the given pointer.
Definition: allocator.h:95
constexpr T * allocate(size_type n)
Use the Memory_resource to allocate the given number of elements of type T.
Definition: allocator.h:84
Polymorphism-free memory resource class with custom allocator and deallocator functions.
Definition: memory_resource.h:88
Sequence of memory buffers.
Definition: buffer_sequence_view.h:72
Iterator_t begin()
Iterator to the first buffer.
Definition: buffer_sequence_view.h:116
Iterator_t end()
Iterator to the last buffer.
Definition: buffer_sequence_view.h:119
mysql::allocators::Allocator< Buffer_view_t > Buffer_allocator_t
Definition: buffer_sequence_view.h:77
Size_t size() const
Return the total size of all buffers.
Definition: buffer_sequence_view.h:163
Description of a heuristic to determine how much memory to allocate.
Definition: grow_calculator.h:68
Result_t compute_new_size(Size_t old_size, Size_t requested_size) const
Compute the new size.
Definition: grow_calculator.cpp:39
std::string debug_string() const
In debug mode, return a string that describes the internal structure of this object,...
Definition: grow_constraint.h:124
Size_t get_max_size() const
Definition: grow_constraint.cpp:32
Owned, non-contiguous, growable memory buffer.
Definition: managed_buffer_sequence.h:115
void set_grow_calculator(const Grow_calculator_t &grow_calculator)
Set the grow calculator.
Definition: managed_buffer_sequence.h:296
typename Buffer_sequence_view_t::Container_t Container_t
Definition: rw_buffer_sequence.h:116
Grow_status reserve_write_size(Size_t requested_write_size)
Ensure the write part has at least the given size.
Definition: managed_buffer_sequence.h:182
typename Buffer_sequence_view_t::Size_t Size_t
Definition: rw_buffer_sequence.h:111
Managed_buffer_sequence & operator=(Managed_buffer_sequence &&)=delete
std::string debug_string(bool show_contents, int indent) const override
In debug mode, return a string that describes the internal structure of this object,...
Definition: managed_buffer_sequence.h:346
Managed_buffer_sequence(Managed_buffer_sequence &)=delete
bool allocate_and_add_buffer(Size_t size)
Allocate and add a new buffer.
Definition: managed_buffer_sequence.h:419
~Managed_buffer_sequence() override
Definition: managed_buffer_sequence.h:161
Grow_calculator Grow_calculator_t
Definition: managed_buffer_sequence.h:128
Grow_status reserve_total_size(Size_t requested_total_size)
Ensure the total capacity - the sum of sizes of read part and write part - is at least the given numb...
Definition: managed_buffer_sequence.h:213
void reset(Size_t keep_buffer_count=1, Size_t keep_container_capacity=16)
Reset the read part and the write part to size 0.
Definition: managed_buffer_sequence.h:252
typename List_t::iterator List_iterator_t
Definition: managed_buffer_sequence.h:484
Managed_buffer_sequence(Managed_buffer_sequence &&)=delete
static void reset_container(List_t &container, Size_t keep_container_capacity)
std::list-specific function to reset the container.
Definition: managed_buffer_sequence.h:518
Grow_status write(const Char_t *data, Size_t size)
Append the given data.
Definition: managed_buffer_sequence.h:320
Buffer_sequence_view< Char_tp, Container_tp > Buffer_sequence_view_t
Definition: managed_buffer_sequence.h:121
typename Buffer_sequence_view_t::Buffer_view_t Buffer_view_t
Definition: rw_buffer_sequence.h:113
Managed_buffer_sequence & operator=(Managed_buffer_sequence &)=delete
Managed_buffer_sequence(const Grow_calculator_t &grow_calculator=Grow_calculator_t(), const Memory_resource_t &memory_resource=Memory_resource_t(), const Size_t default_buffer_count=16)
Construct a new, empty object.
Definition: managed_buffer_sequence.h:145
const Grow_calculator_t & get_grow_calculator() const
Return a const reference to the grow calculator.
Definition: managed_buffer_sequence.h:291
typename Vector_t::iterator Vector_iterator_t
Definition: managed_buffer_sequence.h:486
typename std::vector< Buffer_view_t, Buffer_allocator_t > Vector_t
Definition: managed_buffer_sequence.h:485
Container_t m_buffers
Container of buffers.
Definition: managed_buffer_sequence.h:533
typename Buffer_sequence_view_t::Char_t Char_t
Definition: rw_buffer_sequence.h:110
static void reset_container(Vector_t &container, Size_t keep_container_capacity)
std::vector-specific function to reset the container.
Definition: managed_buffer_sequence.h:500
Managed_buffer_sequence(Container_t buffers, const Grow_calculator_t &grow_calculator, const Memory_resource_t &memory_resource)
Construct a new object from a given container, where both the read part and the write part are size z...
Definition: managed_buffer_sequence.h:394
bool add_buffer(Char_t *buffer_data, Size_t buffer_size)
Insert the given buffer in the container, appending it to the write part.
Definition: managed_buffer_sequence.h:447
std::string debug_string(bool show_contents=false) const
In debug mode, return a string that describes the internal structure of this object,...
Definition: managed_buffer_sequence.h:375
typename std::list< Buffer_view_t, Buffer_allocator_t > List_t
Definition: managed_buffer_sequence.h:483
Grow_calculator_t m_grow_calculator
Determines how much memory to allocate when new memory is needed.
Definition: managed_buffer_sequence.h:527
typename Buffer_sequence_view_t::Buffer_allocator_t Buffer_allocator_t
Definition: managed_buffer_sequence.h:130
mysql::allocators::Memory_resource Memory_resource_t
Definition: managed_buffer_sequence.h:132
Char_allocator_t m_char_allocator
Allocator to allocate buffer data (characters).
Definition: managed_buffer_sequence.h:530
Non-owning manager for a fixed sequence of memory buffers, which is split into a read part and a writ...
Definition: rw_buffer_sequence.h:107
typename Buffer_sequence_view_t::Container_t Container_t
Definition: rw_buffer_sequence.h:116
typename Buffer_sequence_view_t::Size_t Size_t
Definition: rw_buffer_sequence.h:111
typename Buffer_sequence_view_t::Const_iterator_t Const_iterator_t
Definition: rw_buffer_sequence.h:118
void set_position(Size_t new_position)
Set the specified absolute position.
Definition: rw_buffer_sequence.h:168
static std::tuple< Iterator_t, Iterator_t, Size_t > get_boundaries(Buffer_sequence_view_t &buffer_sequence_view)
Return the beginning, end, and size of the read and write parts.
Definition: rw_buffer_sequence.h:477
typename Buffer_sequence_view_t::Buffer_view_t Buffer_view_t
Definition: rw_buffer_sequence.h:113
void increase_position(Size_t delta)
Move the position right, relative to the current position.
Definition: rw_buffer_sequence.h:187
const Buffer_sequence_view_t & write_part() const
Return a const reference to the write part.
Definition: rw_buffer_sequence.h:225
Size_t capacity() const
Return the current size, i.e., total size of all buffers.
Definition: rw_buffer_sequence.h:216
typename Buffer_sequence_view_t::Char_t Char_t
Definition: rw_buffer_sequence.h:110
typename Buffer_sequence_view_t::Iterator_t Iterator_t
Definition: rw_buffer_sequence.h:117
const Buffer_sequence_view_t & read_part() const
Return a const reference to the read part.
Definition: rw_buffer_sequence.h:219
virtual std::string debug_string(bool show_contents, int indent) const
In debug mode, return a string that describes the internal structure of this object,...
Definition: rw_buffer_sequence.h:239
Definition: managed_buffer_sequence.h:56
void * data() const noexcept
Definition: buffer.h:119
size_t size() const noexcept
Definition: buffer.h:120
Allocator class that uses a polymorphic Memory_resource to allocate memory.
Class that wraps resources in a polymorphic manner.
void error(const char *format,...)
Definition: atomics_array.h:39
bool distance(const dd::Spatial_reference_system *srs, const Geometry *g1, const Geometry *g2, double *distance, bool *is_null) noexcept
Computes the distance between two geometries.
Definition: distance.cc:40
Definition: buffer_sequence_view.h:51
Grow_status
Error statuses for classes that use Grow_calculator.
Definition: grow_status.h:38
@ exceeds_max_size
A grow operation could not be performed because there is a configured maximum size.
@ success
A grow operation succeeded.
@ out_of_memory
A grow operation failed because memory allocation failed.
const char * begin(const char *const c)
Definition: base64.h:44
size_t size(const char *const c)
Definition: base64.h:46
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418
size_t buffer_size(const ConstBufferSequence &buffers) noexcept
Definition: buffer.h:313
Definition: gcs_xcom_synode.h:64
std::basic_ostringstream< char, std::char_traits< char >, ut::allocator< char > > ostringstream
Specialization of basic_ostringstream which uses ut::allocator.
Definition: ut0new.h:2872
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2876
#define NODISCARD
The function attribute [[NODISCARD]] is a replacement for [[nodiscard]] to workaround a gcc bug.
Definition: nodiscard.h:47
Container class that provides a sequence of buffers to the caller.
Contains wrapper functions for memory allocation and deallocation.
#define BAPI_LOG(x, y)
Definition: wrapper_functions.h:67
#define BAPI_TRACE
Definition: wrapper_functions.h:65
#define BAPI_VAR(v)
Definition: wrapper_functions.h:68