From 09b3984df8493bdfa5aa19df47f2b98fa07857bd Mon Sep 17 00:00:00 2001 From: sbiscigl Date: Mon, 13 May 2024 17:06:22 -0400 Subject: [PATCH] avoid copying bufs --- .../include/aws/core/utils/Array.h | 44 ++++++++++++++++++- .../utils/crypto/crt/CRTSymmetricCipher.cpp | 16 ++----- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/aws-cpp-sdk-core/include/aws/core/utils/Array.h b/src/aws-cpp-sdk-core/include/aws/core/utils/Array.h index 413c86ef077..a04a044d473 100644 --- a/src/aws-cpp-sdk-core/include/aws/core/utils/Array.h +++ b/src/aws-cpp-sdk-core/include/aws/core/utils/Array.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -64,6 +65,22 @@ namespace Aws } } + /** + * Create new array with a pointer and its dimensions. + * + * @param m_size the capacity of the array. + * @param m_length the current length of the array, + * @param m_data the data that the array points to. + */ + Array(size_t m_size, + size_t m_length, + UniqueArrayPtr m_data) + : m_size(m_size), + m_length(m_length), + m_data(std::move(m_data)) + { + } + /** * Merge multiple arrays into one */ @@ -114,7 +131,7 @@ namespace Aws } //move c_tor - Array(Array&& other) : + Array(Array&& other) noexcept: m_size(other.m_size), m_length(other.m_length), m_data(std::move(other.m_data)) @@ -150,7 +167,7 @@ namespace Aws return *this; } - Array& operator=(Array&& other) + Array& operator=(Array&& other) noexcept { m_size = other.m_size; m_length = other.m_length; @@ -258,6 +275,29 @@ namespace Aws CryptoBuffer(CryptoBuffer&& other) : ByteBuffer(std::move(other)) {} CryptoBuffer& operator=(const CryptoBuffer&) = default; CryptoBuffer& operator=(CryptoBuffer&& other) { ByteBuffer::operator=(std::move(other)); return *this; } + + CryptoBuffer(Crt::ByteBuf&& other) noexcept : ByteBuffer( + other.len, + other.len, + nullptr) + { + m_data.reset(other.buffer); + other.capacity = 0; + other.len = 0; + other.allocator = nullptr; + } + + CryptoBuffer& operator=(Crt::ByteBuf&& other) noexcept + { + m_size = other.len; + m_length = other.len; + m_data.reset(other.buffer); + other.capacity = 0; + other.len = 0; + other.allocator = nullptr; + return *this; + } + bool operator==(const CryptoBuffer& other) const { return ByteBuffer::operator==(other); } bool operator!=(const CryptoBuffer& other) const { return ByteBuffer::operator!=(other); } diff --git a/src/aws-cpp-sdk-core/source/utils/crypto/crt/CRTSymmetricCipher.cpp b/src/aws-cpp-sdk-core/source/utils/crypto/crt/CRTSymmetricCipher.cpp index 3aab938d74b..eaf6d0676b4 100644 --- a/src/aws-cpp-sdk-core/source/utils/crypto/crt/CRTSymmetricCipher.cpp +++ b/src/aws-cpp-sdk-core/source/utils/crypto/crt/CRTSymmetricCipher.cpp @@ -36,9 +36,7 @@ namespace Aws if (m_cipher.Encrypt(toEncrypt, resultBuffer)) { - CryptoBuffer result{resultBuffer.buffer, resultBuffer.len}; - Crt::ByteBufDelete(resultBuffer); - return result; + return {std::move(resultBuffer)}; } Crt::ByteBufDelete(resultBuffer); return {0}; @@ -52,9 +50,7 @@ namespace Aws { auto tagCur = m_cipher.GetTag(); m_tag = CryptoBuffer(tagCur.ptr, tagCur.len); - CryptoBuffer result{resultBuffer.buffer, resultBuffer.len}; - Crt::ByteBufDelete(resultBuffer); - return result; + return {std::move(resultBuffer)}; } Crt::ByteBufDelete(resultBuffer); return {0}; @@ -69,9 +65,7 @@ namespace Aws if (m_cipher.Decrypt(toDecrypt, resultBuffer)) { - CryptoBuffer result{resultBuffer.buffer, resultBuffer.len}; - Crt::ByteBufDelete(resultBuffer); - return result; + return {std::move(resultBuffer)}; } Crt::ByteBufDelete(resultBuffer); return (0); @@ -83,9 +77,7 @@ namespace Aws if (m_cipher.FinalizeDecryption(resultBuffer)) { - CryptoBuffer result{resultBuffer.buffer, resultBuffer.len}; - Crt::ByteBufDelete(resultBuffer); - return result; + return {std::move(resultBuffer)}; } Crt::ByteBufDelete(resultBuffer); return {0};