48 alignas(
alignof(std::atomic<int>))
68 std::size_t binary_size) {
70 const std::size_t total_memory_size = header_size + binary_size;
72 void* raw_ptr = std::malloc(total_memory_size);
73 if (raw_ptr ==
nullptr) {
76 throw std::bad_alloc();
81 ptr->binary_size = binary_size;
82 ptr->binary_capacity = binary_size;
83 ptr->is_reference_count_enabled =
false;
96 constexpr std::size_t total_memory_size = 128;
97 static_assert(total_memory_size > header_size);
99 void* raw_ptr = std::malloc(total_memory_size);
100 if (raw_ptr ==
nullptr) {
103 throw std::bad_alloc();
108 ptr->binary_size = 0U;
109 ptr->binary_capacity = total_memory_size - header_size;
110 ptr->is_reference_count_enabled =
false;
132 if (new_capacity >= required_size) {
137 const std::size_t new_total_memory_size = header_size + new_capacity;
140 std::realloc(buffer, new_total_memory_size);
141 if (raw_ptr ==
nullptr) {
144 throw std::bad_alloc();
149 ptr->binary_capacity = new_total_memory_size - header_size;
162 assert(buffer->is_reference_count_enabled);
164 return *
reinterpret_cast<std::atomic<int>*
>(buffer->reference_count_buffer);
173 if (buffer ==
nullptr) {
176 if (buffer->is_reference_count_enabled) {
177 using AtomicType = std::atomic<int>;
190 assert(!buffer->is_reference_count_enabled);
191 new (
static_cast<void*
>(buffer->reference_count_buffer))
193 buffer->is_reference_count_enabled =
true;
214 const int reference_count_before =
216 if (reference_count_before == 1) {
Namespace of internal implementations.
void decrease_reference_count_of_sharable_binary(SharableBinaryHeader *buffer) noexcept
Decrease a reference count of a buffer of sharable binary data, and deallocate the buffer if possible...
char * binary_buffer_of(SharableBinaryHeader *buffer) noexcept
Get buffer of binary data from a buffer of sharable binary data.
SharableBinaryHeader * expand_sharable_binary(SharableBinaryHeader *buffer, std::size_t required_size)
Expand a buffer of sharable binary data.
void add_reference_count_of_sharable_binary(SharableBinaryHeader *buffer) noexcept
Add a reference count of a buffer of sharable binary data.
constexpr std::size_t SHARABLE_BINARY_ALIGNMENT
Alignment of sharable binary data.
void deallocate_sharable_binary(SharableBinaryHeader *buffer) noexcept
Deallocate a buffer of sharable binary data.
std::atomic< int > & reference_count_of(SharableBinaryHeader *buffer) noexcept
Access the reference count of a buffer of sharable binary data.
SharableBinaryHeader * allocate_shared_binary()
Allocate a buffer of sharable binary data with automatic initial size.
void enable_reference_count_of_sharable_binary(SharableBinaryHeader *buffer) noexcept
Enable reference count of a buffer of sharable binary data.
SharableBinaryHeader * allocate_sharable_binary(std::size_t binary_size)
Allocate a buffer of sharable binary data.