1 #ifndef OSMIUM_MEMORY_BUFFER_HPP 2 #define OSMIUM_MEMORY_BUFFER_HPP 61 std::runtime_error(
"Osmium buffer is full") {
147 explicit Buffer(
unsigned char* data,
size_t size) :
154 throw std::invalid_argument(
"buffer size needs to be multiple of alignment");
169 explicit Buffer(
unsigned char* data,
size_t capacity,
size_t committed) :
172 m_capacity(capacity),
173 m_written(committed),
174 m_committed(committed) {
176 throw std::invalid_argument(
"buffer capacity needs to be multiple of alignment");
179 throw std::invalid_argument(
"buffer parameter 'committed' needs to be multiple of alignment");
198 m_memory(new unsigned char[capacity]),
199 m_data(m_memory.get()),
200 m_capacity(capacity),
205 throw std::invalid_argument(
"buffer capacity needs to be multiple of alignment");
224 unsigned char*
data() const noexcept {
310 throw std::logic_error(
"Can't grow Buffer if it doesn't use internal memory management.");
312 if (m_capacity < size) {
314 throw std::invalid_argument(
"buffer capacity needs to be multiple of alignment");
316 std::unique_ptr<unsigned char[]> memory(
new unsigned char[size]);
317 std::copy_n(m_memory.get(), m_capacity, memory.get());
319 swap(m_memory, memory);
320 m_data = m_memory.get();
337 assert(is_aligned());
339 const size_t offset = m_committed;
340 m_committed = m_written;
351 m_written = m_committed;
362 const size_t committed = m_committed;
378 template <
typename T>
379 T&
get(
const size_t offset)
const {
381 return *
reinterpret_cast<T*
>(&m_data[offset]);
420 if (m_written + size > m_capacity && m_full) {
424 if (m_written + size > m_capacity) {
425 if (m_memory && (m_auto_grow == auto_grow::yes)) {
427 size_t new_capacity = m_capacity * 2;
428 while (m_written + size > new_capacity) {
436 unsigned char* data = &m_data[m_written];
456 template <
typename T>
459 unsigned char* target = reserve_space(item.padded_size());
460 std::copy_n(reinterpret_cast<const unsigned char*>(&item), item.padded_size(), target);
461 return *
reinterpret_cast<T*
>(target);
475 assert(m_data && buffer);
476 unsigned char* target = reserve_space(buffer.
committed());
498 template <
typename T>
505 template <
typename T>
528 template <
typename T>
544 return iterator(m_data, m_data + m_committed);
556 template <
typename T>
573 return iterator(m_data + offset, m_data + m_committed);
584 template <
typename T>
587 return t_iterator<T>(m_data + m_committed, m_data + m_committed);
600 return iterator(m_data + m_committed, m_data + m_committed);
603 template <
typename T>
614 template <
typename T>
625 template <
typename T>
633 return const_iterator(m_data + m_committed, m_data + m_committed);
636 template <
typename T>
645 template <
typename T>
657 explicit operator bool() const noexcept {
658 return m_data !=
nullptr;
689 template <
typename TCallbackClass>
700 next = std::next(it_read);
701 if (!it_read->removed()) {
702 if (it_read != it_write) {
703 assert(it_read.data() >= data());
704 assert(it_write.
data() >= data());
705 size_t old_offset =
static_cast<size_t>(it_read.data() - data());
706 size_t new_offset =
static_cast<size_t>(it_write.
data() - data());
707 callback->moving_in_buffer(old_offset, new_offset);
708 std::memmove(it_write.
data(), it_read.data(), it_read->padded_size());
714 assert(it_write.
data() >= data());
715 m_written =
static_cast<size_t>(it_write.
data() - data());
716 m_committed = m_written;
736 return lhs.data() == rhs.data() && lhs.capacity() == rhs.capacity() && lhs.committed() == rhs.committed();
740 return ! (lhs == rhs);
747 #endif // OSMIUM_MEMORY_BUFFER_HPP size_t m_written
Definition: buffer.hpp:114
void swap(Buffer &other)
Definition: buffer.hpp:661
t_const_iterator< T > cend() const
Definition: buffer.hpp:626
const_iterator begin() const
Definition: buffer.hpp:641
size_t clear()
Definition: buffer.hpp:361
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
bool is_aligned() const noexcept
Definition: buffer.hpp:260
size_t written() const noexcept
Definition: buffer.hpp:250
OSMIUM_DEPRECATED void set_full_callback(std::function< void(Buffer &)> full)
Definition: buffer.hpp:285
iterator get_iterator(size_t offset)
Definition: buffer.hpp:571
void grow(size_t size)
Definition: buffer.hpp:307
const_iterator end() const
Definition: buffer.hpp:650
Definition: item_iterator.hpp:119
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:222
unsigned char * m_data
Definition: buffer.hpp:112
constexpr item_size_type align_bytes
Definition: item.hpp:53
Buffer(unsigned char *data, size_t capacity, size_t committed)
Definition: buffer.hpp:169
Definition: reader_iterator.hpp:39
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:721
ItemIterator< TMember > & advance_once()
Definition: item_iterator.hpp:168
Buffer(size_t capacity, auto_grow auto_grow=auto_grow::yes)
Definition: buffer.hpp:197
unsigned char * data() const
Definition: item_iterator.hpp:189
t_iterator< T > end()
Definition: buffer.hpp:585
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
T & add_item(const T &item)
Definition: buffer.hpp:457
void purge_removed(TCallbackClass *callback)
Definition: buffer.hpp:690
t_iterator< T > begin()
Definition: buffer.hpp:529
void add_buffer(const Buffer &buffer)
Definition: buffer.hpp:474
const_iterator cbegin() const
Definition: buffer.hpp:609
size_t m_committed
Definition: buffer.hpp:115
unsigned char * data() const noexcept
Definition: buffer.hpp:224
Buffer() noexcept
Definition: buffer.hpp:129
size_t capacity() const noexcept
Definition: buffer.hpp:233
osmium::io::InputIterator< osmium::io::Reader > end(osmium::io::Reader &)
Definition: reader_iterator.hpp:45
unsigned char * reserve_space(const size_t size)
Definition: buffer.hpp:417
auto_grow m_auto_grow
Definition: buffer.hpp:116
void push_back(const osmium::memory::Item &item)
Definition: buffer.hpp:488
iterator end()
Definition: buffer.hpp:598
iterator begin()
Definition: buffer.hpp:542
const_iterator cend() const
Definition: buffer.hpp:631
size_t m_capacity
Definition: buffer.hpp:113
t_const_iterator< T > get_iterator(size_t offset) const
Definition: buffer.hpp:615
size_t committed() const noexcept
Definition: buffer.hpp:241
Buffer(unsigned char *data, size_t size)
Definition: buffer.hpp:147
t_const_iterator< T > end() const
Definition: buffer.hpp:646
Definition: buffer.hpp:97
Definition: buffer.hpp:58
const_iterator get_iterator(size_t offset) const
Definition: buffer.hpp:620
t_const_iterator< T > cbegin() const
Definition: buffer.hpp:604
t_const_iterator< T > begin() const
Definition: buffer.hpp:637
auto_grow
Definition: buffer.hpp:104
buffer_is_full()
Definition: buffer.hpp:60
osmium::io::InputIterator< osmium::io::Reader > begin(osmium::io::Reader &reader)
Definition: reader_iterator.hpp:41
void rollback()
Definition: buffer.hpp:349
std::unique_ptr< unsigned char[]> m_memory
Definition: buffer.hpp:111
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:431
t_iterator< T > get_iterator(size_t offset)
Definition: buffer.hpp:557
std::function< void(Buffer &)> m_full
Definition: buffer.hpp:117
size_t commit()
Definition: buffer.hpp:335