Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 08:19:40

0001 // Copyright 2023 Matt Borland
0002 // Distributed under the Boost Software License, Version 1.0.
0003 // https://www.boost.org/LICENSE_1_0.txt
0004 
0005 #ifndef BOOST_CHARCONV_DETAIL_MEMCPY_HPP
0006 #define BOOST_CHARCONV_DETAIL_MEMCPY_HPP
0007 
0008 #include <boost/charconv/detail/config.hpp>
0009 #include <cstring>
0010 #include <cstdint>
0011 
0012 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89689
0013 // GCC 10 added checks for length of memcpy which yields the following warning (converted to error with -Werror)
0014 // /usr/include/x86_64-linux-gnu/bits/string_fortified.h:34:33: error: 
0015 // ‘void* __builtin___memcpy_chk(void*, const void*, long unsigned int, long unsigned int)’ specified size between 
0016 // 18446744071562067968 and 18446744073709551615 exceeds maximum object size 9223372036854775807 [-Werror=stringop-overflow=]
0017 //
0018 // memcpy is defined as taking a size_t for the count and the largest count this will recieve is the number of digits
0019 // in a 128-bit int (39) so we can safely ignore
0020 #if defined(__GNUC__) && __GNUC__ >= 10
0021 #  pragma GCC diagnostic push
0022 #  pragma GCC diagnostic ignored "-Wstringop-overflow"
0023 #  define BOOST_CHARCONV_STRINGOP_OVERFLOW_DISABLED
0024 #endif
0025 
0026 namespace boost { namespace charconv { namespace detail {
0027 
0028 #if !defined(BOOST_CHARCONV_NO_CONSTEXPR_DETECTION) && defined(BOOST_CXX14_CONSTEXPR)
0029 
0030 #define BOOST_CHARCONV_CONSTEXPR constexpr
0031 
0032 constexpr char* memcpy(char* dest, const char* src, std::size_t count)
0033 {
0034     if (BOOST_CHARCONV_IS_CONSTANT_EVALUATED(count))
0035     {
0036         for (std::size_t i = 0; i < count; ++i)
0037         {
0038             *(dest + i) = *(src + i);
0039         }
0040 
0041         return dest;
0042     }
0043     else
0044     {
0045         // Workaround for GCC-11 because it does not honor GCC diagnostic ignored
0046         // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53431
0047         // Hopefully the optimizer turns this into memcpy
0048         #if defined(__GNUC__) && __GNUC__ == 11
0049             for (std::size_t i = 0; i < count; ++i)
0050             {
0051                 *(dest + i) = *(src + i);
0052             }
0053 
0054             return dest;
0055         #else
0056             return static_cast<char*>(std::memcpy(dest, src, count));
0057         #endif
0058     }
0059 }
0060 
0061 #else // Either not C++14 or no way of telling if we are in a constexpr context
0062 
0063 #define BOOST_CHARCONV_CONSTEXPR inline
0064 
0065 inline void* memcpy(void* dest, const void* src, std::size_t count)
0066 {
0067     return std::memcpy(dest, src, count);
0068 }
0069 
0070 #endif
0071 
0072 }}} // Namespace boost::charconv::detail
0073 
0074 #ifdef BOOST_CHARCONV_STRINGOP_OVERFLOW_DISABLED
0075 #  pragma GCC diagnostic pop
0076 #endif
0077 
0078 #endif // BOOST_CHARCONV_DETAIL_MEMCPY_HPP