diff --git a/CMakeLists.txt b/CMakeLists.txt index 14f00956..bbcf8d89 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,13 +81,13 @@ if(NOT WIN32 AND NOT BUILD_FRAMEWORK) endif() include_directories(${CMAKE_CURRENT_BINARY_DIR}) -configure_file(config-taglib.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h) +configure_file(config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h) if(WITH_ASF) - set(TAGLIB_WITH_ASF TRUE) + set(TAGLIB_WITH_ASF TRUE) endif() if(WITH_MP4) - set(TAGLIB_WITH_MP4 TRUE) + set(TAGLIB_WITH_MP4 TRUE) endif() configure_file(taglib/taglib_config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/taglib_config.h) diff --git a/ConfigureChecks.cmake b/ConfigureChecks.cmake index 67b99d88..440a4414 100644 --- a/ConfigureChecks.cmake +++ b/ConfigureChecks.cmake @@ -7,30 +7,37 @@ include(CheckTypeSize) include(CheckCXXSourceCompiles) include(TestBigEndian) -# Determine the CPU byte order. +# Check if the size of integral types are suitable. -test_big_endian(TAGLIB_BIG_ENDIAN) - -if(NOT TAGLIB_BIG_ENDIAN) - set(TAGLIB_LITTLE_ENDIAN 1) +check_type_size("short" SIZEOF_SHORT) +if(NOT ${SIZEOF_SHORT} EQUAL 2) + MESSAGE(FATAL_ERROR "TagLib requires that short is 16-bit wide.") endif() -# Determine the size of integral types. +check_type_size("int" SIZEOF_INT) +if(NOT ${SIZEOF_INT} EQUAL 4) + MESSAGE(FATAL_ERROR "TagLib requires that int is 32-bit wide.") +endif() -check_type_size("short" SIZEOF_SHORT) -check_type_size("int" SIZEOF_INT) check_type_size("long long" SIZEOF_LONGLONG) -check_type_size("wchar_t" SIZEOF_WCHAR_T) +if(NOT ${SIZEOF_LONGLONG} EQUAL 8) + MESSAGE(FATAL_ERROR "TagLib requires that long long is 64-bit wide.") +endif() -# Determine if your compiler supports std::wstring. +check_type_size("wchar_t" SIZEOF_WCHAR_T) +if(${SIZEOF_WCHAR_T} LESS 2) + MESSAGE(FATAL_ERROR "TagLib requires that wchar_t is sufficient to store a UTF-16 char.") +endif() -check_cxx_source_compiles(" - #include - int main() { - std::wstring x(L\"ABC\"); - return 0; - } -" HAVE_STD_WSTRING) +# Determine the CPU byte order. + +test_big_endian(IS_BIG_ENDIAN) + +if(NOT IS_BIG_ENDIAN) + set(SYSTEM_BYTEORDER 1) +else() + set(SYSTEM_BYTEORDER 2) +endif() # Determine which kind of atomic operations your compiler supports. diff --git a/bindings/c/tag_c.cpp b/bindings/c/tag_c.cpp index b1af613c..5b739d6f 100644 --- a/bindings/c/tag_c.cpp +++ b/bindings/c/tag_c.cpp @@ -19,10 +19,7 @@ * USA * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - +#include "taglib_config.h" #include #include #include diff --git a/config-taglib.h.cmake b/config.h.cmake similarity index 56% rename from config-taglib.h.cmake rename to config.h.cmake index aee25a79..436ca440 100644 --- a/config-taglib.h.cmake +++ b/config.h.cmake @@ -1,25 +1,8 @@ -/* config-taglib.h. Generated by cmake from config-taglib.h.cmake */ +/* config.h. Generated by cmake from config.h.cmake */ -/* Indicates the endianness of your target system */ -#cmakedefine TAGLIB_LITTLE_ENDIAN 1 -#cmakedefine TAGLIB_BIG_ENDIAN 1 - -/* Size of integral types */ -#cmakedefine SIZEOF_SHORT ${SIZEOF_SHORT} -#cmakedefine SIZEOF_INT ${SIZEOF_INT} -#cmakedefine SIZEOF_LONGLONG ${SIZEOF_LONGLONG} -#cmakedefine SIZEOF_WCHAR_T ${SIZEOF_WCHAR_T} - -/* Defined if your compiler supports std::wstring */ -#cmakedefine HAVE_STD_WSTRING 1 - -/* Defined if your compiler supports some atomic operations */ -#cmakedefine HAVE_STD_ATOMIC 1 -#cmakedefine HAVE_BOOST_ATOMIC 1 -#cmakedefine HAVE_GCC_ATOMIC 1 -#cmakedefine HAVE_MAC_ATOMIC 1 -#cmakedefine HAVE_WIN_ATOMIC 1 -#cmakedefine HAVE_IA64_ATOMIC 1 +/* Indicates the byte order of your target system */ +/* 1 if little-endian, 2 if big-endian. */ +#cmakedefine SYSTEM_BYTEORDER ${SYSTEM_BYTEORDER} /* Defined if your compiler supports some byte swap functions */ #cmakedefine HAVE_GCC_BYTESWAP_16 1 @@ -33,11 +16,15 @@ /* Defined if your compiler supports codecvt */ #cmakedefine HAVE_STD_CODECVT 1 +/* Defined if your compiler supports some atomic operations */ +#cmakedefine HAVE_STD_ATOMIC 1 +#cmakedefine HAVE_BOOST_ATOMIC 1 +#cmakedefine HAVE_GCC_ATOMIC 1 +#cmakedefine HAVE_MAC_ATOMIC 1 +#cmakedefine HAVE_WIN_ATOMIC 1 +#cmakedefine HAVE_IA64_ATOMIC 1 + /* Defined if you have libz */ #cmakedefine HAVE_ZLIB 1 -#cmakedefine NO_ITUNES_HACKS 1 -#cmakedefine WITH_ASF 1 -#cmakedefine WITH_MP4 1 - #cmakedefine TESTS_DIR "@TESTS_DIR@" diff --git a/taglib/CMakeLists.txt b/taglib/CMakeLists.txt index 72712ca2..352f7e2e 100644 --- a/taglib/CMakeLists.txt +++ b/taglib/CMakeLists.txt @@ -50,6 +50,7 @@ set(tag_HDRS toolkit/tmap.h toolkit/tmap.tcc toolkit/tpropertymap.h + toolkit/trefcounter.h mpeg/mpegfile.h mpeg/mpegproperties.h mpeg/mpegheader.h @@ -289,6 +290,7 @@ set(toolkit_SRCS toolkit/tfilestream.cpp toolkit/tdebug.cpp toolkit/tpropertymap.cpp + toolkit/trefcounter.cpp toolkit/unicode.cpp ) diff --git a/taglib/asf/asfattribute.cpp b/taglib/asf/asfattribute.cpp index 937a8816..4ee2d0a1 100644 --- a/taglib/asf/asfattribute.cpp +++ b/taglib/asf/asfattribute.cpp @@ -25,6 +25,7 @@ #include #include +#include "trefcounter.h" #include "asfattribute.h" #include "asffile.h" diff --git a/taglib/asf/asfpicture.cpp b/taglib/asf/asfpicture.cpp index 3db695a4..999f9204 100644 --- a/taglib/asf/asfpicture.cpp +++ b/taglib/asf/asfpicture.cpp @@ -25,6 +25,7 @@ #include #include +#include "trefcounter.h" #include "asfattribute.h" #include "asffile.h" #include "asfpicture.h" diff --git a/taglib/asf/asfproperties.cpp b/taglib/asf/asfproperties.cpp index 11d43b9d..638baf6e 100644 --- a/taglib/asf/asfproperties.cpp +++ b/taglib/asf/asfproperties.cpp @@ -23,8 +23,7 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#include "config.h" - +#include "taglib_config.h" #include #include #include "asfproperties.h" diff --git a/taglib/asf/asftag.cpp b/taglib/asf/asftag.cpp index 70881209..0767817f 100644 --- a/taglib/asf/asftag.cpp +++ b/taglib/asf/asftag.cpp @@ -23,8 +23,7 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#include "config.h" - +#include "taglib_config.h" #include #include "asftag.h" diff --git a/taglib/fileref.cpp b/taglib/fileref.cpp index 6da560b7..157ba2ee 100644 --- a/taglib/fileref.cpp +++ b/taglib/fileref.cpp @@ -27,7 +27,7 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#include +#include "taglib_config.h" #ifdef _WIN32 # include @@ -36,6 +36,7 @@ #include #include #include +#include "trefcounter.h" #include "fileref.h" #include "asffile.h" diff --git a/taglib/mp4/mp4coverart.cpp b/taglib/mp4/mp4coverart.cpp index 5ccc76d6..2746469d 100644 --- a/taglib/mp4/mp4coverart.cpp +++ b/taglib/mp4/mp4coverart.cpp @@ -25,6 +25,7 @@ #include #include +#include "trefcounter.h" #include "mp4coverart.h" using namespace TagLib; diff --git a/taglib/mp4/mp4item.cpp b/taglib/mp4/mp4item.cpp index fdb96451..671f26b4 100644 --- a/taglib/mp4/mp4item.cpp +++ b/taglib/mp4/mp4item.cpp @@ -25,6 +25,7 @@ #include #include +#include "trefcounter.h" #include "mp4item.h" using namespace TagLib; diff --git a/taglib/mp4/mp4properties.cpp b/taglib/mp4/mp4properties.cpp index f5d7ed21..d704b452 100644 --- a/taglib/mp4/mp4properties.cpp +++ b/taglib/mp4/mp4properties.cpp @@ -23,8 +23,7 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#include "config.h" - +#include "taglib_config.h" #include #include #include "mp4file.h" diff --git a/taglib/mp4/mp4tag.cpp b/taglib/mp4/mp4tag.cpp index e16a994b..1edeace7 100644 --- a/taglib/mp4/mp4tag.cpp +++ b/taglib/mp4/mp4tag.cpp @@ -23,8 +23,7 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#include "config.h" - +#include "taglib_config.h" #include #include #include diff --git a/taglib/mpeg/id3v2/id3v2frame.cpp b/taglib/mpeg/id3v2/id3v2frame.cpp index a444b9eb..424ac2d2 100644 --- a/taglib/mpeg/id3v2/id3v2frame.cpp +++ b/taglib/mpeg/id3v2/id3v2frame.cpp @@ -24,6 +24,7 @@ ***************************************************************************/ #include "config.h" +#include "taglib_config.h" #if HAVE_ZLIB #include diff --git a/taglib/mpeg/id3v2/id3v2framefactory.cpp b/taglib/mpeg/id3v2/id3v2framefactory.cpp index 54b37ce8..3138f90a 100644 --- a/taglib/mpeg/id3v2/id3v2framefactory.cpp +++ b/taglib/mpeg/id3v2/id3v2framefactory.cpp @@ -24,7 +24,7 @@ ***************************************************************************/ #include "config.h" - +#include "taglib_config.h" #include #include "id3v2framefactory.h" @@ -134,7 +134,7 @@ Frame *FrameFactory::createFrame(const ByteVector &origData, Header *tagHeader) // TagLib doesn't mess with encrypted frames, so just treat them // as unknown frames. -#if HAVE_ZLIB == 0 +#if !defined(HAVE_ZLIB) || HAVE_ZLIB == 0 if(header->compression()) { debug("Compressed frames are currently not supported."); return new UnknownFrame(data, header); diff --git a/taglib/mpeg/mpegheader.cpp b/taglib/mpeg/mpegheader.cpp index c715dbc1..a582f758 100644 --- a/taglib/mpeg/mpegheader.cpp +++ b/taglib/mpeg/mpegheader.cpp @@ -28,6 +28,7 @@ #include #include #include +#include "trefcounter.h" #include "mpegheader.h" diff --git a/taglib/taglib_config.h.cmake b/taglib/taglib_config.h.cmake index 0f499e2c..1560c247 100644 --- a/taglib/taglib_config.h.cmake +++ b/taglib/taglib_config.h.cmake @@ -2,3 +2,4 @@ #define TAGLIB_WITH_ASF 1 #define TAGLIB_WITH_MP4 1 + diff --git a/taglib/taglib_export.h b/taglib/taglib_export.h index 3e868551..737ae644 100644 --- a/taglib/taglib_export.h +++ b/taglib/taglib_export.h @@ -40,8 +40,4 @@ #define TAGLIB_EXPORT #endif -#ifndef TAGLIB_NO_CONFIG -#include "taglib_config.h" -#endif - #endif diff --git a/taglib/toolkit/taglib.h b/taglib/toolkit/taglib.h index bc94aa88..f93f280d 100755 --- a/taglib/toolkit/taglib.h +++ b/taglib/toolkit/taglib.h @@ -26,7 +26,7 @@ #ifndef TAGLIB_H #define TAGLIB_H -#include "config.h" +#include "taglib_config.h" #define TAGLIB_MAJOR_VERSION 1 #define TAGLIB_MINOR_VERSION 8 @@ -45,113 +45,6 @@ #endif #include -#include - -// Check the widths of integral types. - -#if SIZEOF_SHORT != 2 -# error TagLib requires that short is 16-bit wide. -#endif - -#if SIZEOF_INT != 4 -# error TagLib requires that int is 32-bit wide. -#endif - -#if SIZEOF_LONGLONG != 8 -# error TagLib requires that long long is 64-bit wide. -#endif - -#if SIZEOF_WCHAR_T < 2 -# error TagLib requires that wchar_t is sufficient to store a UTF-16 char. -#endif - -// Atomic increment/decrement operations - -#if defined(HAVE_STD_ATOMIC) -# include -# define TAGLIB_ATOMIC_INT std::atomic -# define TAGLIB_ATOMIC_INC(x) x.fetch_add(1) -# define TAGLIB_ATOMIC_DEC(x) (x.fetch_sub(1) - 1) -#elif defined(HAVE_BOOST_ATOMIC) -# include -# define TAGLIB_ATOMIC_INT boost::atomic -# define TAGLIB_ATOMIC_INC(x) x.fetch_add(1) -# define TAGLIB_ATOMIC_DEC(x) (x.fetch_sub(1) - 1) -#elif defined(HAVE_GCC_ATOMIC) -# define TAGLIB_ATOMIC_INT int -# define TAGLIB_ATOMIC_INC(x) __sync_add_and_fetch(&x, 1) -# define TAGLIB_ATOMIC_DEC(x) __sync_sub_and_fetch(&x, 1) -#elif defined(HAVE_WIN_ATOMIC) -# if !defined(NOMINMAX) -# define NOMINMAX -# endif -# include -# define TAGLIB_ATOMIC_INT long -# define TAGLIB_ATOMIC_INC(x) InterlockedIncrement(&x) -# define TAGLIB_ATOMIC_DEC(x) InterlockedDecrement(&x) -#elif defined(HAVE_MAC_ATOMIC) -# include -# define TAGLIB_ATOMIC_INT int32_t -# define TAGLIB_ATOMIC_INC(x) OSAtomicIncrement32Barrier(&x) -# define TAGLIB_ATOMIC_DEC(x) OSAtomicDecrement32Barrier(&x) -#elif defined(HAVE_IA64_ATOMIC) -# include -# define TAGLIB_ATOMIC_INT int -# define TAGLIB_ATOMIC_INC(x) __sync_add_and_fetch(&x, 1) -# define TAGLIB_ATOMIC_DEC(x) __sync_sub_and_fetch(&x, 1) -#else -# define TAGLIB_ATOMIC_INT int -# define TAGLIB_ATOMIC_INC(x) (++x) -# define TAGLIB_ATOMIC_DEC(x) (--x) -#endif - -// Optimized byte swap functions. - -#if defined(HAVE_MSC_BYTESWAP) -# include -#elif defined(HAVE_GLIBC_BYTESWAP) -# include -#elif defined(HAVE_MAC_BYTESWAP) -# include -#elif defined(HAVE_OPENBSD_BYTESWAP) -# include -#endif - -#if defined(HAVE_GCC_BYTESWAP_16) -# define TAGLIB_BYTESWAP_16(x) __builtin_bswap16(x) -#elif defined(HAVE_MSC_BYTESWAP) -# define TAGLIB_BYTESWAP_16(x) _byteswap_ushort(x) -#elif defined(HAVE_GLIBC_BYTESWAP) -# define TAGLIB_BYTESWAP_16(x) __bswap_16(x) -#elif defined(HAVE_MAC_BYTESWAP) -# define TAGLIB_BYTESWAP_16(x) OSSwapInt16(x) -#elif defined(HAVE_OPENBSD_BYTESWAP) -# define TAGLIB_BYTESWAP_16(x) swap16(x) -#endif - -#if defined(HAVE_GCC_BYTESWAP_32) -# define TAGLIB_BYTESWAP_32(x) __builtin_bswap32(x) -#elif defined(HAVE_MSC_BYTESWAP) -# define TAGLIB_BYTESWAP_32(x) _byteswap_ulong(x) -#elif defined(HAVE_GLIBC_BYTESWAP) -# define TAGLIB_BYTESWAP_32(x) __bswap_32(x) -#elif defined(HAVE_MAC_BYTESWAP) -# define TAGLIB_BYTESWAP_32(x) OSSwapInt32(x) -#elif defined(HAVE_OPENBSD_BYTESWAP) -# define TAGLIB_BYTESWAP_32(x) swap32(x) -#endif - -#if defined(HAVE_GCC_BYTESWAP_64) -# define TAGLIB_BYTESWAP_64(x) __builtin_bswap64(x) -#elif defined(HAVE_MSC_BYTESWAP) -# define TAGLIB_BYTESWAP_64(x) _byteswap_uint64(x) -#elif defined(HAVE_GLIBC_BYTESWAP) -# define TAGLIB_BYTESWAP_64(x) __bswap_64(x) -#elif defined(HAVE_MAC_BYTESWAP) -# define TAGLIB_BYTESWAP_64(x) OSSwapInt64(x) -#elif defined(HAVE_OPENBSD_BYTESWAP) -# define TAGLIB_BYTESWAP_64(x) swap64(x) -#endif //! A namespace for all TagLib related classes and functions @@ -180,35 +73,7 @@ namespace TagLib { * Unfortunately std::wstring isn't defined on some systems, (i.e. GCC < 3) * so I'm providing something here that should be constant. */ -#ifdef HAVE_STD_WSTRING - typedef std::wstring wstring; -#else typedef std::basic_string wstring; -#endif - -#ifndef DO_NOT_DOCUMENT // Tell Doxygen to skip this class. - /*! - * \internal - * This is just used as a base class for shared classes in TagLib. - * - * \warning This is not part of the TagLib public API! - */ - - class RefCounter - { - public: - RefCounter() : refCount(1) {} - - void ref() { TAGLIB_ATOMIC_INC(refCount); } - bool deref() { return (TAGLIB_ATOMIC_DEC(refCount) == 0); } - int count() { return refCount; } - - private: - volatile TAGLIB_ATOMIC_INT refCount; - }; - -#endif // DO_NOT_DOCUMENT - } /*! diff --git a/taglib/toolkit/tbytevector.cpp b/taglib/toolkit/tbytevector.cpp index 06f38b6e..471af477 100644 --- a/taglib/toolkit/tbytevector.cpp +++ b/taglib/toolkit/tbytevector.cpp @@ -23,11 +23,25 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ +#include "config.h" + #include #include #include + +#if defined(HAVE_MSC_BYTESWAP) +# include +#elif defined(HAVE_GLIBC_BYTESWAP) +# include +#elif defined(HAVE_MAC_BYTESWAP) +# include +#elif defined(HAVE_OPENBSD_BYTESWAP) +# include +#endif + #include #include +#include "trefcounter.h" #include "tbytevector.h" @@ -185,9 +199,25 @@ T byteSwap(T x) template <> ushort byteSwap(ushort x) { -#ifdef TAGLIB_BYTESWAP_16 +#if defined(HAVE_GCC_BYTESWAP_16) - return TAGLIB_BYTESWAP_16(x); + return __builtin_bswap16(x); + +#elif defined(HAVE_MSC_BYTESWAP) + + return _byteswap_ushort(x); + +#elif defined(HAVE_GLIBC_BYTESWAP) + + return __bswap_16(x); + +#elif defined(HAVE_MAC_BYTESWAP) + + return OSSwapInt16(x); + +#elif defined(HAVE_OPENBSD_BYTESWAP) + + return swap16(x); #else @@ -199,9 +229,25 @@ ushort byteSwap(ushort x) template <> uint byteSwap(uint x) { -#ifdef TAGLIB_BYTESWAP_32 +#if defined(HAVE_GCC_BYTESWAP_32) - return TAGLIB_BYTESWAP_32(x); + return __builtin_bswap32(x); + +#elif defined(HAVE_MSC_BYTESWAP) + + return _byteswap_ulong(x); + +#elif defined(HAVE_GLIBC_BYTESWAP) + + return __bswap_32(x); + +#elif defined(HAVE_MAC_BYTESWAP) + + return OSSwapInt32(x); + +#elif defined(HAVE_OPENBSD_BYTESWAP) + + return swap32(x); #else @@ -216,9 +262,25 @@ uint byteSwap(uint x) template <> ulonglong byteSwap(ulonglong x) { -#ifdef TAGLIB_BYTESWAP_64 +#if defined(HAVE_GCC_BYTESWAP_64) - return TAGLIB_BYTESWAP_64(x); + return __builtin_bswap64(x); + +#elif defined(HAVE_MSC_BYTESWAP) + + return _byteswap_uint64(x); + +#elif defined(HAVE_GLIBC_BYTESWAP) + + return __bswap_64(x); + +#elif defined(HAVE_MAC_BYTESWAP) + + return OSSwapInt64(x); + +#elif defined(HAVE_OPENBSD_BYTESWAP) + + return swap64(x); #else @@ -246,7 +308,7 @@ T toNumber(const ByteVector &v, size_t offset, bool mostSignificantByteFirst) T tmp; ::memcpy(&tmp, v.data() + offset, sizeof(T)); -#ifdef TAGLIB_LITTLE_ENDIAN +#if SYSTEM_BYTEORDER == 1 const bool swap = mostSignificantByteFirst; #else const bool swap != mostSignificantByteFirst; @@ -279,7 +341,7 @@ ByteVector fromNumber(T value, bool mostSignificantByteFirst) { const size_t size = sizeof(T); -#ifdef TAGLIB_LITTLE_ENDIAN +#if SYSTEM_BYTEORDER == 1 const bool swap = mostSignificantByteFirst; #else const bool swap != mostSignificantByteFirst; diff --git a/taglib/toolkit/tlist.tcc b/taglib/toolkit/tlist.tcc index 37817f05..81e29153 100644 --- a/taglib/toolkit/tlist.tcc +++ b/taglib/toolkit/tlist.tcc @@ -24,6 +24,7 @@ ***************************************************************************/ #include +#include "trefcounter.h" namespace TagLib { diff --git a/taglib/toolkit/tmap.tcc b/taglib/toolkit/tmap.tcc index 0f2b9933..d6f51c07 100644 --- a/taglib/toolkit/tmap.tcc +++ b/taglib/toolkit/tmap.tcc @@ -23,6 +23,8 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ +#include "trefcounter.h" + namespace TagLib { //////////////////////////////////////////////////////////////////////////////// diff --git a/taglib/toolkit/trefcounter.cpp b/taglib/toolkit/trefcounter.cpp new file mode 100644 index 00000000..327d7245 --- /dev/null +++ b/taglib/toolkit/trefcounter.cpp @@ -0,0 +1,105 @@ +/*************************************************************************** + copyright : (C) 2013 by Tsuda Kageyu + email : tsuda.kageyu@gmail.com + ***************************************************************************/ + +/*************************************************************************** + * This library is free software; you can redistribute it and/or modify * + * it under the terms of the GNU Lesser General Public License version * + * 2.1 as published by the Free Software Foundation. * + * * + * This library is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * + * 02110-1301 USA * + * * + * Alternatively, this file is available under the Mozilla Public * + * License Version 1.1. You may obtain a copy of the License at * + * http://www.mozilla.org/MPL/ * + ***************************************************************************/ + +#include "config.h" +#include "trefcounter.h" + +#if defined(HAVE_STD_ATOMIC) +# include +# define ATOMIC_INT std::atomic +# define ATOMIC_INC(x) x.fetch_add(1) +# define ATOMIC_DEC(x) (x.fetch_sub(1) - 1) +#elif defined(HAVE_BOOST_ATOMIC) +# include +# define ATOMIC_INT boost::atomic +# define ATOMIC_INC(x) x.fetch_add(1) +# define ATOMIC_DEC(x) (x.fetch_sub(1) - 1) +#elif defined(HAVE_GCC_ATOMIC) +# define ATOMIC_INT int +# define ATOMIC_INC(x) __sync_add_and_fetch(&x, 1) +# define ATOMIC_DEC(x) __sync_sub_and_fetch(&x, 1) +#elif defined(HAVE_WIN_ATOMIC) +# if !defined(NOMINMAX) +# define NOMINMAX +# endif +# include +# define ATOMIC_INT long +# define ATOMIC_INC(x) InterlockedIncrement(&x) +# define ATOMIC_DEC(x) InterlockedDecrement(&x) +#elif defined(HAVE_MAC_ATOMIC) +# include +# define ATOMIC_INT int32_t +# define ATOMIC_INC(x) OSAtomicIncrement32Barrier(&x) +# define ATOMIC_DEC(x) OSAtomicDecrement32Barrier(&x) +#elif defined(HAVE_IA64_ATOMIC) +# include +# define ATOMIC_INT int +# define ATOMIC_INC(x) __sync_add_and_fetch(&x, 1) +# define ATOMIC_DEC(x) __sync_sub_and_fetch(&x, 1) +#else +# define ATOMIC_INT int +# define ATOMIC_INC(x) (++x) +# define ATOMIC_DEC(x) (--x) +#endif + +namespace TagLib +{ + class RefCounter::RefCounterPrivate + { + public: + RefCounterPrivate() : refCount(1) {} + + void ref() { ATOMIC_INC(refCount); } + bool deref() { return (ATOMIC_DEC(refCount) == 0); } + int count() const { return refCount; } + + volatile ATOMIC_INT refCount; + }; + + RefCounter::RefCounter() + : d(new RefCounterPrivate()) + { + } + + RefCounter::~RefCounter() + { + delete d; + } + + void RefCounter::ref() + { + d->ref(); + } + + bool RefCounter::deref() + { + return d->deref(); + } + + int RefCounter::count() const + { + return d->count(); + } +} diff --git a/taglib/toolkit/trefcounter.h b/taglib/toolkit/trefcounter.h new file mode 100644 index 00000000..2d4ea7c8 --- /dev/null +++ b/taglib/toolkit/trefcounter.h @@ -0,0 +1,57 @@ +/*************************************************************************** + copyright : (C) 2013 by Tsuda Kageyu + email : tsuda.kageyu@gmail.com + ***************************************************************************/ + +/*************************************************************************** + * This library is free software; you can redistribute it and/or modify * + * it under the terms of the GNU Lesser General Public License version * + * 2.1 as published by the Free Software Foundation. * + * * + * This library is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * + * 02110-1301 USA * + * * + * Alternatively, this file is available under the Mozilla Public * + * License Version 1.1. You may obtain a copy of the License at * + * http://www.mozilla.org/MPL/ * + ***************************************************************************/ + +#ifndef TAGLIB_REFCOUNTER_H +#define TAGLIB_REFCOUNTER_H + +#include "taglib.h" + +#ifndef DO_NOT_DOCUMENT // Tell Doxygen to skip this class. +/*! + * \internal + * This is just used as a base class for shared classes in TagLib. + * + * \warning This is not part of the TagLib public API! + */ +namespace TagLib +{ + class RefCounter + { + public: + RefCounter(); + virtual ~RefCounter(); + + void ref(); + bool deref(); + int count() const; + + private: + class RefCounterPrivate; + RefCounterPrivate *d; + }; +} + +#endif // DO_NOT_DOCUMENT +#endif diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index eaf5291e..cf745f81 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -25,17 +25,24 @@ // This class assumes that std::basic_string has a contiguous and null-terminated buffer. +#include "config.h" + #include "tstring.h" #include "tdebug.h" #include "tstringlist.h" +#include "trefcounter.h" #include #include -// x86 CPUs are alignment-tolerant or allow pointer casts from smaller types to larger types. -#if defined(__i386__) || defined(_M_IX86) || defined(__amd64) || defined(__amd64__) \ - || defined(_M_AMD64) || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) -# define TAGLIB_ALIGNMENT_TOLERANT 1 +#if defined(HAVE_MSC_BYTESWAP) +# include +#elif defined(HAVE_GLIBC_BYTESWAP) +# include +#elif defined(HAVE_MAC_BYTESWAP) +# include +#elif defined(HAVE_OPENBSD_BYTESWAP) +# include #endif #ifdef HAVE_STD_CODECVT @@ -48,13 +55,29 @@ namespace { inline TagLib::ushort byteSwap(TagLib::ushort x) { -#ifdef TAGLIB_BYTESWAP_16 +#if defined(HAVE_GCC_BYTESWAP_16) - return TAGLIB_BYTESWAP_16(x); + return __builtin_bswap16(x); + +#elif defined(HAVE_MSC_BYTESWAP) + + return _byteswap_ushort(x); + +#elif defined(HAVE_GLIBC_BYTESWAP) + + return __bswap_16(x); + +#elif defined(HAVE_MAC_BYTESWAP) + + return OSSwapInt16(x); + +#elif defined(HAVE_OPENBSD_BYTESWAP) + + return swap16(x); #else - return((x >> 8) & 0xff) | ((x & 0xff) << 8); + return ((x >> 8) & 0xff) | ((x & 0xff) << 8); #endif } @@ -813,12 +836,6 @@ void String::copyFromUTF16(const wchar_t *s, size_t length, Type t) void String::copyFromUTF16(const char *s, size_t length, Type t) { -#if SIZEOF_WCHAR_T == 2 && defined(TAGLIB_ALIGNMENT_TOLERANT) - - copyFromUTF16(reinterpret_cast(s), length / 2, t); - -#else - bool swap; if(t == UTF16) { if(length < 2) { @@ -850,11 +867,9 @@ void String::copyFromUTF16(const char *s, size_t length, Type t) d->data[i] = swap ? combine(*s, *(s + 1)) : combine(*(s + 1), *s); s += 2; } - -#endif } -#ifdef TAGLIB_LITTLE_ENDIAN +#if SYSTEM_BYTEORDER == 1 const String::Type String::WCharByteOrder = String::UTF16LE; diff --git a/tests/test_fileref.cpp b/tests/test_fileref.cpp index 5500ae7f..06663c97 100644 --- a/tests/test_fileref.cpp +++ b/tests/test_fileref.cpp @@ -6,9 +6,6 @@ #include #include #include "utils.h" -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif using namespace std; using namespace TagLib; diff --git a/tests/utils.h b/tests/utils.h index b69bfa50..a2d997eb 100644 --- a/tests/utils.h +++ b/tests/utils.h @@ -1,6 +1,5 @@ -#ifdef HAVE_CONFIG_H -#include -#endif +#include "config.h" + #ifdef _WIN32 #include #else