Browse Source

Move alignment functions out of types.hpp

coverity_scan
Daniel Scharrer 13 years ago
parent
commit
0102716ca4
  1. 1
      CMakeLists.txt
  2. 1
      src/crypto/iteratedhash.hpp
  3. 64
      src/util/align.hpp
  4. 1
      src/util/load.cpp
  5. 32
      src/util/types.hpp

1
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

1
src/crypto/iteratedhash.hpp

@ -29,6 +29,7 @@
#include <boost/range/size.hpp>
#include "crypto/checksum.hpp"
#include "util/align.hpp"
#include "util/endian.hpp"
#include "util/math.hpp"
#include "util/types.hpp"

64
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 <limits>
#include "util/math.hpp"
#include "configure.hpp"
namespace util {
//! Get the alignment of a type.
template <class T>
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 <class T>
bool is_aligned(const void * p) {
return is_aligned_on(p, alignment_of<T>());
}
} // namespace util
#endif // INNOEXTRACT_UTIL_ALIGN_HPP

1
src/util/load.cpp

@ -32,6 +32,7 @@
#include <boost/unordered_map.hpp>
#include "util/log.hpp"
#include "util/math.hpp"
namespace util {

32
src/util/types.hpp

@ -30,10 +30,6 @@
#include <boost/integer/static_min_max.hpp>
#include <boost/integer.hpp>
#include "util/math.hpp"
#include "configure.hpp"
namespace util {
#if BOOST_VERSION < 104200
@ -89,34 +85,6 @@ struct compatible_integer<Base, Bits, true> {
>::exact type;
};
//! Get the alignment of a type.
template <class T>
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 <class T>
bool is_aligned(const void * p) {
return is_aligned_on(p, alignment_of<T>());
}
//! CRTP helper class to hide the ugly static casts needed to get to the derived class.
template <class Impl>
class static_polymorphic {

Loading…
Cancel
Save