@@ -1,78 +0,0 @@
|
||||
#ifndef OSMIUM_DETAIL_MMAP_VECTOR_ANON_HPP
|
||||
#define OSMIUM_DETAIL_MMAP_VECTOR_ANON_HPP
|
||||
|
||||
/*
|
||||
|
||||
This file is part of Osmium (http://osmcode.org/libosmium).
|
||||
|
||||
Copyright 2013,2014 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.
|
||||
|
||||
*/
|
||||
|
||||
#ifdef __linux__
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include <osmium/index/detail/typed_mmap.hpp>
|
||||
#include <osmium/index/detail/mmap_vector_base.hpp>
|
||||
|
||||
namespace osmium {
|
||||
|
||||
namespace detail {
|
||||
|
||||
/**
|
||||
* This class looks and behaves like STL vector, but uses mmap internally.
|
||||
*/
|
||||
template <typename T>
|
||||
class mmap_vector_anon : public mmap_vector_base<T, mmap_vector_anon> {
|
||||
|
||||
public:
|
||||
|
||||
mmap_vector_anon() :
|
||||
mmap_vector_base<T, osmium::detail::mmap_vector_anon>(
|
||||
-1,
|
||||
osmium::detail::mmap_vector_size_increment,
|
||||
0,
|
||||
osmium::detail::typed_mmap<T>::map(osmium::detail::mmap_vector_size_increment)) {
|
||||
}
|
||||
|
||||
void reserve(size_t new_capacity) {
|
||||
if (new_capacity > this->capacity()) {
|
||||
this->data(osmium::detail::typed_mmap<T>::remap(this->data(), this->capacity(), new_capacity));
|
||||
this->m_capacity = new_capacity;
|
||||
}
|
||||
}
|
||||
|
||||
}; // class mmap_vector_anon
|
||||
|
||||
} // namespace detail
|
||||
|
||||
} // namespace osmium
|
||||
|
||||
#endif // __linux__
|
||||
|
||||
#endif // OSMIUM_DETAIL_MMAP_VECTOR_ANON_HPP
|
||||
-183
@@ -1,183 +0,0 @@
|
||||
#ifndef OSMIUM_DETAIL_MMAP_VECTOR_BASE_HPP
|
||||
#define OSMIUM_DETAIL_MMAP_VECTOR_BASE_HPP
|
||||
|
||||
/*
|
||||
|
||||
This file is part of Osmium (http://osmcode.org/libosmium).
|
||||
|
||||
Copyright 2013,2014 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 <cstddef>
|
||||
#include <new>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <osmium/index/detail/typed_mmap.hpp>
|
||||
|
||||
namespace osmium {
|
||||
|
||||
namespace detail {
|
||||
|
||||
constexpr size_t mmap_vector_size_increment = 1024 * 1024;
|
||||
|
||||
/**
|
||||
* This is a base class for implementing classes that look like
|
||||
* STL vector but use mmap internally. This class can not be used
|
||||
* on it's own. Use the derived classes mmap_vector_anon or
|
||||
* mmap_vector_file.
|
||||
*/
|
||||
template <typename T, template <typename> class TDerived>
|
||||
class mmap_vector_base {
|
||||
|
||||
protected:
|
||||
|
||||
int m_fd;
|
||||
size_t m_capacity;
|
||||
size_t m_size;
|
||||
T* m_data;
|
||||
|
||||
explicit mmap_vector_base(int fd, size_t capacity, size_t size, T* data) :
|
||||
m_fd(fd),
|
||||
m_capacity(capacity),
|
||||
m_size(size),
|
||||
m_data(data) {
|
||||
}
|
||||
|
||||
explicit mmap_vector_base(int fd, size_t capacity, size_t size) :
|
||||
m_fd(fd),
|
||||
m_capacity(capacity),
|
||||
m_size(size),
|
||||
m_data(osmium::detail::typed_mmap<T>::grow_and_map(capacity, m_fd)) {
|
||||
}
|
||||
|
||||
void data(T* data) {
|
||||
m_data = data;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
typedef T value_type;
|
||||
typedef T& reference;
|
||||
typedef const T& const_reference;
|
||||
typedef T* pointer;
|
||||
typedef const T* const_pointer;
|
||||
typedef T* iterator;
|
||||
typedef const T* const_iterator;
|
||||
|
||||
~mmap_vector_base() {
|
||||
osmium::detail::typed_mmap<T>::unmap(m_data, m_capacity);
|
||||
}
|
||||
|
||||
size_t capacity() const {
|
||||
return m_capacity;
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return m_size;
|
||||
}
|
||||
|
||||
bool empty() const {
|
||||
return m_size == 0;
|
||||
}
|
||||
|
||||
const T* data() const {
|
||||
return m_data;
|
||||
}
|
||||
|
||||
T* data() {
|
||||
return m_data;
|
||||
}
|
||||
|
||||
T& operator[](size_t n) {
|
||||
return m_data[n];
|
||||
}
|
||||
|
||||
T at(size_t n) const {
|
||||
if (n >= m_size) {
|
||||
throw std::out_of_range("out of range");
|
||||
}
|
||||
return m_data[n];
|
||||
}
|
||||
|
||||
void clear() {
|
||||
m_size = 0;
|
||||
}
|
||||
|
||||
void shrink_to_fit() {
|
||||
// XXX do something here
|
||||
}
|
||||
|
||||
void push_back(const T& value) {
|
||||
if (m_size >= m_capacity) {
|
||||
resize(m_size+1);
|
||||
}
|
||||
m_data[m_size] = value;
|
||||
++m_size;
|
||||
}
|
||||
|
||||
void resize(size_t new_size) {
|
||||
if (new_size > this->capacity()) {
|
||||
static_cast<TDerived<T>*>(this)->reserve(new_size + osmium::detail::mmap_vector_size_increment);
|
||||
}
|
||||
if (new_size > this->size()) {
|
||||
new (this->data() + this->size()) T[new_size - this->size()];
|
||||
}
|
||||
this->m_size = new_size;
|
||||
}
|
||||
|
||||
iterator begin() {
|
||||
return m_data;
|
||||
}
|
||||
|
||||
iterator end() {
|
||||
return m_data + m_size;
|
||||
}
|
||||
|
||||
const_iterator begin() const {
|
||||
return m_data;
|
||||
}
|
||||
|
||||
const_iterator end() const {
|
||||
return m_data + m_size;
|
||||
}
|
||||
|
||||
const_iterator cbegin() {
|
||||
return m_data;
|
||||
}
|
||||
|
||||
const_iterator cend() {
|
||||
return m_data + m_size;
|
||||
}
|
||||
|
||||
}; // class mmap_vector_base
|
||||
|
||||
} // namespace detail
|
||||
|
||||
} // namespace osmium
|
||||
|
||||
#endif // OSMIUM_DETAIL_MMAP_VECTOR_BASE_HPP
|
||||
@@ -1,83 +0,0 @@
|
||||
#ifndef OSMIUM_DETAIL_MMAP_VECTOR_FILE_HPP
|
||||
#define OSMIUM_DETAIL_MMAP_VECTOR_FILE_HPP
|
||||
|
||||
/*
|
||||
|
||||
This file is part of Osmium (http://osmcode.org/libosmium).
|
||||
|
||||
Copyright 2013,2014 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 <cstddef>
|
||||
|
||||
#include <osmium/index/detail/typed_mmap.hpp>
|
||||
#include <osmium/index/detail/mmap_vector_base.hpp>
|
||||
#include <osmium/index/detail/tmpfile.hpp>
|
||||
|
||||
namespace osmium {
|
||||
|
||||
namespace detail {
|
||||
|
||||
/**
|
||||
* This class looks and behaves like STL vector, but mmap's a file internally.
|
||||
*/
|
||||
template <typename T>
|
||||
class mmap_vector_file : public mmap_vector_base<T, mmap_vector_file> {
|
||||
|
||||
public:
|
||||
|
||||
explicit mmap_vector_file() :
|
||||
mmap_vector_base<T, osmium::detail::mmap_vector_file>(
|
||||
osmium::detail::create_tmp_file(),
|
||||
osmium::detail::mmap_vector_size_increment,
|
||||
0) {
|
||||
}
|
||||
|
||||
explicit mmap_vector_file(int fd) :
|
||||
mmap_vector_base<T, osmium::detail::mmap_vector_file>(
|
||||
fd,
|
||||
osmium::detail::typed_mmap<T>::file_size(fd) == 0 ? osmium::detail::mmap_vector_size_increment : osmium::detail::typed_mmap<T>::file_size(fd),
|
||||
osmium::detail::typed_mmap<T>::file_size(fd)) {
|
||||
}
|
||||
|
||||
void reserve(size_t new_capacity) {
|
||||
if (new_capacity > this->capacity()) {
|
||||
osmium::detail::typed_mmap<T>::unmap(this->data(), this->capacity());
|
||||
osmium::detail::typed_mmap<T>::grow_file(new_capacity, this->m_fd);
|
||||
osmium::detail::typed_mmap<T>::map(new_capacity, this->m_fd);
|
||||
this->m_capacity = new_capacity;
|
||||
}
|
||||
}
|
||||
|
||||
}; // class mmap_vector_file
|
||||
|
||||
} // namespace detail
|
||||
|
||||
} // namespace osmium
|
||||
|
||||
#endif // OSMIUM_DETAIL_MMAP_VECTOR_FILE_HPP
|
||||
-62
@@ -1,62 +0,0 @@
|
||||
#ifndef OSMIUM_DETAIL_TMPFILE_HPP
|
||||
#define OSMIUM_DETAIL_TMPFILE_HPP
|
||||
|
||||
/*
|
||||
|
||||
This file is part of Osmium (http://osmcode.org/libosmium).
|
||||
|
||||
Copyright 2013,2014 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 <cerrno>
|
||||
#include <cstdio>
|
||||
#include <system_error>
|
||||
|
||||
namespace osmium {
|
||||
|
||||
namespace detail {
|
||||
|
||||
/**
|
||||
* Create and open a temporary file. It is removed after opening.
|
||||
*
|
||||
* @return File descriptor of temporary file.
|
||||
* @exception std::system_error if something went wrong.
|
||||
*/
|
||||
inline int create_tmp_file() {
|
||||
FILE* file = ::tmpfile();
|
||||
if (!file) {
|
||||
throw std::system_error(errno, std::system_category(), "tempfile failed");
|
||||
}
|
||||
return fileno(file);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
} // namespace osmium
|
||||
|
||||
#endif // OSMIUM_DETAIL_TMPFILE
|
||||
-217
@@ -1,217 +0,0 @@
|
||||
#ifndef OSMIUM_DETAIL_TYPED_MMAP_HPP
|
||||
#define OSMIUM_DETAIL_TYPED_MMAP_HPP
|
||||
|
||||
/*
|
||||
|
||||
This file is part of Osmium (http://osmcode.org/libosmium).
|
||||
|
||||
Copyright 2013,2014 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 <cerrno>
|
||||
#include <cstddef>
|
||||
#include <stdexcept>
|
||||
#include <system_error>
|
||||
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// for bsd systems
|
||||
#ifndef MAP_ANONYMOUS
|
||||
# define MAP_ANONYMOUS MAP_ANON
|
||||
#endif
|
||||
|
||||
namespace osmium {
|
||||
|
||||
/**
|
||||
* @brief Namespace for Osmium internal use
|
||||
*/
|
||||
namespace detail {
|
||||
|
||||
/**
|
||||
* This is a helper class for working with memory mapped files and
|
||||
* anonymous shared memory. It wraps the necessary system calls
|
||||
* adding:
|
||||
* - error checking: all functions throw exceptions where needed
|
||||
* - internal casts and size calculations allow use with user defined
|
||||
* type T instead of void*
|
||||
*
|
||||
* This class only contains static functions. It should never be
|
||||
* instantiated.
|
||||
*
|
||||
* @tparam T Type of objects we want to store.
|
||||
*/
|
||||
template <typename T>
|
||||
class typed_mmap {
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Create anonymous private memory mapping with enough space for size
|
||||
* objects of type T.
|
||||
*
|
||||
* Note that no constructor is called for any of the objects in this memory!
|
||||
*
|
||||
* @param size Number of objects of type T that should fit into this memory
|
||||
* @return Pointer to mapped memory
|
||||
* @exception std::system_error If mmap(2) failed
|
||||
*/
|
||||
static T* map(size_t size) {
|
||||
void* addr = ::mmap(nullptr, sizeof(T) * size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wold-style-cast"
|
||||
if (addr == MAP_FAILED) {
|
||||
throw std::system_error(errno, std::system_category(), "mmap failed");
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
return reinterpret_cast<T*>(addr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create shared memory mapping of a file with enough space for size
|
||||
* objects of type T. The file must already have at least the
|
||||
* required size.
|
||||
*
|
||||
* Note that no constructor is called for any of the objects in this memory!
|
||||
*
|
||||
* @param size Number of objects of type T that should fit into this memory
|
||||
* @param fd File descriptor
|
||||
* @param write True if data should be writable
|
||||
* @return Pointer to mapped memory
|
||||
* @exception std::system_error If mmap(2) failed
|
||||
*/
|
||||
static T* map(size_t size, int fd, bool write = false) {
|
||||
int prot = PROT_READ;
|
||||
if (write) {
|
||||
prot |= PROT_WRITE;
|
||||
}
|
||||
void* addr = ::mmap(nullptr, sizeof(T) * size, prot, MAP_SHARED, fd, 0);
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wold-style-cast"
|
||||
if (addr == MAP_FAILED) {
|
||||
throw std::system_error(errno, std::system_category(), "mmap failed");
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
return reinterpret_cast<T*>(addr);
|
||||
}
|
||||
|
||||
// mremap(2) is only available on linux systems
|
||||
#ifdef __linux__
|
||||
/**
|
||||
* Grow memory mapping created with map().
|
||||
*
|
||||
* Note that no constructor is called for any of the objects in this memory!
|
||||
*
|
||||
* @param data Pointer to current mapping (as returned by typed_mmap())
|
||||
* @param old_size Number of objects currently stored in this memory
|
||||
* @param new_size Number of objects we want to have space for
|
||||
* @exception std::system_error If mremap(2) call failed
|
||||
*/
|
||||
static T* remap(T* data, size_t old_size, size_t new_size) {
|
||||
void* addr = ::mremap(reinterpret_cast<void*>(data), sizeof(T) * old_size, sizeof(T) * new_size, MREMAP_MAYMOVE);
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wold-style-cast"
|
||||
if (addr == MAP_FAILED) {
|
||||
throw std::system_error(errno, std::system_category(), "mremap failed");
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
return reinterpret_cast<T*>(addr);
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Release memory from map() call.
|
||||
*
|
||||
* Note that no destructor is called for the objects in this memory!
|
||||
*
|
||||
* @param data Pointer to the data
|
||||
* @param size Number of objects of type T stored
|
||||
* @exception std::system_error If munmap(2) call failed
|
||||
*/
|
||||
static void unmap(T* data, size_t size) {
|
||||
if (::munmap(reinterpret_cast<void*>(data), sizeof(T) * size) != 0) {
|
||||
throw std::system_error(errno, std::system_category(), "munmap failed");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get number of objects of type T that would fit into a file.
|
||||
*
|
||||
* @param fd File descriptor
|
||||
* @return Number of objects of type T in this file
|
||||
* @exception std::system_error If fstat(2) call failed
|
||||
* @exception std::length_error If size of the file isn't a multiple of sizeof(T)
|
||||
*/
|
||||
static size_t file_size(int fd) {
|
||||
struct stat s;
|
||||
if (fstat(fd, &s) < 0) {
|
||||
throw std::system_error(errno, std::system_category(), "fstat failed");
|
||||
}
|
||||
if (s.st_size % sizeof(T) != 0) {
|
||||
throw std::length_error("file size has to be multiple of object size");
|
||||
}
|
||||
return s.st_size / sizeof(T);
|
||||
}
|
||||
|
||||
/**
|
||||
* Grow file so there is enough space for at least new_size objects
|
||||
* of type T. If the file is large enough already, nothing is done.
|
||||
* The file is never shrunk.
|
||||
*
|
||||
* @param new_size Number of objects of type T that should fit into this file
|
||||
* @param fd File descriptor
|
||||
* @exception std::system_error If ftruncate(2) call failed
|
||||
*/
|
||||
static void grow_file(size_t new_size, int fd) {
|
||||
if (file_size(fd) < new_size) {
|
||||
if (::ftruncate(fd, sizeof(T) * new_size) < 0) {
|
||||
throw std::system_error(errno, std::system_category(), "ftruncate failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Grow file to given size (if it is smaller) and mmap it.
|
||||
*
|
||||
* @param size Number of objects of type T that should fit into this file
|
||||
* @param fd File descriptor
|
||||
* @exception Errors thrown by grow_file() or map()
|
||||
*/
|
||||
static T* grow_and_map(size_t size, int fd) {
|
||||
grow_file(size, fd);
|
||||
return map(size, fd, true);
|
||||
}
|
||||
|
||||
}; // class typed_mmap
|
||||
|
||||
} // namespace detail
|
||||
|
||||
} // namespace osmium
|
||||
|
||||
#endif // OSMIUM_DETAIL_TYPED_MMAP_HPP
|
||||
Reference in New Issue
Block a user