• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2021, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file implements the `Heap::Data` (a heap allocated data).
32  */
33 
34 #include "heap_data.hpp"
35 
36 #include "common/code_utils.hpp"
37 #include "common/debug.hpp"
38 
39 namespace ot {
40 namespace Heap {
41 
SetFrom(const uint8_t * aBuffer,uint16_t aLength)42 Error Data::SetFrom(const uint8_t *aBuffer, uint16_t aLength)
43 {
44     Error error;
45 
46     SuccessOrExit(error = UpdateBuffer(aLength));
47     VerifyOrExit(aLength != 0);
48 
49     SuccessOrAssert(mData.CopyBytesFrom(aBuffer, aLength));
50 
51 exit:
52     return error;
53 }
54 
SetFrom(const Message & aMessage)55 Error Data::SetFrom(const Message &aMessage)
56 {
57     return SetFrom(aMessage, aMessage.GetOffset(), aMessage.GetLength() - aMessage.GetOffset());
58 }
59 
SetFrom(const Message & aMessage,uint16_t aOffset,uint16_t aLength)60 Error Data::SetFrom(const Message &aMessage, uint16_t aOffset, uint16_t aLength)
61 {
62     Error error;
63 
64     VerifyOrExit(aOffset + aLength <= aMessage.GetLength(), error = kErrorParse);
65 
66     SuccessOrExit(error = UpdateBuffer(aLength));
67     VerifyOrExit(aLength != 0);
68 
69     SuccessOrAssert(aMessage.Read(aOffset, mData.GetBytes(), aLength));
70 
71 exit:
72     return error;
73 }
74 
SetFrom(Data && aData)75 void Data::SetFrom(Data &&aData)
76 {
77     Free();
78     TakeFrom(aData);
79 }
80 
Free(void)81 void Data::Free(void)
82 {
83     Heap::Free(mData.GetBytes());
84     mData.Init(nullptr, 0);
85 }
86 
UpdateBuffer(uint16_t aNewLength)87 Error Data::UpdateBuffer(uint16_t aNewLength)
88 {
89     Error error = kErrorNone;
90 
91     VerifyOrExit(aNewLength != mData.GetLength());
92 
93     Heap::Free(mData.GetBytes());
94 
95     if (aNewLength == 0)
96     {
97         mData.Init(nullptr, 0);
98     }
99     else
100     {
101         uint8_t *newBuffer = static_cast<uint8_t *>(Heap::CAlloc(aNewLength, sizeof(uint8_t)));
102 
103         VerifyOrExit(newBuffer != nullptr, error = kErrorNoBufs);
104         mData.Init(newBuffer, aNewLength);
105     }
106 
107 exit:
108     return error;
109 }
110 
TakeFrom(Data & aData)111 void Data::TakeFrom(Data &aData)
112 {
113     mData.Init(aData.mData.GetBytes(), aData.GetLength());
114     aData.mData.Init(nullptr, 0);
115 }
116 
117 } // namespace Heap
118 } // namespace ot
119