28namespace qb::allocator {
38template <
typename T,
typename U>
41 return sizeof(T) /
sizeof(U) +
static_cast<bool>(
sizeof(T) %
sizeof(U));
58 using base_type = std::allocator<T>;
59 constexpr static const std::size_t _SIZE = 4096;
116 rhs._begin = rhs._end = 0;
117 rhs._flag_front =
false;
149 rhs._begin = rhs._end = 0;
150 rhs._flag_front =
false;
172 [[nodiscard]]
inline std::size_t
182 [[nodiscard]]
inline T *
192 [[nodiscard]]
inline T *
202 [[nodiscard]]
inline T *
212 [[nodiscard]]
inline const T *
222 [[nodiscard]]
inline const T *
232 [[nodiscard]]
inline std::size_t
247 if (new_size <=
size())
347 const auto save_index =
_end;
349 return _data + save_index;
356 return _data + nb_item;
358 std::size_t new_capacity;
361 new_capacity =
_factor * _SIZE;
362 }
while (new_capacity - nb_item <
size);
364 const auto new_data = base_type::allocate(new_capacity);
365 std::memcpy(new_data,
_data +
_begin, nb_item *
sizeof(T));
374 return _data + nb_item;
385 template <
typename U,
typename... _Init>
389 return *(
new (
reinterpret_cast<U *
>(
allocate_back(BUCKET_SIZE)))
390 U(std::forward<_Init>(init)...));
402 template <
typename U,
typename... _Init>
407 U(std::forward<_Init>(init)...));
437 template <
typename U,
typename... _Init>
441 return *(
new (
reinterpret_cast<U *
>(
allocate(BUCKET_SIZE)))
442 U(std::forward<_Init>(init)...));
452 template <
typename U>
456 return *
reinterpret_cast<U *
>(
468 template <
typename U>
471 return *
reinterpret_cast<U *
>(
482 template <
typename U>
486 return *
reinterpret_cast<U *
>(
498 template <
typename U>
501 return *
reinterpret_cast<U *
>(
561 std::swap(*
reinterpret_cast<CacheLine *
>(
this),
573 template <
typename _It>
605 template <
typename U>
625 pipe &operator=(
pipe const &) = default;
626 pipe &operator=(
pipe &&) noexcept = default;
635 template <typename U>
638 return put(
static_cast<const U &
>(rhs));
648 template <
typename U>
651 return put(std::to_string(rhs));
661 template <std::
size_t _Size>
663 put(
const char (&str)[_Size]) {
664 memcpy(allocate_back(_Size - 1), str, _Size - 1);
675 template <std::
size_t _Size>
689 template <
typename T>
691 put(std::vector<T>
const &vec) {
692 memcpy(allocate_back(vec.size()),
reinterpret_cast<const char *
>(vec.data()),
705 template <
typename T, std::
size_t _Size>
707 put(std::array<T, _Size>
const &arr) {
708 memcpy(allocate_back(arr.size()),
reinterpret_cast<const char *
>(arr.data()),
721 template <
typename _It>
723 put(_It begin, _It
const &end) {
724 auto out = allocate_back(end - begin);
725 while (begin != end) {
740 template <
typename U>
753 template <
typename U>
766 pipe &put(
char const *data, std::size_t size)
noexcept;
775 pipe &write(
const char *data, std::size_t size)
noexcept;
782 std::string str() const noexcept;
789 std::string_view view() const noexcept;
794pipe<
char> &
pipe<
char>::put<
char>(const
char &c);
797pipe<
char> &
pipe<
char>::put<
unsigned char>(const
unsigned char &c);
800pipe<
char> &
pipe<
char>::put<const
char *>(const
char *const &c);
803pipe<
char> &
pipe<
char>::put<std::
string>(std::
string const &str);
806pipe<
char> &
pipe<
char>::put<std::string_view>(std::string_view const &str);
821template <typename stream, typename = std::enable_if_t<
822 !std::is_same_v<stream, qb::allocator::
pipe<
char>>>>
824operator<<(stream &os, qb::allocator::
pipe<
char> const &p) {
825 os << std::string_view(p.begin(), p.size());
Branch prediction hint utilities for performance optimization.
Base class for the extensible buffer.
Definition pipe.h:57
void reset() noexcept
Completely resets the buffer.
Definition pipe.h:297
void clear() noexcept
Clears the buffer content (alias for reset)
Definition pipe.h:307
T * end() const noexcept
Returns a pointer just after the last valid element.
Definition pipe.h:203
T * _data
Buffer data.
Definition pipe.h:67
void free_back(std::size_t const size) noexcept
Frees elements at the end of the buffer.
Definition pipe.h:269
U & recycle_back(U const &data)
Copies an object to the end of the buffer.
Definition pipe.h:454
std::size_t _begin
Index of first valid element.
Definition pipe.h:62
U & allocate_back(_Init &&...init)
Allocates and constructs an object of type U at the end of the buffer.
Definition pipe.h:387
void reorder() noexcept
Reorganizes the buffer to consolidate free space.
Definition pipe.h:511
std::size_t _factor
Buffer expansion factor.
Definition pipe.h:66
void reset(std::size_t const begin) noexcept
Resets the buffer to a specific position.
Definition pipe.h:282
base_pipe & operator=(base_pipe &&rhs) noexcept
Move assignment operator.
Definition pipe.h:141
std::size_t capacity() const noexcept
Returns the total capacity of the buffer.
Definition pipe.h:173
base_pipe & operator=(base_pipe const &rhs)
Copy assignment operator.
Definition pipe.h:126
std::size_t size() const noexcept
Returns the number of valid elements in the buffer.
Definition pipe.h:233
bool _flag_front
Indicates if the last allocation was at the beginning.
Definition pipe.h:64
base_pipe()
Default constructor.
Definition pipe.h:75
bool empty() const noexcept
Checks if the buffer is empty.
Definition pipe.h:317
void flush() const noexcept
Flushes the buffer (synchronization operation, no-op in this class)
Definition pipe.h:526
std::size_t _capacity
Total buffer capacity.
Definition pipe.h:65
U & allocate(_Init &&...init)
Allocates space and constructs an object.
Definition pipe.h:439
U & recycle(U const &data)
Copies an object to the beginning or end of the buffer.
Definition pipe.h:484
~base_pipe()
Destructor.
Definition pipe.h:162
T * data() const noexcept
Returns a pointer to the buffer data.
Definition pipe.h:183
void resize(std::size_t new_size)
Resizes the buffer.
Definition pipe.h:246
void free(std::size_t const size) noexcept
Frees a specified number of elements.
Definition pipe.h:329
T * begin() const noexcept
Returns a pointer to the first valid element.
Definition pipe.h:193
void reserve(std::size_t const size)
Reserves space in the buffer.
Definition pipe.h:536
auto * allocate_back(std::size_t const size)
Allocates space at the end of the buffer.
Definition pipe.h:345
const T * cend() const noexcept
Returns a constant pointer just after the last valid element.
Definition pipe.h:223
base_pipe(base_pipe const &rhs)
Copy constructor.
Definition pipe.h:90
auto allocate(std::size_t const size)
Allocates space at the beginning or end of the buffer.
Definition pipe.h:419
U & allocate_size(std::size_t const size, _Init &&...init)
Allocates space with a custom size and constructs an object.
Definition pipe.h:404
const T * cbegin() const noexcept
Returns a constant pointer to the first valid element.
Definition pipe.h:213
base_pipe(base_pipe &&rhs) noexcept
Move constructor.
Definition pipe.h:108
std::size_t _end
Index just after the last valid element.
Definition pipe.h:63
U & recycle_back(U const &data, std::size_t const size)
Copies data to the end of the buffer.
Definition pipe.h:470
U & recycle(U const &data, std::size_t const size)
Copies data to the beginning or end of the buffer.
Definition pipe.h:500
void free_front(std::size_t const size) noexcept
Frees elements at the beginning of the buffer.
Definition pipe.h:259
Extensible buffer optimized for performance.
Definition pipe.h:552
void swap(pipe &rhs) noexcept
Swaps content with another buffer.
Definition pipe.h:560
pipe & put(_It begin, _It const &end)
Adds elements from an iterator range.
Definition pipe.h:575
pipe & put(T const *data, std::size_t const size)
Adds elements from an array.
Definition pipe.h:593
const_pointer c_str() const noexcept
Get C-style string representation.
Definition string.h:435
constexpr std::size_t size() const noexcept
Get the string length.
Definition string.h:509
std::ostream & operator<<(std::ostream &os, qb::Actor const &actor)
Stream output operator for Actor objects.
std::ostream & operator<<(std::ostream &os, const qb::jsonb &j)
Stream output operator for qb::jsonb.
Definition json.h:324
json::string_t string
JSON string type from nlohmann::json.
Definition json.h:67
bool likely(bool expr)
Hint for branch prediction when a condition is expected to be true.
Definition branch_hints.h:49
Pipe pipe
Alias for the Pipe class.
Definition Pipe.h:117
Defines a base class to make derived classes non-copyable.
Platform-specific alignment macros, cache-line definitions, and related utilities.
Fixed-size string implementation optimized for performance.
A structure automatically aligned to cache line boundaries.
Definition prefix.h:108
constexpr auto getItemSize()
Calculates the number of elements of type U needed to store an element of type T.
Definition pipe.h:40