2014-08-06 17:39:07 +02:00
|
|
|
/*
|
2019-08-16 13:16:45 +02:00
|
|
|
* Copyright 2003-2019 Max Kellermann <max.kellermann@gmail.com>
|
2014-08-06 17:39:07 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FOREIGN_FIFO_BUFFER_HXX
|
|
|
|
#define FOREIGN_FIFO_BUFFER_HXX
|
|
|
|
|
|
|
|
#include "WritableBuffer.hxx"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2014-08-06 17:39:07 +02:00
|
|
|
#include <cstddef>
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <utility>
|
2014-08-06 17:39:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A first-in-first-out buffer: you can append data at the end, and
|
|
|
|
* read data from the beginning. This class automatically shifts the
|
|
|
|
* buffer as needed. It is not thread safe.
|
|
|
|
*
|
|
|
|
* This class does not manage buffer memory. It will not allocate or
|
|
|
|
* free any memory, it only manages the contents of an existing buffer
|
|
|
|
* given to the constructor.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
class ForeignFifoBuffer {
|
|
|
|
public:
|
2020-01-03 15:38:02 +01:00
|
|
|
using size_type = std::size_t;
|
2019-08-16 13:16:45 +02:00
|
|
|
using Range = WritableBuffer<T>;
|
2020-01-03 15:49:29 +01:00
|
|
|
using pointer = typename Range::pointer;
|
|
|
|
using const_pointer = typename Range::const_pointer;
|
2014-08-06 17:39:07 +02:00
|
|
|
|
|
|
|
protected:
|
2017-04-24 20:16:18 +02:00
|
|
|
size_type head = 0, tail = 0, capacity;
|
2014-08-06 17:39:07 +02:00
|
|
|
T *data;
|
|
|
|
|
|
|
|
public:
|
2018-08-20 14:53:39 +02:00
|
|
|
explicit constexpr ForeignFifoBuffer(std::nullptr_t n) noexcept
|
2017-04-24 20:16:18 +02:00
|
|
|
:capacity(0), data(n) {}
|
2014-08-06 17:39:07 +02:00
|
|
|
|
2018-08-20 14:53:39 +02:00
|
|
|
constexpr ForeignFifoBuffer(T *_data, size_type _capacity) noexcept
|
2017-04-24 20:16:18 +02:00
|
|
|
:capacity(_capacity), data(_data) {}
|
2014-08-06 17:39:07 +02:00
|
|
|
|
2018-08-20 14:53:39 +02:00
|
|
|
ForeignFifoBuffer(ForeignFifoBuffer &&src) noexcept
|
2014-08-06 17:39:07 +02:00
|
|
|
:head(src.head), tail(src.tail),
|
|
|
|
capacity(src.capacity), data(src.data) {
|
|
|
|
src.SetNull();
|
|
|
|
}
|
|
|
|
|
2018-08-20 14:53:39 +02:00
|
|
|
ForeignFifoBuffer &operator=(ForeignFifoBuffer &&src) noexcept {
|
2014-08-06 17:39:07 +02:00
|
|
|
head = src.head;
|
|
|
|
tail = src.tail;
|
|
|
|
capacity = src.capacity;
|
|
|
|
data = src.data;
|
|
|
|
src.SetNull();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-08-16 09:57:47 +02:00
|
|
|
void swap(ForeignFifoBuffer<T> &other) noexcept {
|
2019-08-16 09:55:49 +02:00
|
|
|
using std::swap;
|
|
|
|
swap(head, other.head);
|
|
|
|
swap(tail, other.tail);
|
|
|
|
swap(capacity, other.capacity);
|
|
|
|
swap(data, other.data);
|
2014-08-07 09:02:24 +02:00
|
|
|
}
|
|
|
|
|
2019-08-16 09:57:55 +02:00
|
|
|
friend void swap(ForeignFifoBuffer<T> &a, ForeignFifoBuffer<T> &b) noexcept {
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2018-08-20 14:53:39 +02:00
|
|
|
constexpr bool IsNull() const noexcept {
|
2014-08-06 17:39:07 +02:00
|
|
|
return data == nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-20 14:53:39 +02:00
|
|
|
constexpr bool IsDefined() const noexcept {
|
2014-08-06 17:39:07 +02:00
|
|
|
return !IsNull();
|
|
|
|
}
|
|
|
|
|
2018-08-20 14:53:39 +02:00
|
|
|
T *GetBuffer() noexcept {
|
2014-08-06 17:39:07 +02:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2018-08-20 14:53:39 +02:00
|
|
|
constexpr size_type GetCapacity() const noexcept {
|
2014-08-06 17:39:07 +02:00
|
|
|
return capacity;
|
|
|
|
}
|
|
|
|
|
2018-08-20 14:53:39 +02:00
|
|
|
void SetNull() noexcept {
|
2014-08-06 17:39:07 +02:00
|
|
|
head = tail = 0;
|
|
|
|
capacity = 0;
|
|
|
|
data = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-20 14:53:39 +02:00
|
|
|
void SetBuffer(T *_data, size_type _capacity) noexcept {
|
2014-08-06 17:39:07 +02:00
|
|
|
assert(_data != nullptr);
|
|
|
|
assert(_capacity > 0);
|
|
|
|
|
|
|
|
head = tail = 0;
|
|
|
|
capacity = _capacity;
|
|
|
|
data = _data;
|
|
|
|
}
|
|
|
|
|
2018-08-20 14:53:39 +02:00
|
|
|
void MoveBuffer(T *new_data, size_type new_capacity) noexcept {
|
2014-08-06 17:39:07 +02:00
|
|
|
assert(new_capacity >= tail - head);
|
|
|
|
|
|
|
|
std::move(data + head, data + tail, new_data);
|
|
|
|
data = new_data;
|
|
|
|
capacity = new_capacity;
|
|
|
|
tail -= head;
|
|
|
|
head = 0;
|
|
|
|
}
|
|
|
|
|
2018-08-20 14:53:39 +02:00
|
|
|
void Clear() noexcept {
|
2014-08-06 17:39:07 +02:00
|
|
|
head = tail = 0;
|
|
|
|
}
|
|
|
|
|
2018-08-20 14:53:39 +02:00
|
|
|
constexpr bool empty() const noexcept {
|
2014-08-06 17:39:07 +02:00
|
|
|
return head == tail;
|
|
|
|
}
|
|
|
|
|
2018-08-20 14:53:39 +02:00
|
|
|
constexpr bool IsFull() const noexcept {
|
2014-08-06 17:39:07 +02:00
|
|
|
return head == 0 && tail == capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares writing. Returns a buffer range which may be written.
|
2017-04-24 20:19:14 +02:00
|
|
|
* When you are finished, call Append().
|
2014-08-06 17:39:07 +02:00
|
|
|
*/
|
2018-08-20 14:53:39 +02:00
|
|
|
Range Write() noexcept {
|
2017-11-10 19:24:33 +01:00
|
|
|
if (empty())
|
2014-08-07 16:11:00 +02:00
|
|
|
Clear();
|
|
|
|
else if (tail == capacity)
|
|
|
|
Shift();
|
|
|
|
|
2014-08-06 17:39:07 +02:00
|
|
|
return Range(data + tail, capacity - tail);
|
|
|
|
}
|
|
|
|
|
2018-08-20 14:53:39 +02:00
|
|
|
bool WantWrite(size_type n) noexcept {
|
2014-08-06 17:39:07 +02:00
|
|
|
if (tail + n <= capacity)
|
|
|
|
/* enough space after the tail */
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const size_type in_use = tail - head;
|
|
|
|
const size_type required_capacity = in_use + n;
|
|
|
|
if (required_capacity > capacity)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Shift();
|
|
|
|
assert(tail + n <= capacity);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expands the tail of the buffer, after data has been written to
|
2017-04-24 20:19:14 +02:00
|
|
|
* the buffer returned by Write().
|
2014-08-06 17:39:07 +02:00
|
|
|
*/
|
2018-08-20 14:53:39 +02:00
|
|
|
void Append(size_type n) noexcept {
|
2014-08-06 17:39:07 +02:00
|
|
|
assert(tail <= capacity);
|
|
|
|
assert(n <= capacity);
|
|
|
|
assert(tail + n <= capacity);
|
|
|
|
|
|
|
|
tail += n;
|
|
|
|
}
|
|
|
|
|
2018-08-20 14:53:39 +02:00
|
|
|
constexpr size_type GetAvailable() const noexcept {
|
2014-08-06 17:52:56 +02:00
|
|
|
return tail - head;
|
|
|
|
}
|
|
|
|
|
2014-08-06 17:39:07 +02:00
|
|
|
/**
|
|
|
|
* Return a buffer range which may be read. The buffer pointer is
|
|
|
|
* writable, to allow modifications while parsing.
|
|
|
|
*/
|
2018-08-20 14:53:39 +02:00
|
|
|
constexpr Range Read() const noexcept {
|
2014-08-06 17:39:07 +02:00
|
|
|
return Range(data + head, tail - head);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks a chunk as consumed.
|
|
|
|
*/
|
2018-08-20 14:53:39 +02:00
|
|
|
void Consume(size_type n) noexcept {
|
2014-08-06 17:39:07 +02:00
|
|
|
assert(tail <= capacity);
|
|
|
|
assert(head <= tail);
|
|
|
|
assert(n <= tail);
|
|
|
|
assert(head + n <= tail);
|
|
|
|
|
|
|
|
head += n;
|
|
|
|
}
|
|
|
|
|
2020-01-03 15:49:29 +01:00
|
|
|
size_type Read(pointer p, size_type n) noexcept {
|
2014-08-06 17:39:07 +02:00
|
|
|
auto range = Read();
|
|
|
|
if (n > range.size)
|
|
|
|
n = range.size;
|
|
|
|
std::copy_n(range.data, n, p);
|
|
|
|
Consume(n);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2014-08-07 08:53:02 +02:00
|
|
|
/**
|
|
|
|
* Move as much data as possible from the specified buffer.
|
|
|
|
*
|
|
|
|
* @return the number of items moved
|
|
|
|
*/
|
2018-08-20 14:53:39 +02:00
|
|
|
size_type MoveFrom(ForeignFifoBuffer<T> &src) noexcept {
|
2014-08-07 08:53:02 +02:00
|
|
|
auto r = src.Read();
|
|
|
|
auto w = Write();
|
2018-08-20 15:21:05 +02:00
|
|
|
|
|
|
|
if (w.size < r.size && head > 0) {
|
|
|
|
/* if the source contains more data than we
|
|
|
|
can append at the tail, try to make more
|
|
|
|
room by shifting the head to 0 */
|
|
|
|
Shift();
|
|
|
|
w = Write();
|
|
|
|
}
|
|
|
|
|
2021-01-21 16:37:50 +01:00
|
|
|
const auto n = std::min(r.size, w.size);
|
2014-08-07 08:53:02 +02:00
|
|
|
|
|
|
|
std::move(r.data, r.data + n, w.data);
|
|
|
|
Append(n);
|
|
|
|
src.Consume(n);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2014-08-06 17:39:07 +02:00
|
|
|
protected:
|
2018-08-20 14:53:39 +02:00
|
|
|
void Shift() noexcept {
|
2014-08-06 17:39:07 +02:00
|
|
|
if (head == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(head <= capacity);
|
|
|
|
assert(tail <= capacity);
|
|
|
|
assert(tail >= head);
|
|
|
|
|
|
|
|
std::move(data + head, data + tail, data);
|
|
|
|
|
|
|
|
tail -= head;
|
|
|
|
head = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|