diff --git a/CMakeLists.txt b/CMakeLists.txt index 33ec5d1f..c98c326b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -170,7 +170,6 @@ set(INTERNAL_HEADERS ${HEADER_DIR}/internal/catch_startup_exception_registry.h ${HEADER_DIR}/internal/catch_stream.h ${HEADER_DIR}/internal/catch_streambuf.h - ${HEADER_DIR}/internal/catch_stringdata.h ${HEADER_DIR}/internal/catch_stringref.h ${HEADER_DIR}/internal/catch_string_manip.h ${HEADER_DIR}/internal/catch_suppress_warnings.h @@ -222,7 +221,6 @@ set(IMPL_SOURCES ${HEADER_DIR}/internal/catch_section_info.cpp ${HEADER_DIR}/internal/catch_startup_exception_registry.cpp ${HEADER_DIR}/internal/catch_stream.cpp - ${HEADER_DIR}/internal/catch_stringdata.cpp ${HEADER_DIR}/internal/catch_stringref.cpp ${HEADER_DIR}/internal/catch_string_manip.cpp ${HEADER_DIR}/internal/catch_tag_alias.cpp diff --git a/include/internal/catch_stringdata.cpp b/include/internal/catch_stringdata.cpp deleted file mode 100644 index 5261739a..00000000 --- a/include/internal/catch_stringdata.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2016 Two Blue Cubes Ltd. All rights reserved. - * - * Distributed under the Boost Software License, Version 1.0. (See accompanying - * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - */ - -#include "catch_stringdata.h" -#include "catch_stringref.h" - -#include -#include -#include - -namespace Catch { - - auto StringData::getEmpty() -> StringData* { - static StringData s_empty( 0 ); - return &s_empty; - } - auto StringData::create( StringRef const& stringRef ) -> StringData* { - return create( stringRef, stringRef.size() ); - } - auto StringData::create( StringRef const& stringRef, size_t capacity ) -> StringData* { - if( capacity == 0 ) { - return getEmpty(); - } - else { - assert( stringRef.size() <= capacity ); - auto bufferLen = sizeof(StringData)+capacity; - void* buffer = new char[bufferLen]; - - return new(buffer) StringData( stringRef, capacity ); - } - } - StringData::StringData( unsigned int initialRef ) - : m_refs( initialRef ), - size( 0 ) - {} - StringData::StringData( StringRef const& stringRef, size_t capacity ) - : m_refs( 1 ), - size( capacity) - { - std::memcpy( chars, stringRef.data(), stringRef.size() ); - chars[stringRef.size() ] = 0; - } - -} // namespace Catch \ No newline at end of file diff --git a/include/internal/catch_stringdata.h b/include/internal/catch_stringdata.h deleted file mode 100644 index d4016976..00000000 --- a/include/internal/catch_stringdata.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2016 Two Blue Cubes Ltd. All rights reserved. - * - * Distributed under the Boost Software License, Version 1.0. (See accompanying - * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - */ -#ifndef CATCH_STRINGDATA_H_INCLUDED -#define CATCH_STRINGDATA_H_INCLUDED - -#include -#include - -namespace Catch { - - class StringRef; - - class StringData { - mutable std::atomic m_refs; - public: - size_t size; - union { - char chars[1]; - }; - - auto isUniquelyOwned() const noexcept -> bool { - return m_refs == 1; - } - static auto getEmpty() -> StringData*; - static auto create( StringRef const& stringRef ) -> StringData*; - static auto create( StringRef const& stringRef, size_t capacity ) -> StringData*; - - void addRef() const noexcept { - if( m_refs > 0 ) - ++m_refs; - } - void release() const noexcept { - unsigned int refs = m_refs; - if( refs > 1 ) - --m_refs; - else if( refs == 1 ) - delete[] reinterpret_cast( this ); - } - private: - StringData( unsigned int initialRef = 1 ); - StringData( StringRef const& stringRef, size_t capacity ); - - StringData( StringData const& ) = delete; - StringData& operator=( StringData const& ) = delete; - }; - -} // namespace Catch - -#endif // CATCH_STRINGDATA_H_INCLUDED diff --git a/include/internal/catch_stringref.cpp b/include/internal/catch_stringref.cpp index 61e001bc..a6389bd4 100644 --- a/include/internal/catch_stringref.cpp +++ b/include/internal/catch_stringref.cpp @@ -6,7 +6,6 @@ */ #include "catch_stringref.h" -#include "catch_stringdata.h" #include #include @@ -25,12 +24,8 @@ namespace Catch { StringRef::StringRef( StringRef const& other ) noexcept : m_start( other.m_start ), - m_size( other.m_size ), - m_data( other.m_data ) - { - if( m_data ) - m_data->addRef(); - } + m_size( other.m_size ) + {} StringRef::StringRef( StringRef&& other ) noexcept : m_start( other.m_start ), @@ -62,8 +57,7 @@ namespace Catch { {} StringRef::~StringRef() noexcept { - if( isOwned() ) - m_data->release(); + delete[] m_data; } auto StringRef::operator = ( StringRef other ) noexcept -> StringRef& { @@ -98,8 +92,10 @@ namespace Catch { void StringRef::takeOwnership() { if( !isOwned() ) { - m_data = StringData::create( *this ); - m_start = m_data->chars; + m_data = new char[m_size+1]; + strncpy( m_data, m_start, m_size ); + m_data[m_size] = '\0'; + m_start = m_data; } } auto StringRef::substr( size_type start, size_type size ) const noexcept -> StringRef { diff --git a/include/internal/catch_stringref.h b/include/internal/catch_stringref.h index b1f1c41e..5870151c 100644 --- a/include/internal/catch_stringref.h +++ b/include/internal/catch_stringref.h @@ -24,14 +24,13 @@ namespace Catch { /// threads. class StringRef { friend struct StringRefTestAccess; - friend class StringData; using size_type = size_t; char const* m_start; size_type m_size; - StringData const* m_data = nullptr; + char* m_data = nullptr; void takeOwnership();