From 0102716ca49394c75edeb02be3e07d769ac3b4a4 Mon Sep 17 00:00:00 2001 From: Daniel Scharrer Date: Thu, 24 Oct 2013 00:47:43 +0200 Subject: [PATCH] Move alignment functions out of types.hpp --- CMakeLists.txt | 1 + src/crypto/iteratedhash.hpp | 1 + src/util/align.hpp | 64 +++++++++++++++++++++++++++++++++++++ src/util/load.cpp | 1 + src/util/types.hpp | 32 ------------------- 5 files changed, 67 insertions(+), 32 deletions(-) create mode 100644 src/util/align.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 992e2ac..1a99d33 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -306,6 +306,7 @@ set(INNOEXTRACT_SOURCES src/stream/slice.hpp src/stream/slice.cpp + src/util/align.hpp src/util/boostfs_compat.hpp src/util/console.hpp src/util/console.cpp diff --git a/src/crypto/iteratedhash.hpp b/src/crypto/iteratedhash.hpp index 8101a0a..43c5c38 100644 --- a/src/crypto/iteratedhash.hpp +++ b/src/crypto/iteratedhash.hpp @@ -29,6 +29,7 @@ #include #include "crypto/checksum.hpp" +#include "util/align.hpp" #include "util/endian.hpp" #include "util/math.hpp" #include "util/types.hpp" diff --git a/src/util/align.hpp b/src/util/align.hpp new file mode 100644 index 0000000..cee4449 --- /dev/null +++ b/src/util/align.hpp @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2011-2013 Daniel Scharrer + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the author(s) be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + */ + +/*! + * Utility functions for dealing with alignment of objects. + */ +#ifndef INNOEXTRACT_UTIL_ALIGN_HPP +#define INNOEXTRACT_UTIL_ALIGN_HPP + +#include + +#include "util/math.hpp" + +#include "configure.hpp" + +namespace util { + +//! Get the alignment of a type. +template +unsigned int alignment_of() { +#if INNOEXTRACT_HAVE_ALIGNOF + return alignof(T); +#elif defined(_MSC_VER) && _MSC_VER >= 1300 + return __alignof(T); +#elif defined(__GNUC__) + return __alignof__(T); +#else + return sizeof(T); +#endif +} + +//! Check if a pointer has aparticular alignment. +inline bool is_aligned_on(const void * p, size_t alignment) { + return alignment == 1 + || (is_power_of_2(alignment) ? mod_power_of_2(size_t(p), alignment) == 0 + : size_t(p) % alignment == 0); +} + +//! Check if a pointer is aligned for a specific type. +template +bool is_aligned(const void * p) { + return is_aligned_on(p, alignment_of()); +} + +} // namespace util + +#endif // INNOEXTRACT_UTIL_ALIGN_HPP diff --git a/src/util/load.cpp b/src/util/load.cpp index 83093cd..cde733f 100644 --- a/src/util/load.cpp +++ b/src/util/load.cpp @@ -32,6 +32,7 @@ #include #include "util/log.hpp" +#include "util/math.hpp" namespace util { diff --git a/src/util/types.hpp b/src/util/types.hpp index 14dd228..159c93d 100644 --- a/src/util/types.hpp +++ b/src/util/types.hpp @@ -30,10 +30,6 @@ #include #include -#include "util/math.hpp" - -#include "configure.hpp" - namespace util { #if BOOST_VERSION < 104200 @@ -89,34 +85,6 @@ struct compatible_integer { >::exact type; }; - -//! Get the alignment of a type. -template -unsigned int alignment_of() { -#if INNOEXTRACT_HAVE_ALIGNOF - return alignof(T); -#elif defined(_MSC_VER) && _MSC_VER >= 1300 - return __alignof(T); -#elif defined(__GNUC__) - return __alignof__(T); -#else - return sizeof(T); -#endif -} - -//! Check if a pointer has aparticular alignment. -inline bool is_aligned_on(const void * p, size_t alignment) { - return alignment == 1 - || (is_power_of_2(alignment) ? mod_power_of_2(size_t(p), alignment) == 0 - : size_t(p) % alignment == 0); -} - -//! Check if a pointer is aligned for a specific type. -template -bool is_aligned(const void * p) { - return is_aligned_on(p, alignment_of()); -} - //! CRTP helper class to hide the ugly static casts needed to get to the derived class. template class static_polymorphic {