65 std::exchange(other.next_allocation_point_,
nullptr)),
68 std::exchange(other.last_direct_allocation_,
nullptr)) {}
112 void* prev_buffer = *head;
118 void* prev_buffer = *head;
131 [[nodiscard]]
void*
allocate(std::size_t size, std::size_t alignment) {
140 if (result !=
nullptr) {
167 void* ptr = std::malloc(size);
168 if (ptr ==
nullptr) {
169 throw std::bad_alloc();
178 constexpr std::size_t next_buffer_size_rate = 2U;
179 constexpr std::size_t max_buffer_size =
static_cast<std::size_t
>(1)
210 std::size_t size, std::size_t alignment) {
230 std::size_t size, std::size_t alignment) {
232 std::size_t size_with_header_and_padding_space =
233 size + std::max(
sizeof(
void*), alignment);
237 *header = prev_allocation;
238 void* ptr_after_header = header + 1;
239 void* result = std::align(alignment, size, ptr_after_header,
240 size_with_header_and_padding_space);
241 assert(result !=
nullptr);
270 instance1.swap(instance2);
Class of an allocator which releases memory only when the allocator is destroyed.
static constexpr std::size_t initial_buffer_size
Size of the initial buffer.
std::size_t remaining_buffer_
Remaining size of the current buffer.
~monotonic_allocator()
Destructor.
monotonic_allocator()
Constructor.
monotonic_allocator(monotonic_allocator &&other) noexcept
Move constructor.
void * last_direct_allocation_
Last pointer to the memory allocated directly using malloc function.
void swap(monotonic_allocator &other) noexcept
Swap with another instance.
std::size_t current_buffer_size_
Size of the current buffer.
void * allocate_directly(std::size_t size, std::size_t alignment)
Allocate memory directly using malloc function.
void * current_buffer_
Current buffer.
void deallocate(void *ptr) noexcept
Deallocate memory.
static constexpr std::size_t max_allocation_from_buffer
Maximum size of memory allocated from buffer.
void prepare_buffer()
Prepare a buffer.
static void * allocate_buffer(std::size_t size)
Allocate a buffer.
monotonic_allocator & operator=(monotonic_allocator &&other) noexcept
Move assignment operator.
monotonic_allocator & operator=(const monotonic_allocator &) noexcept
Copy assignment operator.
monotonic_allocator(const monotonic_allocator &) noexcept
Copy constructor.
void change_buffer()
Prepare the next buffer.
void * next_allocation_point_
Pointer to the byte from which the next allocation starts.
void * allocate(std::size_t size, std::size_t alignment)
Allocate memory.
void * try_allocate_from_buffer(std::size_t size, std::size_t alignment)
Try to allocate from the current buffer.
Namespace of this project.
void swap(msgpack_light::monotonic_allocator &instance1, msgpack_light::monotonic_allocator &instance2) noexcept
Swap two instances.
void swap(msgpack_light::details::basic_binary_buffer &instance1, msgpack_light::details::basic_binary_buffer &instance2) noexcept
Implementation of std::swap for msgpack_light::details::basic_binary_buffer.