Files
openthread/src/core/common/heap_string.cpp
T
Abtin Keshavarzian 14373d5543 [heap] introduce Move() and unify TakeFrom() for move semantics (#11972)
This change enhances the move semantics for heap-allocated container
classes (`Array`, `Data`, and `String`) by introducing a consistent
pattern.

A new `Move()` method is added to `Heap::Array`, `Heap::Data`, and
`Heap::String`. This method returns an rvalue reference to the object,
making the intent to transfer ownership explicit at the call site.

The existing `TakeFrom()` methods are updated to accept an rvalue
reference and now include a check to prevent self-assignment, which
improves robustness.

For consistency, `Heap::Data::SetFrom(Data&&)` and
`Heap::String::Set(String&&)` are renamed to `TakeFrom()`.

All call sites are updated to use the new `foo.TakeFrom(bar.Move())`
pattern, replacing the more verbose and less clear
`static_cast<...&&>(bar)`.

Unit tests are updated to validate the new `TakeFrom()` and `Move()`
semantics, including tests for self-assignment and moving from a
null (empty) container
2025-09-29 18:04:56 -07:00

98 lines
3.0 KiB
C++

/*
* Copyright (c) 2021, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file
* This file implements the `Heap::String` (a heap allocated string).
*/
#include "heap_string.hpp"
#include "common/code_utils.hpp"
#include "common/string.hpp"
namespace ot {
namespace Heap {
Error String::Set(const char *aCString)
{
Error error = kErrorNone;
size_t curSize;
size_t newSize;
VerifyOrExit(aCString != nullptr, Free());
curSize = (mStringBuffer != nullptr) ? strlen(mStringBuffer) + 1 : 0;
newSize = strlen(aCString) + 1;
if (curSize != newSize)
{
char *newBuffer = static_cast<char *>(Heap::CAlloc(sizeof(char), newSize));
VerifyOrExit(newBuffer != nullptr, error = kErrorNoBufs);
Heap::Free(mStringBuffer);
mStringBuffer = newBuffer;
}
memcpy(mStringBuffer, aCString, newSize);
exit:
return error;
}
void String::TakeFrom(String &&aString)
{
if (&aString != this)
{
Heap::Free(mStringBuffer);
mStringBuffer = aString.mStringBuffer;
aString.mStringBuffer = nullptr;
}
}
void String::Free(void)
{
Heap::Free(mStringBuffer);
mStringBuffer = nullptr;
}
bool String::operator==(const char *aCString) const
{
bool isEqual;
VerifyOrExit((aCString != nullptr) && (mStringBuffer != nullptr), isEqual = (mStringBuffer == aCString));
isEqual = StringMatch(mStringBuffer, aCString);
exit:
return isEqual;
}
} // namespace Heap
} // namespace ot