Loading...
Searching...
No Matches
buffer.hpp
Go to the documentation of this file.
#ifndef OSMIUM_MEMORY_BUFFER_HPP
#define OSMIUM_MEMORY_BUFFER_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/memory/item.hpp>
#include <osmium/memory/item_iterator.hpp>
#include <osmium/osm/entity.hpp>
#include <osmium/util/compatibility.hpp>
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstring>
#include <functional>
#include <iterator>
#include <memory>
#include <stdexcept>
#include <utility>
namespace osmium {
struct OSMIUM_EXPORT buffer_is_full : public std::runtime_error {
buffer_is_full() :
std::runtime_error{"Osmium buffer is full"} {
}
}; // struct buffer_is_full
namespace memory {
class Buffer {
public:
// This is needed so we can call std::back_inserter() on a Buffer.
using value_type = Item;
enum class auto_grow {
no = 0,
yes = 1,
internal = 2
}; // enum class auto_grow
private:
std::unique_ptr<Buffer> m_next_buffer;
std::unique_ptr<unsigned char[]> m_memory{};
unsigned char* m_data = nullptr;
std::size_t m_capacity = 0;
std::size_t m_written = 0;
std::size_t m_committed = 0;
#ifndef NDEBUG
uint8_t m_builder_count = 0;
#endif
auto_grow m_auto_grow{auto_grow::no};
static std::size_t calculate_capacity(std::size_t capacity) noexcept {
enum {
// The majority of all Nodes will fit into this size.
min_capacity = 64
};
if (capacity < min_capacity) {
return min_capacity;
}
return padded_length(capacity);
}
void grow_internal() {
assert(m_data && "This must be a valid buffer");
if (!m_memory) {
throw std::logic_error{"Can't grow Buffer if it doesn't use internal memory management."};
}
std::unique_ptr<Buffer> old{new Buffer{std::move(m_memory), m_capacity, m_committed}};
m_memory = std::unique_ptr<unsigned char[]>{new unsigned char[m_capacity]};
m_data = m_memory.get();
m_written -= m_committed;
std::copy_n(old->data() + m_committed, m_written, m_data);
m_committed = 0;
old->m_next_buffer = std::move(m_next_buffer);
m_next_buffer = std::move(old);
}
public:
Buffer() noexcept = default;
explicit Buffer(unsigned char* data, std::size_t size) :
m_data(data),
m_capacity(size),
m_written(size),
m_committed(size) {
if (size % align_bytes != 0) {
throw std::invalid_argument{"buffer size needs to be multiple of alignment"};
}
}
explicit Buffer(unsigned char* data, std::size_t capacity, std::size_t committed) :
m_data(data),
m_capacity(capacity),
m_written(committed),
m_committed(committed) {
if (capacity % align_bytes != 0) {
throw std::invalid_argument{"buffer capacity needs to be multiple of alignment"};
}
if (committed % align_bytes != 0) {
throw std::invalid_argument{"buffer parameter 'committed' needs to be multiple of alignment"};
}
if (committed > capacity) {
throw std::invalid_argument{"buffer parameter 'committed' can not be larger than capacity"};
}
}
explicit Buffer(std::unique_ptr<unsigned char[]> data, std::size_t capacity, std::size_t committed) :
m_memory(std::move(data)),
m_data(m_memory.get()),
m_capacity(capacity),
m_written(committed),
m_committed(committed) {
if (capacity % align_bytes != 0) {
throw std::invalid_argument{"buffer capacity needs to be multiple of alignment"};
}
if (committed % align_bytes != 0) {
throw std::invalid_argument{"buffer parameter 'committed' needs to be multiple of alignment"};
}
if (committed > capacity) {
throw std::invalid_argument{"buffer parameter 'committed' can not be larger than capacity"};
}
}
explicit Buffer(std::size_t capacity, auto_grow auto_grow = auto_grow::yes) :
m_memory(new unsigned char[calculate_capacity(capacity)]),
m_data(m_memory.get()),
m_capacity(calculate_capacity(capacity)),
m_auto_grow(auto_grow) {
}
// buffers can not be copied
Buffer(const Buffer&) = delete;
Buffer& operator=(const Buffer&) = delete;
// buffers can be moved
Buffer(Buffer&& other) noexcept :
m_next_buffer(std::move(other.m_next_buffer)),
m_memory(std::move(other.m_memory)),
m_data(other.m_data),
m_capacity(other.m_capacity),
m_written(other.m_written),
m_committed(other.m_committed),
#ifndef NDEBUG
m_builder_count(other.m_builder_count),
#endif
m_auto_grow(other.m_auto_grow) {
other.m_data = nullptr;
other.m_capacity = 0;
other.m_written = 0;
other.m_committed = 0;
#ifndef NDEBUG
other.m_builder_count = 0;
#endif
}
Buffer& operator=(Buffer&& other) noexcept {
m_next_buffer = std::move(other.m_next_buffer);
m_memory = std::move(other.m_memory);
m_data = other.m_data;
m_capacity = other.m_capacity;
m_written = other.m_written;
m_committed = other.m_committed;
#ifndef NDEBUG
m_builder_count = other.m_builder_count;
#endif
m_auto_grow = other.m_auto_grow;
other.m_data = nullptr;
other.m_capacity = 0;
other.m_written = 0;
other.m_committed = 0;
#ifndef NDEBUG
other.m_builder_count = 0;
#endif
return *this;
}
~Buffer() noexcept = default;
#ifndef NDEBUG
void increment_builder_count() noexcept {
++m_builder_count;
}
void decrement_builder_count() noexcept {
assert(m_builder_count > 0);
--m_builder_count;
}
uint8_t builder_count() const noexcept {
return m_builder_count;
}
#endif
unsigned char* data() const noexcept {
assert(m_data && "This must be a valid buffer");
return m_data;
}
std::size_t capacity() const noexcept {
return m_capacity;
}
std::size_t committed() const noexcept {
return m_committed;
}
std::size_t written() const noexcept {
return m_written;
}
bool is_aligned() const noexcept {
assert(m_data && "This must be a valid buffer");
return (m_written % align_bytes == 0) && (m_committed % align_bytes == 0);
}
void grow(std::size_t size) {
assert(m_data && "This must be a valid buffer");
if (!m_memory) {
throw std::logic_error{"Can't grow Buffer if it doesn't use internal memory management."};
}
size = calculate_capacity(size);
if (m_capacity < size) {
std::unique_ptr<unsigned char[]> memory{new unsigned char[size]};
std::copy_n(m_memory.get(), m_capacity, memory.get());
using std::swap;
swap(m_memory, memory);
m_data = m_memory.get();
m_capacity = size;
}
}
bool has_nested_buffers() const noexcept {
return m_next_buffer != nullptr;
}
std::unique_ptr<Buffer> get_last_nested() {
assert(has_nested_buffers());
Buffer* buffer = this;
while (buffer->m_next_buffer->has_nested_buffers()) {
buffer = buffer->m_next_buffer.get();
}
return std::move(buffer->m_next_buffer);
}
std::size_t commit() {
assert(m_data && "This must be a valid buffer");
assert(m_builder_count == 0 && "Make sure there are no Builder objects still in scope");
assert(is_aligned());
const std::size_t offset = m_committed;
m_committed = m_written;
return offset;
}
void rollback() {
assert(m_data && "This must be a valid buffer");
assert(m_builder_count == 0 && "Make sure there are no Builder objects still in scope");
m_written = m_committed;
}
std::size_t clear() {
assert(m_builder_count == 0 && "Make sure there are no Builder objects still in scope");
const std::size_t num_used_bytes = m_committed;
m_written = 0;
m_committed = 0;
return num_used_bytes;
}
template <typename T>
T& get(const std::size_t offset) const {
assert(m_data && "This must be a valid buffer");
assert(offset % alignof(T) == 0 && "Wrong alignment");
return *reinterpret_cast<T*>(&m_data[offset]);
}
unsigned char* reserve_space(const std::size_t size) {
assert(m_data && "This must be a valid buffer");
// if there's still not enough space, then try growing the buffer.
if (m_written + size > m_capacity) {
if (!m_memory || m_auto_grow == auto_grow::no) {
throw osmium::buffer_is_full{};
}
if (m_auto_grow == auto_grow::internal && m_committed != 0) {
grow_internal();
}
if (m_written + size > m_capacity) {
// double buffer size until there is enough space
std::size_t new_capacity = m_capacity * 2;
while (m_written + size > new_capacity) {
new_capacity *= 2;
}
grow(new_capacity);
}
}
unsigned char* reserved_space = &m_data[m_written];
m_written += size;
return reserved_space;
}
template <typename T>
T& add_item(const T& item) {
assert(m_data && "This must be a valid buffer");
unsigned char* target = reserve_space(item.padded_size());
std::copy_n(reinterpret_cast<const unsigned char*>(&item), item.padded_size(), target);
return *reinterpret_cast<T*>(target);
}
void add_buffer(const Buffer& buffer) {
assert(m_data && "This must be a valid buffer");
assert(buffer && "Buffer parameter must be a valid buffer");
assert(m_builder_count == 0 && "Make sure there are no Builder objects still in scope");
unsigned char* target = reserve_space(buffer.committed());
std::copy_n(buffer.data(), buffer.committed(), target);
}
void push_back(const osmium::memory::Item& item) {
assert(m_data && "This must be a valid buffer");
assert(m_builder_count == 0 && "Make sure there are no Builder objects still in scope");
add_item(item);
commit();
}
template <typename T>
using t_iterator = osmium::memory::ItemIterator<T>;
template <typename T>
using t_const_iterator = osmium::memory::ItemIterator<const T>;
using iterator = t_iterator<osmium::OSMEntity>;
using const_iterator = t_const_iterator<osmium::OSMEntity>;
template <typename T>
ItemIteratorRange<T> select() {
return ItemIteratorRange<T>{m_data, m_data + m_committed};
}
template <typename T>
ItemIteratorRange<const T> select() const {
return ItemIteratorRange<const T>{m_data, m_data + m_committed};
}
template <typename T>
t_iterator<T> begin() {
assert(m_data && "This must be a valid buffer");
return t_iterator<T>(m_data, m_data + m_committed);
}
iterator begin() {
assert(m_data && "This must be a valid buffer");
return {m_data, m_data + m_committed};
}
template <typename T>
t_iterator<T> get_iterator(std::size_t offset) {
assert(m_data && "This must be a valid buffer");
assert(offset % alignof(T) == 0 && "Wrong alignment");
return {m_data + offset, m_data + m_committed};
}
iterator get_iterator(std::size_t offset) {
assert(m_data && "This must be a valid buffer");
assert(offset % alignof(OSMEntity) == 0 && "Wrong alignment");
return {m_data + offset, m_data + m_committed};
}
template <typename T>
t_iterator<T> end() {
assert(m_data && "This must be a valid buffer");
return {m_data + m_committed, m_data + m_committed};
}
iterator end() {
assert(m_data && "This must be a valid buffer");
return {m_data + m_committed, m_data + m_committed};
}
template <typename T>
t_const_iterator<T> cbegin() const {
assert(m_data && "This must be a valid buffer");
return {m_data, m_data + m_committed};
}
const_iterator cbegin() const {
assert(m_data && "This must be a valid buffer");
return {m_data, m_data + m_committed};
}
template <typename T>
t_const_iterator<T> get_iterator(std::size_t offset) const {
assert(m_data && "This must be a valid buffer");
assert(offset % alignof(T) == 0 && "Wrong alignment");
return {m_data + offset, m_data + m_committed};
}
const_iterator get_iterator(std::size_t offset) const {
assert(m_data && "This must be a valid buffer");
assert(offset % alignof(OSMEntity) == 0 && "Wrong alignment");
return {m_data + offset, m_data + m_committed};
}
template <typename T>
t_const_iterator<T> cend() const {
assert(m_data && "This must be a valid buffer");
return {m_data + m_committed, m_data + m_committed};
}
const_iterator cend() const {
assert(m_data && "This must be a valid buffer");
return {m_data + m_committed, m_data + m_committed};
}
template <typename T>
t_const_iterator<T> begin() const {
return cbegin<T>();
}
const_iterator begin() const {
return cbegin();
}
template <typename T>
t_const_iterator<T> end() const {
return cend<T>();
}
const_iterator end() const {
return cend();
}
explicit operator bool() const noexcept {
return m_data != nullptr;
}
void swap(Buffer& other) {
using std::swap;
swap(m_next_buffer, other.m_next_buffer);
swap(m_memory, other.m_memory);
swap(m_data, other.m_data);
swap(m_capacity, other.m_capacity);
swap(m_written, other.m_written);
swap(m_committed, other.m_committed);
swap(m_auto_grow, other.m_auto_grow);
}
template <typename TCallbackClass>
void purge_removed(TCallbackClass* callback) {
assert(m_data && "This must be a valid buffer");
assert(callback);
if (begin() == end()) {
return;
}
iterator it_write = begin();
iterator next;
for (iterator it_read = begin(); it_read != end(); it_read = next) {
next = std::next(it_read);
if (!it_read->removed()) {
if (it_read != it_write) {
assert(it_read.data() >= data());
assert(it_write.data() >= data());
const auto old_offset = static_cast<std::size_t>(it_read.data() - data());
const auto new_offset = static_cast<std::size_t>(it_write.data() - data());
callback->moving_in_buffer(old_offset, new_offset);
std::memmove(it_write.data(), it_read.data(), it_read->padded_size());
}
it_write.advance_once();
}
}
assert(it_write.data() >= data());
m_written = static_cast<std::size_t>(it_write.data() - data());
m_committed = m_written;
}
void purge_removed() {
assert(m_data && "This must be a valid buffer");
if (begin() == end()) {
return;
}
iterator it_write = begin();
iterator next;
for (iterator it_read = begin(); it_read != end(); it_read = next) {
next = std::next(it_read);
if (!it_read->removed()) {
if (it_read != it_write) {
assert(it_read.data() >= data());
assert(it_write.data() >= data());
std::memmove(it_write.data(), it_read.data(), it_read->padded_size());
}
it_write.advance_once();
}
}
assert(it_write.data() >= data());
m_written = static_cast<std::size_t>(it_write.data() - data());
m_committed = m_written;
}
}; // class Buffer
inline void swap(Buffer& lhs, Buffer& rhs) {
lhs.swap(rhs);
}
inline bool operator==(const Buffer& lhs, const Buffer& rhs) noexcept {
if (!lhs || !rhs) {
return !lhs && !rhs;
}
return lhs.data() == rhs.data() && lhs.capacity() == rhs.capacity() && lhs.committed() == rhs.committed();
}
inline bool operator!=(const Buffer& lhs, const Buffer& rhs) noexcept {
return !(lhs == rhs);
}
} // namespace memory
} // namespace osmium
#endif // OSMIUM_MEMORY_BUFFER_HPP
osmium::memory::ItemIterator
Definition item_iterator.hpp:59
osmium::memory::Item
Definition item.hpp:105
compatibility.hpp
OSMIUM_EXPORT
#define OSMIUM_EXPORT
Definition compatibility.hpp:54
entity.hpp
item.hpp
item_iterator.hpp
osmium::io::end
InputIterator< Reader > end(Reader &)
Definition reader_iterator.hpp:47
osmium::io::begin
InputIterator< Reader > begin(Reader &reader)
Definition reader_iterator.hpp:43
osmium::io::read_meta::no
@ no
osmium::io::read_meta::yes
@ yes
osmium::memory::padded_length
constexpr std::size_t padded_length(std::size_t length) noexcept
Definition item.hpp:64
osmium::memory::align_bytes
@ align_bytes
Definition item.hpp:61
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::operator==
bool operator==(const Changeset &lhs, const Changeset &rhs)
Definition changeset.hpp:440
osmium::operator!=
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition changeset.hpp:444
std
Definition location.hpp:555