Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/serialiser/include/IoBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ struct IoBuffer {
_capacity = capacity;
return _buffer;
}
const auto oldCapacity = _capacity;
_capacity = capacity;
if (dynamic_cast<Reallocator *>(_allocator.resource())) {
// N.B. 'realloc' is safe as long as the de-allocation is done via 'free'
Expand All @@ -129,7 +130,7 @@ struct IoBuffer {
auto *tBuffer = _allocator.allocate(_capacity);
// std::memmove(tBuffer, _buffer, std::min(_size, size) * sizeof(uint8_t));
std::copy(_buffer, _buffer + std::min(_size, _capacity) * sizeof(uint8_t), tBuffer);
_allocator.deallocate(_buffer, _capacity);
_allocator.deallocate(_buffer, oldCapacity);
_buffer = tBuffer;
}
return _buffer;
Expand Down Expand Up @@ -256,15 +257,17 @@ struct IoBuffer {
[[nodiscard]] explicit IoBuffer(uint8_t *data, std::size_t size, bool owning)
: IoBuffer({ data, size }, Allocator(owning ? ThrowingAllocator::defaultOwning() : ThrowingAllocator::defaultNonOwning())) {};

[[nodiscard]] IoBuffer(const IoBuffer &other) noexcept
: IoBuffer(other._capacity, other._allocator.select_on_container_copy_construction()) {
[[nodiscard]] IoBuffer(const IoBuffer& other) noexcept
: IoBuffer(other._capacity, (dynamic_cast<ThrowingAllocator*>(other._allocator.resource()) != nullptr)
? other._allocator.select_on_container_copy_construction(): Allocator(other._allocator.resource()))
{
_buffer = resize(other._size);
_position = other._position;
std::memmove(_buffer, other._buffer, _size * sizeof(uint8_t));
}

[[nodiscard]] IoBuffer(IoBuffer &&other) noexcept
: _allocator(other._allocator.select_on_container_copy_construction())
: _allocator(other._allocator.resource())
, _buffer(std::exchange(other._buffer, nullptr))
, _position(other._position)
, _size(other._size)
Expand Down Expand Up @@ -343,12 +346,12 @@ struct IoBuffer {
}
}

constexpr ByteBufferPointer resize(const std::size_t size) noexcept {
_size = size;
if (size <= _capacity) {
return _buffer;
constexpr ByteBufferPointer resize(const std::size_t newSize) noexcept {
if (newSize > _capacity) {
reserve(newSize);
}
return reserve(size);
_size = newSize;
return _buffer;
}

template<MetaInfo meta = WITH, Number I>
Expand Down