31#ifndef MYSQL_BUFFER_MANAGED_BUFFER_SEQUENCE_H_
32#define MYSQL_BUFFER_MANAGED_BUFFER_SEQUENCE_H_
53namespace managed_buffer_sequence::unittest {
110template <
class Char_tp =
unsigned char,
111 template <
class Element_tp,
class Allocator_tp>
class Container_tp =
147 const Size_t default_buffer_count = 16)
151 grow_calculator, memory_resource) {}
183 if (requested_write_size > std::numeric_limits<Size_t>::max() - read_size)
215 auto [
error, new_capacity] =
226 <<
" " <<
BAPI_VAR(requested_total_size) <<
" "
253 Size_t keep_container_capacity = 16) {
261 Size_t kept_buffer_count = 0;
264 kept_buffer_count < keep_buffer_count;
267 kept_size += it->size();
277 keep_container_capacity =
278 std::max(keep_container_capacity, 2 + kept_buffer_count);
322 const auto *remaining_data = data;
323 auto remaining_size = size;
325 while (remaining_size != 0) {
326 auto copy_size = std::min(buffer_it->size(), remaining_size);
327 std::memcpy(buffer_it->begin(), remaining_data, copy_size);
328 remaining_data += copy_size;
329 remaining_size -= copy_size;
346 [[maybe_unused]]
int indent)
const override {
352 sep = std::string(
",\n") +
353 std::string(
static_cast<std::string::size_type
>(indent * 2),
' ');
356 int next_indent = (indent != 0) ? indent + 1 : 0;
359 ss <<
"Managed_buffer_sequence(ptr=" << (
const void *)
this
362 << sep <<
"buffers.size=" <<
m_buffers.size()
374 std::string
debug_string([[maybe_unused]]
bool show_contents =
false)
const {
421 if (data ==
nullptr) {
422 BAPI_LOG(
"info",
"error: out of memory allocating " << size <<
" bytes");
427 BAPI_LOG(
"info",
"error: out of memory growing container of "
448 auto [write_begin, write_end, write_size] =
452 auto [read_begin, read_end, read_size] =
455 auto write_begin_offset =
std::distance(read_begin, write_begin);
456 auto write_end_offset =
std::distance(read_begin, write_end);
461 BAPI_LOG(
"info",
"error: out of memory growing container");
467 read_end = std::next(read_begin, read_end_offset);
468 write_begin = std::next(read_begin, write_begin_offset);
469 write_end = std::next(read_begin, write_end_offset + 1);
482 using List_t =
typename std::list<Buffer_view_t, Buffer_allocator_t>;
484 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);
520 Size_t keep_container_capacity) {
521 if (
container.size() > keep_container_capacity)
522 container.resize(keep_container_capacity);
Class that groups a pointer+size as one object, without managing the memory for it.
Sequence of memory buffers.
Definition: buffer_sequence_view.h:72
mysqlns::resource::Allocator< Buffer_view_t > Buffer_allocator_t
Definition: buffer_sequence_view.h:77
Iterator_t end()
Iterator to the last buffer.
Definition: buffer_sequence_view.h:119
Iterator_t begin()
Iterator to the first buffer.
Definition: buffer_sequence_view.h:116
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:67
Result_t compute_new_size(Size_t old_size, Size_t requested_size) const
Compute the new size.
Definition: grow_calculator.cpp:39
Size_t get_max_size() const
Definition: grow_constraint.cpp:32
std::string debug_string() const
In debug mode, return a string that describes the internal structure of this object,...
Definition: grow_constraint.h:125
Owned, non-contiguous, growable memory buffer.
Definition: managed_buffer_sequence.h:114
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:144
Grow_status write(const Char_t *data, Size_t size)
Append the given data.
Definition: managed_buffer_sequence.h:319
Buffer_sequence_view< Char_tp, Container_tp > Buffer_sequence_view_t
Definition: managed_buffer_sequence.h:120
mysqlns::resource::Memory_resource Memory_resource_t
Definition: managed_buffer_sequence.h:131
typename Buffer_sequence_view_t::Container_t Container_t
Definition: rw_buffer_sequence.h:115
typename std::list< Buffer_view_t, Buffer_allocator_t > List_t
Definition: managed_buffer_sequence.h:482
Char_allocator_t m_char_allocator
Allocator to allocate buffer data (characters).
Definition: managed_buffer_sequence.h:531
typename Buffer_sequence_view_t::Buffer_allocator_t Buffer_allocator_t
Definition: managed_buffer_sequence.h:129
Managed_buffer_sequence & operator=(Managed_buffer_sequence &&)=delete
typename Buffer_sequence_view_t::Buffer_view_t Buffer_view_t
Definition: rw_buffer_sequence.h:112
Container_t m_buffers
Container of buffers.
Definition: managed_buffer_sequence.h:534
typename Vector_t::iterator Vector_iterator_t
Definition: managed_buffer_sequence.h:485
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:251
Managed_buffer_sequence(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:345
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 & operator=(Managed_buffer_sequence &)=delete
const Grow_calculator_t & get_grow_calculator() const
Return a const reference to the grow calculator.
Definition: managed_buffer_sequence.h:290
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:519
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:446
Grow_calculator Grow_calculator_t
Definition: managed_buffer_sequence.h:127
Grow_calculator_t m_grow_calculator
Determines how much memory to allocate when new memory is needed.
Definition: managed_buffer_sequence.h:528
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:393
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:374
typename Buffer_sequence_view_t::Char_t Char_t
Definition: rw_buffer_sequence.h:109
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:181
typename std::vector< Buffer_view_t, Buffer_allocator_t > Vector_t
Definition: managed_buffer_sequence.h:484
~Managed_buffer_sequence() override
Definition: managed_buffer_sequence.h:160
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:212
bool allocate_and_add_buffer(Size_t size)
Allocate and add a new buffer.
Definition: managed_buffer_sequence.h:418
Managed_buffer_sequence(Managed_buffer_sequence &)=delete
typename Buffer_sequence_view_t::Size_t Size_t
Definition: rw_buffer_sequence.h:110
void set_grow_calculator(const Grow_calculator_t &grow_calculator)
Set the grow calculator.
Definition: managed_buffer_sequence.h:295
typename List_t::iterator List_iterator_t
Definition: managed_buffer_sequence.h:483
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:106
const Buffer_sequence_view_t & write_part() const
Return a const reference to the write part.
Definition: rw_buffer_sequence.h:224
typename Buffer_sequence_view_t::Container_t Container_t
Definition: rw_buffer_sequence.h:115
void set_position(Size_t new_position)
Set the specified absolute position.
Definition: rw_buffer_sequence.h:167
typename Buffer_sequence_view_t::Buffer_view_t Buffer_view_t
Definition: rw_buffer_sequence.h:112
void increase_position(Size_t delta)
Move the position right, relative to the current position.
Definition: rw_buffer_sequence.h:186
typename Buffer_sequence_view_t::Iterator_t Iterator_t
Definition: rw_buffer_sequence.h:116
const Buffer_sequence_view_t & read_part() const
Return a const reference to the read part.
Definition: rw_buffer_sequence.h:218
Size_t capacity() const
Return the current size, i.e., total size of all buffers.
Definition: rw_buffer_sequence.h:215
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:476
typename Buffer_sequence_view_t::Const_iterator_t Const_iterator_t
Definition: rw_buffer_sequence.h:117
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:238
typename Buffer_sequence_view_t::Char_t Char_t
Definition: rw_buffer_sequence.h:109
typename Buffer_sequence_view_t::Size_t Size_t
Definition: rw_buffer_sequence.h:110
Definition: managed_buffer_sequence.h:55
constexpr T * allocate(size_type n)
Use the Memory_resource to allocate the given number of elements of type T.
Definition: allocator.h:84
constexpr void deallocate(T *p, size_type size)
Use the Memory_resource to deallocate the given pointer.
Definition: allocator.h:95
Polymorphism-free memory resource class with custom allocator and deallocator functions.
Definition: memory_resource.h:88
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.
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:37
@ 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.
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:420
size_t buffer_size(const ConstBufferSequence &buffers) noexcept
Definition: buffer.h:315
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:2870
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2874
#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:68
#define BAPI_TRACE
Definition: wrapper_functions.h:66
#define BAPI_VAR(v)
Definition: wrapper_functions.h:69