Skip to content

Commit

Permalink
avoid copying bufs
Browse files Browse the repository at this point in the history
  • Loading branch information
sbiscigl committed May 16, 2024
1 parent e6f113c commit 09b3984
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
44 changes: 42 additions & 2 deletions src/aws-cpp-sdk-core/include/aws/core/utils/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <aws/core/utils/memory/AWSMemory.h>
#include <aws/core/utils/memory/stl/AWSVector.h>
#include <aws/crt/Types.h>
#include <memory>
#include <cassert>
#include <cstring>
Expand Down Expand Up @@ -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<T> m_data)
: m_size(m_size),
m_length(m_length),
m_data(std::move(m_data))
{
}

/**
* Merge multiple arrays into one
*/
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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); }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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};
Expand All @@ -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);
Expand All @@ -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};
Expand Down

0 comments on commit 09b3984

Please sign in to comment.