24 #ifndef STATICLIB_IO_ARRAY_SINK_HPP
25 #define STATICLIB_IO_ARRAY_SINK_HPP
31 #include "staticlib/config.hpp"
32 #include "staticlib/support.hpp"
40 namespace detail_array_sink {
43 if (size_bytes <= 0) {
46 return reinterpret_cast<char*
> (std::malloc(static_cast<size_t> (size_bytes)));
58 template<
typename AllocFun,
typename FreeFun>
99 size_t initial_capacity,
float grow_coef = 1.5):
100 alloc_fun(std::move(alloc_fun)),
101 free_fun(std::move(free_fun)),
102 grow_coef(grow_coef),
103 buf(alloc_fun(static_cast<int>(initial_capacity + 1))),
104 capacity(initial_capacity) {
105 if (
nullptr == buf) {
107 "Alloc error for capacity: [" + sl::support::to_string(capacity) +
"]"));
132 alloc_fun(std::move(other.alloc_fun)),
133 free_fun(std::move(other.free_fun)),
134 grow_coef(other.grow_coef),
136 capacity(other.capacity),
137 bufsize(other.bufsize) {
150 alloc_fun(std::move(other.alloc_fun));
151 free_fun(std::move(other.free_fun));
152 grow_coef = other.grow_coef;
154 capacity = other.capacity;
156 bufsize = other.bufsize;
165 if (
nullptr != buf) {
177 while (bufsize + span.
size() > capacity) {
178 capacity =
static_cast<size_t>(
static_cast<float>(capacity) * grow_coef);
179 char* nbuf = alloc_fun(static_cast<int>(capacity + 1));
181 "Alloc error for capacity: [" + sl::support::to_string(capacity) +
"]"));
182 std::memcpy(nbuf, buf, bufsize);
186 std::memcpy(buf + bufsize, span.
data(), span.
size());
187 bufsize += span.
size();
210 if (
nullptr != buf) {
218 throw io_exception(TRACEMSG(
"Cannot release invalid empty 'array_sink'"));
260 template <
typename AllocFun,
typename FreeFun>
262 size_t initial_capacity = 15) {