upgrade libosmium dependency

This commit is contained in:
Dennis Luxen
2015-03-04 12:50:42 +01:00
32 changed files with 628 additions and 150 deletions
+13 -16
View File
@@ -34,13 +34,13 @@
#define MAP_ANON MAP_ANONYMOUS
#define MAP_FAILED ((void *) -1)
#ifdef __USE_FILE_OFFSET64
# define DWORD_HI(x) (x >> 32)
# define DWORD_LO(x) ((x) & 0xffffffff)
#else
# define DWORD_HI(x) (0)
# define DWORD_LO(x) (x)
#endif
static DWORD dword_hi(uint64_t x) {
return static_cast<DWORD>(x >> 32);
}
static DWORD dword_lo(uint64_t x) {
return static_cast<DWORD>(x & 0xffffffff);
}
static void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
{
@@ -66,13 +66,14 @@ static void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t
} else
flProtect = PAGE_READONLY;
off_t end = length + offset;
HANDLE mmap_fd, h;
uint64_t end = static_cast<uint64_t>(length) + offset;
HANDLE mmap_fd;
if (fd == -1)
mmap_fd = INVALID_HANDLE_VALUE;
else
mmap_fd = (HANDLE)_get_osfhandle(fd);
h = CreateFileMapping(mmap_fd, NULL, flProtect, DWORD_HI(end), DWORD_LO(end), NULL);
HANDLE h = CreateFileMapping(mmap_fd, NULL, flProtect, dword_hi(end), dword_lo(end), NULL);
if (h == NULL)
return MAP_FAILED;
@@ -85,7 +86,7 @@ static void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t
dwDesiredAccess |= FILE_MAP_EXECUTE;
if (flags & MAP_PRIVATE)
dwDesiredAccess |= FILE_MAP_COPY;
void *ret = MapViewOfFile(h, dwDesiredAccess, DWORD_HI(offset), DWORD_LO(offset), length);
void *ret = MapViewOfFile(h, dwDesiredAccess, dword_hi(offset), dword_lo(offset), length);
if (ret == NULL) {
CloseHandle(h);
ret = MAP_FAILED;
@@ -95,12 +96,8 @@ static void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t
static int munmap(void *addr, size_t length)
{
UnmapViewOfFile(addr);
return 0;
return UnmapViewOfFile(addr) ? 0 : -1;
/* ruh-ro, we leaked handle from CreateFileMapping() ... */
}
#undef DWORD_HI
#undef DWORD_LO
#endif
@@ -33,6 +33,8 @@ DEALINGS IN THE SOFTWARE.
*/
#ifdef OSMIUM_WITH_SPARSEHASH
#include <cstddef>
#include <utility>
#include <vector>
@@ -139,4 +141,6 @@ namespace osmium {
} // namespace osmium
#endif // OSMIUM_WITH_SPARSEHASH
#endif // OSMIUM_INDEX_BYID_SPARSE_MEM_TABLE_HPP
+3 -3
View File
@@ -75,7 +75,7 @@ namespace osmium {
public:
static constexpr size_t input_buffer_size = 256 * 1024;
static constexpr unsigned int input_buffer_size = 1024 * 1024;
Decompressor() = default;
@@ -245,11 +245,11 @@ namespace osmium {
}
} else {
buffer.resize(osmium::io::Decompressor::input_buffer_size);
ssize_t nread = ::read(m_fd, const_cast<char*>(buffer.data()), buffer.size());
auto nread = ::read(m_fd, const_cast<char*>(buffer.data()), osmium::io::Decompressor::input_buffer_size);
if (nread < 0) {
throw std::system_error(errno, std::system_category(), "Read failed");
}
buffer.resize(static_cast<size_t>(nread));
buffer.resize(nread);
}
return buffer;
@@ -214,7 +214,12 @@ namespace osmium {
if (!m_done || !m_queue.empty()) {
std::future<osmium::memory::Buffer> buffer_future;
m_queue.wait_and_pop(buffer_future);
return buffer_future.get();
try {
return buffer_future.get();
} catch (...) {
m_done = true;
throw;
}
}
return osmium::memory::Buffer();
@@ -43,7 +43,6 @@ DEALINGS IN THE SOFTWARE.
# include <unistd.h>
#else
# include <io.h>
typedef int ssize_t;
#endif
#include <osmium/io/overwrite.hpp>
@@ -123,9 +122,14 @@ namespace osmium {
* @throws std::system_error On error.
*/
inline void reliable_write(const int fd, const unsigned char* output_buffer, const size_t size) {
constexpr size_t max_write = 100 * 1024 * 1024; // Max 100 MByte per write
size_t offset = 0;
do {
ssize_t length = ::write(fd, output_buffer + offset, size - offset);
auto write_count = size - offset;
if (write_count > max_write) {
write_count = max_write;
}
auto length = ::write(fd, output_buffer + offset, static_cast<unsigned int>(write_count));
if (length < 0) {
throw std::system_error(errno, std::system_category(), "Write failed");
}
+27 -11
View File
@@ -39,6 +39,8 @@ DEALINGS IN THE SOFTWARE.
#include <zlib.h>
#include <osmium/util/cast.hpp>
namespace osmium {
namespace io {
@@ -48,19 +50,26 @@ namespace osmium {
/**
* Compress data using zlib.
*
* Note that this function can not compress data larger than
* what fits in an unsigned long, on Windows this is usually 32bit.
*
* @param input Data to compress.
* @returns Compressed data.
*/
inline std::string zlib_compress(const std::string& input) {
unsigned long output_size = ::compressBound(input.size());
unsigned long output_size = ::compressBound(osmium::static_cast_with_assert<unsigned long>(input.size()));
std::string output(output_size, '\0');
if (::compress(reinterpret_cast<unsigned char*>(const_cast<char *>(output.data())),
&output_size,
reinterpret_cast<const unsigned char*>(input.data()),
input.size()) != Z_OK) {
throw std::runtime_error("failed to compress data");
auto result = ::compress(
reinterpret_cast<unsigned char*>(const_cast<char *>(output.data())),
&output_size,
reinterpret_cast<const unsigned char*>(input.data()),
osmium::static_cast_with_assert<unsigned long>(input.size())
);
if (result != Z_OK) {
throw std::runtime_error(std::string("failed to compress data: ") + zError(result));
}
output.resize(output_size);
@@ -71,6 +80,9 @@ namespace osmium {
/**
* Uncompress data using zlib.
*
* Note that this function can not uncompress data larger than
* what fits in an unsigned long, on Windows this is usually 32bit.
*
* @param input Compressed input data.
* @param raw_size Size of uncompressed data.
* @returns Uncompressed data.
@@ -78,11 +90,15 @@ namespace osmium {
inline std::unique_ptr<std::string> zlib_uncompress(const std::string& input, unsigned long raw_size) {
auto output = std::unique_ptr<std::string>(new std::string(raw_size, '\0'));
if (::uncompress(reinterpret_cast<unsigned char*>(const_cast<char *>(output->data())),
&raw_size,
reinterpret_cast<const unsigned char*>(input.data()),
input.size()) != Z_OK) {
throw std::runtime_error("failed to uncompress data");
auto result = ::uncompress(
reinterpret_cast<unsigned char*>(const_cast<char *>(output->data())),
&raw_size,
reinterpret_cast<const unsigned char*>(input.data()),
osmium::static_cast_with_assert<unsigned long>(input.size())
);
if (result != Z_OK) {
throw std::runtime_error(std::string("failed to uncompress data: ") + zError(result));
}
return output;
+3 -1
View File
@@ -43,11 +43,13 @@ DEALINGS IN THE SOFTWARE.
#include <thread>
#include <utility>
#include <osmium/util/compatibility.hpp>
namespace osmium {
namespace thread {
constexpr std::chrono::milliseconds full_queue_sleep_duration { 10 }; // XXX
OSMIUM_CONSTEXPR std::chrono::milliseconds full_queue_sleep_duration { 10 }; // XXX
/**
* A thread-safe queue.
+39 -9
View File
@@ -33,36 +33,66 @@ DEALINGS IN THE SOFTWARE.
*/
#include <cassert>
#ifndef assert
# include <cassert>
#endif
#include <limits>
#include <type_traits>
namespace osmium {
template <typename T, typename F, typename std::enable_if<std::is_integral<T>::value && std::is_integral<F>::value && std::is_signed<T>::value && std::is_signed<F>::value, int>::type = 0>
// These functions are wrappers around static_cast<>() that call assert()
// to check that there is no integer overflow happening before doing the
// cast. There are several versions of this templated function here
// depending on the types of the input and output. In any case, both input
// and output have to be integral types. If the cast can't overflow, no
// check is done.
template <typename A, typename B>
struct are_real_integers :
std::integral_constant<bool,
std::is_integral<A>::value &&
std::is_integral<B>::value &&
!std::is_same<A, bool>::value &&
!std::is_same<B, bool>::value> {
};
template <typename T, typename F, typename std::enable_if<are_real_integers<T, F>::value && std::is_same<T, F>::value, int>::type = 0>
inline T static_cast_with_assert(const F value) {
return value;
}
template <typename T, typename F, typename std::enable_if<are_real_integers<T, F>::value && !std::is_same<T, F>::value && (sizeof(T) > sizeof(F)), int>::type = 0>
inline T static_cast_with_assert(const F value) {
return static_cast<T>(value);
}
template <typename T, typename F, typename std::enable_if<are_real_integers<T, F>::value && !std::is_same<T, F>::value && std::is_signed<T>::value == std::is_signed<F>::value && (sizeof(T) == sizeof(F)), int>::type = 0>
inline T static_cast_with_assert(const F value) {
return static_cast<T>(value);
}
template <typename T, typename F, typename std::enable_if<are_real_integers<T, F>::value && !std::is_same<T, F>::value && (sizeof(T) < sizeof(F)) && std::is_signed<T>::value && std::is_signed<F>::value, int>::type = 0>
inline T static_cast_with_assert(const F value) {
static_assert(sizeof(T) < sizeof(F), "unnecessary static_cast_with_assert when casting into type of equal or larger size");
assert(value >= std::numeric_limits<T>::min() && value <= std::numeric_limits<T>::max());
return static_cast<T>(value);
}
template <typename T, typename F, typename std::enable_if<std::is_integral<T>::value && std::is_integral<F>::value && std::is_unsigned<T>::value && std::is_signed<F>::value, int>::type = 0>
template <typename T, typename F, typename std::enable_if<are_real_integers<T, F>::value && !std::is_same<T, F>::value && (sizeof(T) <= sizeof(F)) && std::is_unsigned<T>::value && std::is_signed<F>::value, int>::type = 0>
inline T static_cast_with_assert(const F value) {
static_assert(sizeof(T) <= sizeof(F), "unnecessary static_cast_with_assert when casting into type of larger size");
assert(value >= 0 && static_cast<typename std::make_unsigned<F>::type>(value) <= std::numeric_limits<T>::max());
return static_cast<T>(value);
}
template <typename T, typename F, typename std::enable_if<std::is_integral<T>::value && std::is_integral<F>::value && std::is_unsigned<T>::value && std::is_unsigned<F>::value, int>::type = 0>
template <typename T, typename F, typename std::enable_if<are_real_integers<T, F>::value && !std::is_same<T, F>::value && (sizeof(T) < sizeof(F)) && std::is_unsigned<T>::value && std::is_unsigned<F>::value, int>::type = 0>
inline T static_cast_with_assert(const F value) {
static_assert(sizeof(T) < sizeof(F), "unnecessary static_cast_with_assert when casting into type of equal or larger size");
assert(value <= std::numeric_limits<T>::max());
return static_cast<T>(value);
}
template <typename T, typename F, typename std::enable_if<std::is_integral<T>::value && std::is_integral<F>::value && std::is_signed<T>::value && std::is_unsigned<F>::value, int>::type = 0>
template <typename T, typename F, typename std::enable_if<are_real_integers<T, F>::value && !std::is_same<T, F>::value && (sizeof(T) <= sizeof(F)) && std::is_signed<T>::value && std::is_unsigned<F>::value, int>::type = 0>
inline T static_cast_with_assert(const F value) {
static_assert(sizeof(T) <= sizeof(F), "unnecessary static_cast_with_assert when casting into type of larger size");
assert(value <= std::numeric_limits<T>::max());
return static_cast<T>(value);
}