Dali::Vector release memory later during Copy
[platform/core/uifw/dali-core.git] / dali / public-api / common / dali-vector.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/public-api/common/dali-vector.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for memcpy & memmove
23
24 namespace Dali
25 {
26 VectorBase::VectorBase()
27 : mData(nullptr)
28 {
29 }
30
31 VectorBase::~VectorBase() = default;
32
33 VectorBase::SizeType VectorBase::Capacity() const
34 {
35   SizeType capacity = 0u;
36   if(mData)
37   {
38     SizeType* metadata = reinterpret_cast<SizeType*>(mData);
39     capacity           = *(metadata - 2u);
40   }
41   return capacity;
42 }
43
44 void VectorBase::Release()
45 {
46   if(mData)
47   {
48     // adjust pointer to real beginning
49     SizeType* metadata = reinterpret_cast<SizeType*>(mData);
50
51     delete[](metadata - 2u);
52     mData = nullptr;
53   }
54 }
55
56 void VectorBase::Replace(void* newData)
57 {
58   if(mData)
59   {
60     // adjust pointer to real beginning
61     SizeType* metadata = reinterpret_cast<SizeType*>(mData);
62
63     // Cause timming issue, we set new data before metadata delete.
64     mData = newData;
65
66     // delete metadata address after mData setup safety.
67     delete[](metadata - 2u);
68   }
69   else
70   {
71     // mData was nullptr. Just copy data address
72     mData = newData;
73   }
74 }
75
76 void VectorBase::SetCount(SizeType count)
77 {
78   // someone can call Resize( 0u ) before ever populating the vector
79   if(mData)
80   {
81     SizeType* metadata = reinterpret_cast<SizeType*>(mData);
82     *(metadata - 1u)   = count;
83   }
84 }
85
86 void VectorBase::Reserve(SizeType capacity, SizeType elementSize)
87 {
88   SizeType oldCapacity = Capacity();
89   SizeType oldCount    = Count();
90   if(capacity > oldCapacity)
91   {
92     const SizeType wholeAllocation = sizeof(SizeType) * 2u + capacity * elementSize;
93     void*          wholeData       = reinterpret_cast<void*>(new uint8_t[wholeAllocation]);
94     DALI_ASSERT_ALWAYS(wholeData && "VectorBase::Reserve - Memory allocation failed");
95
96 #if defined(DEBUG_ENABLED)
97     // in debug build this will help identify a vector of uninitialized data
98     memset(wholeData, 0xaa, wholeAllocation);
99 #endif
100     SizeType* metaData = reinterpret_cast<SizeType*>(wholeData);
101     *metaData++        = capacity;
102     *metaData++        = oldCount;
103     if(mData)
104     {
105       // copy over the old data
106       memcpy(metaData, mData, oldCount * elementSize);
107     }
108     // release old buffer and set new data as mData
109     Replace(reinterpret_cast<void*>(metaData));
110   }
111 }
112
113 void VectorBase::Copy(const VectorBase& vector, SizeType elementSize)
114 {
115   // reserve space based on source capacity
116   const SizeType capacity        = vector.Capacity();
117   const SizeType wholeAllocation = sizeof(SizeType) * 2u + capacity * elementSize;
118   void*          wholeData       = reinterpret_cast<void*>(new uint8_t[wholeAllocation]);
119   DALI_ASSERT_ALWAYS(wholeData && "VectorBase::Copy - Memory allocation failed");
120
121   // copy over whole data
122   SizeType* srcData = reinterpret_cast<SizeType*>(vector.mData);
123   SizeType* dstData = reinterpret_cast<SizeType*>(wholeData) + 2u;
124   memcpy(dstData - 2u, srcData - 2u, wholeAllocation);
125
126   // release old buffer and set new data as mData
127   Replace(reinterpret_cast<void*>(dstData));
128 }
129
130 void VectorBase::Swap(VectorBase& vector)
131 {
132   // just swap the data pointers, metadata will swap as side effect
133   std::swap(mData, vector.mData);
134 }
135
136 void VectorBase::Erase(char* address, SizeType elementSize)
137 {
138   // erase can be called on an unallocated vector
139   if(mData)
140   {
141     uint8_t*       startAddress  = reinterpret_cast<uint8_t*>(address) + elementSize;
142     const uint8_t* endAddress    = reinterpret_cast<uint8_t*>(mData) + Count() * elementSize;
143     SizeType       numberOfBytes = endAddress - startAddress;
144     // addresses overlap so use memmove
145     memmove(address, startAddress, numberOfBytes);
146     SetCount(Count() - 1u);
147   }
148 }
149
150 char* VectorBase::Erase(char* first, char* last, SizeType elementSize)
151 {
152   char* next = nullptr;
153
154   if(mData)
155   {
156     uint8_t*       startAddress  = reinterpret_cast<uint8_t*>(last);
157     const uint8_t* endAddress    = reinterpret_cast<uint8_t*>(mData) + Count() * elementSize;
158     SizeType       numberOfBytes = endAddress - startAddress;
159     // addresses overlap so use memmove
160     memmove(first, startAddress, numberOfBytes);
161     SetCount(Count() - (last - first) / elementSize);
162
163     next = first;
164   }
165
166   return next;
167 }
168
169 void VectorBase::CopyMemory(char* destination, const char* source, size_t numberOfBytes)
170 {
171   if(((source < destination) && (source + numberOfBytes > destination)) ||
172      ((destination < source) && (destination + numberOfBytes > source)))
173   {
174     // If there is overlap, use memmove.
175     memmove(destination, source, numberOfBytes);
176   }
177   else
178   {
179     // It's safe to use memcpy if there isn't overlap.
180     memcpy(destination, source, numberOfBytes);
181   }
182 }
183
184 } // namespace Dali