c43f8db Release v2.3.0 44c135f Update README to show dependencies used internally. ece54cd Add external licenses. 908cd5f Updated change log. 96dbf0e Change %-escape in OPL format. 98f6e27 Change write benchmark to interleave reading and writing. 39620ce Make writing of metadata configurable for XML and OPL output. e5a4e5e Add debug output format. 597390f Remove superfluous include and pragmas. ecc57b0 Update pbf reader/writer to use new protozero functions. 5d1e8d2 Update protozero from upstream. ef8746b Fix build on Windows. ddba46f Remove superfluous include. 098c57f Add some paranoia checks to pbf reader. 0f804c2 Try building with newer boost library on travis. 6f79d63 Use explicit return types on lambdas. 355f3b1 New PBF reader and writer based on protozero. 71d719b Add pbf writing benchmark. f014b4c Fix iwyu.sh script: Works now if build directory doesn't exist. a0ace49 Use utf8cpp header-only lib instead of boost for utf8 decoding. 796f18e Bugfix: Reading large XML files could block. 5a2bcbe Replace strcmp by std::string comparison in test. bc49e2c Bugfix: XML writer was not writing whitespace correctly. 61222f8 Fix 64bit byte swap. e56f090 Fix new CRC code on OSX and Windows. 70229aa Add low-level building blocks that allow calculating CRC of OSM data. 0968a66 Remove assert checking for unset version. 62e0261 Refactor test case. 4bfc7fc Allow instantiating osmium::geom::GEOSFactory with existing GEOS factory. e70af0c Remove calls to protobuf cleanup function im benchmarks and examples. 718518d Bugfix in OPL output. Relation member roles were not encoded. 759d5cb Rename parameter that had the same name as a type. 7054cab Provide (Typed)MemoryMapping constructors for backwards compatibility. d09f5d1 Fix typo. b4e578f Make memory mapping utility class more flexible. 633fa8e Travis build without sudo. 7ff23f1 Improved code setting file format from suffix/format argument. 90ef3b9 Remove some tests that didn't test much and failed on FreeBSD. af86273 Add some pragmas to disable warnings for GCC. efac7fd Fix some include problems found by IWYU. 79d2f4c Changed add_user() and add_role() in builders. Add add_member(). 9375d00 Add function to set tags from ptr + length. Improve TagBuilder tests. bafca20 Test helper: Use more const and have sub-builders in their own scope. f73c993 Simplify code. fee1710 Disable warning only when compiling with GCC. 74402f3 Merge pull request #98 from dforsi/master 2c4b449 Update to new upstream catch.hpp version. 1318732 Release v2.2.0 1873998 Add missing test. 2e5ea1d Do not add timestamp to html doc pages. 1b2ea89 Remove debug output. 0be9599 Improved parsing of ids, versions, uids, etc. from strings. 4308d80 Add second version of split_string utility function. f18c9e5 Move part of pbf.hpp into new pbf_type_conv.hpp. d201152 Use new DeltaEncode class in pbf writer. e205610 Add DeltaEncode/DeltaDecode utility classes. 32905d6 Bugfix: Actually throw the exception we are creating... d3e86d8 Add functions to convert item_type to zero-based index. daddf07 Bugfix: Programs writing OSM files can stall up to a second after writing. 00b0247 Add function to set the id of a relation member. f85316a Fix error message. 19bc6cc Fix name of travis install script. 719cd33 spatialite-bin package now available on travis cb03821 Shorten long test string (MSVC doesn't like it). c3440a6 Add BoolVector index class. da08073 Add min_op/max_op utility functions. 411d112 AppVeyor.yml: new links for binary deps 7d9095f add test for badly formatted timestamps a073f73 Add helper methods to DiffObject. 3b9819a Add GeoJSON factory using the RapidJSON library. 107bca5 Use a reference instead of a copy. a6943a4 Mark a few variables that are not changing as const. 51b7e53 Improved error message for geometry exceptions. 5c37a13 Some minor spelling fixes 8ae5723 Bugfix: Dense location store was written out only partially. 5994322 Add support for tiles. 2168bac Add has_map_type() method to map factory. a9634bd Add more tests for mercator projection. 3c13e4d Add functionality to create simple polygons from ways in geom factories. e8c5bb1 Use uint64_t as counter, so there can be no overflows. 07fc9b9 libsparsehash-dev now in travis package whitelist 820e112 Add coverage support to CMake config. 5e9f943 Bugfix: Use the right include to really allow any input file type. d4b48eb CMake: Make version string a cached variable. e6baccb Add (c)begin/end functions to TypedMemoryMapping. Removed get_addr(). 3e32710 Use size() from MemoryMapping in TypedMemoryMapping. 96390db Improve MemoryMapping class documentation. 60a6217 Do not round memory mapped files to page size boundaries. 4907cbe Bugfix: function name. cac01d8 Use _filelengthi64 on Windows instead of fstat(2). 6a25bdf Windows: Put invalid parameter handler into wrapper class. Re-enable test. 110df9b Add invalid parameter handler on Windows to test. 549ed5f Disable some tests (to find which one fails on appveyor). a5b8873 Use resize_file() in memory mapping test instead of ftruncate directly. 40e41d3 Use _chsize_s() instead of _chsize() on Windows. 048397e Refactoring: Use low-level util functions in DataFile. 6a033f9 Remove now unused Windows implementation of mmap. 3eccdbb Move dword_hi/lo functions into osmium::util namespace. be7351b Remove unused code. b859b18 Make dword_hi/lo functions inline. 2e3bc37 Simplify mmap_vector_base/anon/file. f819cf3 Always map full pages. Make sure files behind mapping are large enough. d0c84b6 Add some low-level helper functions for file system access. 62e8d91 Make DataFile constructor explicit. fba684c Fix memory mapping test for windows. 78a7fd5 Add constructor to DataFile to create tmp file with given size. f911893 Bugfix: typo. 1cf2739 Add AnonymousMemoryMapping class. 56eac30 Implement MemoryMapping::resize() function. 1a73262 Bugfix: Counter variables were too small. 1ade32c Fix include position. b03aec3 Fixed some bugs in new DataFile class/tests. f109534 Add DataFile utility class. 9ed3c43 Fix/cleanup some code. 4f326c9 Fix bug: Copy-and-paste error. 78a5b2f Use reinterpret_cast instead of static_cast to get HANDLE on Windows. 7baa318 Fix typo. e669069 Make huge value even huger to see if code reliable fails then. 66137ad Improved documentation of MemoryMapping and TypedMemoryMapping classes. 3121393 Add TypedMemoryMapping class. f45335e Default for get_addr() template type. 685bbaf Remove unused code from tests. ce65bd4 Fix some issue with new MemoryMapping class. e7b8e15 Added MemoryMapping wrapper class for mmap() and Windows equivalent. 6b1effe typo fixed 33d479d Refactored travis build. 4348522 Fix xml data test. 769b1e8 Bugfix: Better check for invalid locations. bba7e68 Appveyor: Disable test failing because of missing dependency. 3d40dc7 Link with /debug on MSVC, add note about LNK4099 warnings. 5ef051f Appveyor: Disable header builds, add benchmarks. ce7485e Reformat Appveyor config. c60e505 use shallow clones for faster git fetch 3b18bca Travis cleanups. b8dfac0 Cleanup travis build. 5f19838 Trying to fix travis gcc build... d4255a4 Remove -Wno-return-type from recommended options. 5f1a41b Add dump_as_array() function to maps. ff22f76 Add constructors and begin()/end() functions to VectorBasedSparseMultimap. c7e05dd Bugfix: Make REGISTER_MAP() macro work when called several time with same name parameter. abdc317 Bugfix: Mark cbegin() and cend() of mmap_vector_base as const functions. d81d439 Add close() function to mmap_vector_base class. d74cff2 Add function on Buffer to get iterator to specific offset. git-subtree-dir: third_party/libosmium git-subtree-split: c43f8db50d93912a8bec5cd9fea733f7fec05549
553 lines
21 KiB
C++
553 lines
21 KiB
C++
#ifndef OSMIUM_MEMORY_BUFFER_HPP
|
|
#define OSMIUM_MEMORY_BUFFER_HPP
|
|
|
|
/*
|
|
|
|
This file is part of Osmium (http://osmcode.org/libosmium).
|
|
|
|
Copyright 2013-2015 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 <algorithm>
|
|
#include <cassert>
|
|
#include <cstddef>
|
|
#include <cstring>
|
|
#include <functional>
|
|
#include <iterator>
|
|
#include <stdexcept>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include <osmium/memory/item.hpp>
|
|
#include <osmium/memory/item_iterator.hpp>
|
|
#include <osmium/osm/entity.hpp>
|
|
|
|
namespace osmium {
|
|
|
|
/**
|
|
* Exception thrown by the osmium::memory::Buffer class when somebody tries
|
|
* to write data into a buffer and it doesn't fit. Buffers with internal
|
|
* memory management will not throw this exception, but increase their size.
|
|
*/
|
|
struct buffer_is_full : public std::runtime_error {
|
|
|
|
buffer_is_full() :
|
|
std::runtime_error("Osmium buffer is full") {
|
|
}
|
|
|
|
}; // struct buffer_is_full
|
|
|
|
/**
|
|
* @brief Memory management of items in buffers and iterators over this data.
|
|
*/
|
|
namespace memory {
|
|
|
|
/**
|
|
* A memory area for storing OSM objects and other items. Each item stored
|
|
* has a type and a length. See the Item class for details.
|
|
*
|
|
* Data can be added to a buffer piece by piece using reserve_space() and
|
|
* add_item(). After all data that together forms an item is added, it must
|
|
* be committed using the commit() call. Usually this is done through the
|
|
* Builder class and its derived classes.
|
|
*
|
|
* You can iterate over all items in a buffer using the iterators returned
|
|
* by begin(), end(), cbegin(), and cend().
|
|
*
|
|
* Buffers exist in two flavours, those with external memory management and
|
|
* those with internal memory management. If you already have some memory
|
|
* with data in it (for instance read from disk), you create a Buffer with
|
|
* external memory management. It is your job then to free the memory once
|
|
* the buffer isn't used any more. If you don't have memory already, you can
|
|
* create a Buffer object and have it manage the memory internally. It will
|
|
* dynamically allocate memory and free it again after use.
|
|
*
|
|
* By default, if a buffer gets full it will throw a buffer_is_full exception.
|
|
* You can use the set_full_callback() method to set a callback functor
|
|
* which will be called instead of throwing an exception.
|
|
*/
|
|
class Buffer {
|
|
|
|
public:
|
|
|
|
enum class auto_grow : bool {
|
|
yes = true,
|
|
no = false
|
|
}; // enum class auto_grow
|
|
|
|
private:
|
|
|
|
std::vector<unsigned char> m_memory;
|
|
unsigned char* m_data;
|
|
size_t m_capacity;
|
|
size_t m_written;
|
|
size_t m_committed;
|
|
auto_grow m_auto_grow {auto_grow::no};
|
|
std::function<void(Buffer&)> m_full;
|
|
|
|
public:
|
|
|
|
typedef Item value_type;
|
|
|
|
/**
|
|
* The constructor without any parameters creates a non-initialized
|
|
* buffer, ie an empty hull of a buffer that has no actual memory
|
|
* associated with it. It can be used to signify end-of-input.
|
|
*/
|
|
Buffer() noexcept :
|
|
m_memory(),
|
|
m_data(nullptr),
|
|
m_capacity(0),
|
|
m_written(0),
|
|
m_committed(0) {
|
|
}
|
|
|
|
/**
|
|
* Constructs an externally memory-managed buffer using the given
|
|
* memory and size.
|
|
*
|
|
* @param data A pointer to some already initialized data.
|
|
* @param size The size of the initialized data.
|
|
* @throws std::invalid_argument When the size isn't a multiple of the alignment.
|
|
*/
|
|
explicit Buffer(unsigned char* data, size_t size) :
|
|
m_memory(),
|
|
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");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Constructs an externally memory-managed buffer with the given
|
|
* capacity that already contains 'committed' bytes of data.
|
|
*
|
|
* @param data A pointer to some (possibly initialized) data.
|
|
* @param capacity The size of the memory for this buffer.
|
|
* @param committed The size of the initialized data. If this is 0, the buffer startes out empty.
|
|
* @throws std::invalid_argument When the capacity or committed isn't a multiple of the alignment.
|
|
*/
|
|
explicit Buffer(unsigned char* data, size_t capacity, size_t committed) :
|
|
m_memory(),
|
|
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");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create an internally memory-managed buffer with the given capacity.
|
|
* different in that it internally gets dynamic memory of the
|
|
* required size. The dynamic memory will be automatically
|
|
* freed when the Buffer is destroyed.
|
|
*/
|
|
explicit Buffer(size_t capacity, auto_grow auto_grow = auto_grow::yes) :
|
|
m_memory(capacity),
|
|
m_data(m_memory.data()),
|
|
m_capacity(capacity),
|
|
m_written(0),
|
|
m_committed(0),
|
|
m_auto_grow(auto_grow) {
|
|
if (capacity % align_bytes != 0) {
|
|
throw std::invalid_argument("buffer capacity needs to be multiple of alignment");
|
|
}
|
|
}
|
|
|
|
// buffers can not be copied
|
|
Buffer(const Buffer&) = delete;
|
|
Buffer& operator=(const Buffer&) = delete;
|
|
|
|
// buffers can be moved
|
|
Buffer(Buffer&&) = default;
|
|
Buffer& operator=(Buffer&&) = default;
|
|
|
|
~Buffer() = default;
|
|
|
|
/**
|
|
* Return a pointer to data inside the buffer.
|
|
*/
|
|
unsigned char* data() const noexcept {
|
|
return m_data;
|
|
}
|
|
|
|
/**
|
|
* Returns the capacity of the buffer, ie how many bytes it can contain.
|
|
*/
|
|
size_t capacity() const noexcept {
|
|
return m_capacity;
|
|
}
|
|
|
|
/**
|
|
* Returns the number of bytes already filled in this buffer.
|
|
*/
|
|
size_t committed() const noexcept {
|
|
return m_committed;
|
|
}
|
|
|
|
/**
|
|
* Returns the number of bytes currently filled in this buffer that
|
|
* are not yet committed.
|
|
*/
|
|
size_t written() const noexcept {
|
|
return m_written;
|
|
}
|
|
|
|
/**
|
|
* This tests if the current state of the buffer is aligned
|
|
* properly. Can be used for asserts.
|
|
*/
|
|
bool is_aligned() const noexcept {
|
|
return (m_written % align_bytes == 0) && (m_committed % align_bytes == 0);
|
|
}
|
|
|
|
/**
|
|
* Set functor to be called whenever the buffer is full
|
|
* instead of throwing buffer_is_full.
|
|
*/
|
|
void set_full_callback(std::function<void(Buffer&)> full) {
|
|
m_full = full;
|
|
}
|
|
|
|
/**
|
|
* Grow capacity of this buffer to the given size.
|
|
* This works only with internally memory-managed buffers.
|
|
* If the given size is not larger than the current capacity, nothing is done.
|
|
* Already written but not committed data is discarded.
|
|
*
|
|
* @param size New capacity.
|
|
*/
|
|
void grow(size_t size) {
|
|
if (m_memory.empty()) {
|
|
throw std::logic_error("Can't grow Buffer if it doesn't use internal memory management.");
|
|
}
|
|
if (m_capacity < size) {
|
|
if (size % align_bytes != 0) {
|
|
throw std::invalid_argument("buffer capacity needs to be multiple of alignment");
|
|
}
|
|
m_memory.resize(size);
|
|
m_data = m_memory.data();
|
|
m_capacity = size;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Mark currently written bytes in the buffer as committed.
|
|
*
|
|
* @returns Last number of committed bytes before this commit.
|
|
*/
|
|
size_t commit() {
|
|
assert(is_aligned());
|
|
|
|
const size_t offset = m_committed;
|
|
m_committed = m_written;
|
|
return offset;
|
|
}
|
|
|
|
/**
|
|
* Roll back changes in buffer to last committed state.
|
|
*/
|
|
void rollback() {
|
|
m_written = m_committed;
|
|
}
|
|
|
|
/**
|
|
* Clear the buffer.
|
|
*
|
|
* @returns Number of bytes in the buffer before it was cleared.
|
|
*/
|
|
size_t clear() {
|
|
const size_t committed = m_committed;
|
|
m_written = 0;
|
|
m_committed = 0;
|
|
return committed;
|
|
}
|
|
|
|
/**
|
|
* Get the data in the buffer at the given offset.
|
|
*
|
|
* @tparam T Type we want to the data to be interpreted as.
|
|
* @returns Reference of given type pointing to the data in the buffer.
|
|
*/
|
|
template <class T>
|
|
T& get(const size_t offset) const {
|
|
return *reinterpret_cast<T*>(&m_data[offset]);
|
|
}
|
|
|
|
/**
|
|
* Reserve space of given size in buffer and return pointer to it.
|
|
* This is the only way of adding data to the buffer. You reserve
|
|
* the space and then fill it.
|
|
*
|
|
* Note that you have to eventually call commit() to actually
|
|
* commit this data.
|
|
*
|
|
* If there isn't enough space in the buffer, one of three things
|
|
* can happen:
|
|
*
|
|
* * If you have set a callback with set_full_callback(), it is
|
|
* called. After the call returns, you must have either grown
|
|
* the buffer or cleared it by calling buffer.clear().
|
|
* * If no callback is defined and this buffer uses internal
|
|
* memory management, the buffers capacity is grown, so that
|
|
* the new data will fit.
|
|
* * Else the buffer_is_full exception is thrown.
|
|
*
|
|
* @param size Number of bytes to reserve.
|
|
* @returns Pointer to reserved space. Note that this pointer is
|
|
* only guaranteed to be valid until the next call to
|
|
* reserve_space().
|
|
* @throws osmium::buffer_is_full Might be thrown if the buffer is full.
|
|
*/
|
|
unsigned char* reserve_space(const size_t size) {
|
|
if (m_written + size > m_capacity) {
|
|
if (m_full) {
|
|
m_full(*this);
|
|
} else if (!m_memory.empty() && (m_auto_grow == auto_grow::yes)) {
|
|
// double buffer size until there is enough space
|
|
size_t new_capacity = m_capacity * 2;
|
|
while (m_written + size > new_capacity) {
|
|
new_capacity *= 2;
|
|
}
|
|
grow(new_capacity);
|
|
} else {
|
|
throw osmium::buffer_is_full();
|
|
}
|
|
}
|
|
unsigned char* data = &m_data[m_written];
|
|
m_written += size;
|
|
return data;
|
|
}
|
|
|
|
/**
|
|
* Add an item to the buffer. The size of the item is stored inside
|
|
* the item, so we know how much memory to copy.
|
|
*
|
|
* Note that you have to eventually call commit() to actually
|
|
* commit this data.
|
|
*
|
|
* @tparam T Class of the item to be copied.
|
|
* @param item Reference to the item to be copied.
|
|
* @returns Reference to newly copied data in the buffer.
|
|
*/
|
|
template <class T>
|
|
T& add_item(const T& item) {
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* Add committed contents of the given buffer to this buffer.
|
|
*
|
|
* Note that you have to eventually call commit() to actually
|
|
* commit this data.
|
|
*/
|
|
void add_buffer(const Buffer& buffer) {
|
|
unsigned char* target = reserve_space(buffer.committed());
|
|
std::copy_n(reinterpret_cast<const unsigned char*>(buffer.data()), buffer.committed(), target);
|
|
}
|
|
|
|
/**
|
|
* Add an item to the buffer. This function is provided so that
|
|
* you can use std::back_inserter.
|
|
*/
|
|
void push_back(const osmium::memory::Item& item) {
|
|
add_item(item);
|
|
commit();
|
|
}
|
|
|
|
/**
|
|
* These iterators can be used to iterate over all items in
|
|
* a buffer.
|
|
*/
|
|
template <class T>
|
|
using t_iterator = osmium::memory::ItemIterator<T>;
|
|
|
|
template <class T>
|
|
using t_const_iterator = osmium::memory::ItemIterator<const T>;
|
|
|
|
typedef t_iterator<osmium::OSMEntity> iterator;
|
|
typedef t_const_iterator<osmium::OSMEntity> const_iterator;
|
|
|
|
template <class T>
|
|
t_iterator<T> begin() {
|
|
return t_iterator<T>(m_data, m_data + m_committed);
|
|
}
|
|
|
|
iterator begin() {
|
|
return iterator(m_data, m_data + m_committed);
|
|
}
|
|
|
|
template <class T>
|
|
t_iterator<T> get_iterator(size_t offset) {
|
|
return t_iterator<T>(m_data + offset, m_data + m_committed);
|
|
}
|
|
|
|
iterator get_iterator(size_t offset) {
|
|
return iterator(m_data + offset, m_data + m_committed);
|
|
}
|
|
|
|
template <class T>
|
|
t_iterator<T> end() {
|
|
return t_iterator<T>(m_data + m_committed, m_data + m_committed);
|
|
}
|
|
|
|
iterator end() {
|
|
return iterator(m_data + m_committed, m_data + m_committed);
|
|
}
|
|
|
|
template <class T>
|
|
t_const_iterator<T> cbegin() const {
|
|
return t_const_iterator<T>(m_data, m_data + m_committed);
|
|
}
|
|
|
|
const_iterator cbegin() const {
|
|
return const_iterator(m_data, m_data + m_committed);
|
|
}
|
|
|
|
template <class T>
|
|
t_const_iterator<T> get_iterator(size_t offset) const {
|
|
return t_const_iterator<T>(m_data + offset, m_data + m_committed);
|
|
}
|
|
|
|
const_iterator get_iterator(size_t offset) const {
|
|
return const_iterator(m_data + offset, m_data + m_committed);
|
|
}
|
|
|
|
template <class T>
|
|
t_const_iterator<T> cend() const {
|
|
return t_const_iterator<T>(m_data + m_committed, m_data + m_committed);
|
|
}
|
|
|
|
const_iterator cend() const {
|
|
return const_iterator(m_data + m_committed, m_data + m_committed);
|
|
}
|
|
|
|
template <class T>
|
|
t_const_iterator<T> begin() const {
|
|
return cbegin<T>();
|
|
}
|
|
|
|
const_iterator begin() const {
|
|
return cbegin();
|
|
}
|
|
|
|
template <class T>
|
|
t_const_iterator<T> end() const {
|
|
return cend<T>();
|
|
}
|
|
|
|
const_iterator end() const {
|
|
return cend();
|
|
}
|
|
|
|
/**
|
|
* In a bool context any initialized buffer is true.
|
|
*/
|
|
explicit operator bool() const {
|
|
return m_data != nullptr;
|
|
}
|
|
|
|
friend void swap(Buffer& lhs, Buffer& rhs) {
|
|
using std::swap;
|
|
|
|
swap(lhs.m_memory, rhs.m_memory);
|
|
swap(lhs.m_data, rhs.m_data);
|
|
swap(lhs.m_capacity, rhs.m_capacity);
|
|
swap(lhs.m_written, rhs.m_written);
|
|
swap(lhs.m_committed, rhs.m_committed);
|
|
}
|
|
|
|
/**
|
|
* Purge removed items from the buffer. This is done by moving all
|
|
* non-removed items forward in the buffer overwriting removed
|
|
* items and then correcting the m_written and m_committed numbers.
|
|
*
|
|
* Note that calling this function invalidates all iterators on this
|
|
* buffer and all offsets in this buffer.
|
|
*
|
|
* For every non-removed item that moves its position, the function
|
|
* 'moving_in_buffer' is called on the given callback object with
|
|
* the old and new offsets in the buffer where the object used to
|
|
* be and is now, respectively. This call can be used to update any
|
|
* indexes.
|
|
*/
|
|
template <class TCallbackClass>
|
|
void purge_removed(TCallbackClass* 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());
|
|
size_t old_offset = static_cast<size_t>(it_read.data() - data());
|
|
size_t new_offset = static_cast<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<size_t>(it_write.data() - data());
|
|
m_committed = m_written;
|
|
}
|
|
|
|
}; // class Buffer
|
|
|
|
inline bool operator==(const Buffer& lhs, const Buffer& rhs) noexcept {
|
|
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
|