From 5e1359c9192c607285ec514f63fa94e0636a9c28 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Thu, 29 Feb 2024 00:09:57 +0000 Subject: [PATCH] correct big buffer move assignment/constructor behavior --- firmware/controllers/core/big_buffer.cpp | 24 ++++++++++++------------ unit_tests/tests/test_util.cpp | 11 +++++++++++ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/firmware/controllers/core/big_buffer.cpp b/firmware/controllers/core/big_buffer.cpp index 1183700cef..142a354c1f 100644 --- a/firmware/controllers/core/big_buffer.cpp +++ b/firmware/controllers/core/big_buffer.cpp @@ -9,6 +9,12 @@ static BigBufferUser s_currentUser; // requirements. static __attribute__((aligned(4))) uint8_t s_bigBuffer[BIG_BUFFER_SIZE]; +#if EFI_UNIT_TEST +BigBufferUser getBigBufferCurrentUser() { + return s_currentUser; +} +#endif // EFI_UNIT_TEST + static void releaseBuffer(void* bufferPtr, BigBufferUser user) { if (bufferPtr != &s_bigBuffer || user != s_currentUser) { // todo: panic! @@ -24,21 +30,15 @@ BigBufferHandle::BigBufferHandle(void* buffer, BigBufferUser user) } BigBufferHandle::BigBufferHandle(BigBufferHandle&& other) { - // swap contents of the two objects - m_bufferPtr = other.m_bufferPtr; - other.m_bufferPtr = nullptr; - - m_user = other.m_user; - other.m_user = BigBufferUser::None; + // swap contents of the two objects, the destructor will clean up the old object + std::swap(m_bufferPtr, other.m_bufferPtr); + std::swap(m_user, other.m_user); } BigBufferHandle& BigBufferHandle::operator= (BigBufferHandle&& other) { - // swap contents of the two objects - m_bufferPtr = other.m_bufferPtr; - other.m_bufferPtr = nullptr; - - m_user = other.m_user; - other.m_user = BigBufferUser::None; + // swap contents of the two objects, the destructor will clean up the old object + std::swap(m_bufferPtr, other.m_bufferPtr); + std::swap(m_user, other.m_user); return *this; } diff --git a/unit_tests/tests/test_util.cpp b/unit_tests/tests/test_util.cpp index 3e0e7e8a0d..4342d4c9fa 100644 --- a/unit_tests/tests/test_util.cpp +++ b/unit_tests/tests/test_util.cpp @@ -17,6 +17,8 @@ #include "mmc_card.h" #include "fl_stack.h" +#include "big_buffer.h" + TEST(util, testitoa) { char buffer[12]; itoa10(buffer, 239); @@ -452,3 +454,12 @@ TEST(util, wrapAngle) { EXPECT_EQ(360, wrapAngleMethod(-360)); EXPECT_EQ(1, wrapAngleMethod(-719)); } + +BigBufferUser getBigBufferCurrentUser(); + +TEST(BigBuffer, CppMagic) { + BigBufferHandle h = getBigBuffer(BigBufferUser::ToothLogger); + ASSERT_EQ(getBigBufferCurrentUser(), BigBufferUser::ToothLogger); + h = {}; + ASSERT_EQ(getBigBufferCurrentUser(), BigBufferUser::None); +}