9dd7d8c8d59fe41a59b5427aef4622e62fafb558
[platform/core/uifw/dali-core.git] / dali / public-api / common / dali-vector.cpp
1 /*
2  * Copyright (c) 2015 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
27 VectorBase::VectorBase()
28   : mData( NULL )
29 {
30 }
31
32 VectorBase::~VectorBase()
33 {
34 }
35
36 VectorBase::SizeType VectorBase::Capacity() const
37 {
38   SizeType capacity = 0u;
39   if( mData )
40   {
41     SizeType* metadata = reinterpret_cast< SizeType* >( mData );
42     capacity = *(metadata - 2u);
43   }
44   return capacity;
45 }
46
47
48 void VectorBase::Release()
49 {
50   if( mData )
51   {
52     // adjust pointer to real beginning
53     SizeType* metadata = reinterpret_cast< SizeType* >( mData );
54     // TODO would be nice to memset to a bitpattern to catch illegal use of container after release
55     // but that would require knowledge of the itemsize
56     delete [] ( metadata - 2u );
57     mData = 0u;
58   }
59 }
60
61 void VectorBase::SetCount( SizeType count )
62 {
63   // someone can call Resize( 0u ) before ever populating the vector
64   if( mData )
65   {
66     SizeType* metadata = reinterpret_cast< SizeType* >( mData );
67     *(metadata - 1u) = count;
68   }
69 }
70
71 void VectorBase::Reserve( SizeType capacity, SizeType elementSize )
72 {
73   SizeType oldCapacity = Capacity();
74   SizeType oldCount = Count();
75   if( capacity > oldCapacity )
76   {
77     const SizeType wholeAllocation = sizeof(SizeType) * 2u + capacity * elementSize;
78     void* wholeData = (void*) new unsigned char[ wholeAllocation ];
79     DALI_ASSERT_ALWAYS( wholeData && "VectorBase::Reserve - Memory allocation failed" );
80
81 #if defined( DEBUG_ENABLED )
82     // in debug build this will help identify a vector of uninitialized data
83     memset( wholeData, 0xaa, wholeAllocation );
84 #endif
85     SizeType* metaData = reinterpret_cast< SizeType* >( wholeData );
86     *metaData++ = capacity;
87     *metaData++ = oldCount;
88     if( mData )
89     {
90       // copy over the old data
91       memcpy( metaData, mData, oldCount * elementSize );
92       // release old buffer
93       Release();
94     }
95     mData = metaData;
96   }
97 }
98
99 void VectorBase::Copy( const VectorBase& vector, SizeType elementSize )
100 {
101   // release old data
102   Release();
103   // reserve space based on source capacity
104   const SizeType capacity = vector.Capacity();
105   Reserve( capacity, elementSize );
106   // copy over whole data
107   const SizeType wholeAllocation = sizeof(SizeType) * 2u + capacity * elementSize;
108   SizeType* srcData = reinterpret_cast< SizeType* >( vector.mData );
109   SizeType* dstData = reinterpret_cast< SizeType* >( mData );
110   memcpy( dstData - 2u, srcData - 2u, wholeAllocation );
111 }
112
113 void VectorBase::Swap( VectorBase& vector )
114 {
115   // just swap the data pointers, metadata will swap as side effect
116   std::swap( mData, vector.mData );
117 }
118
119 void VectorBase::Erase( char* address, SizeType elementSize )
120 {
121   // erase can be called on an unallocated vector
122   if( mData )
123   {
124     char* startAddress = address + elementSize;
125     const char* endAddress = reinterpret_cast< char* >( mData ) + Count() * elementSize;
126     SizeType numberOfBytes = endAddress - startAddress;
127     // addresses overlap so use memmove
128     memmove( address, startAddress, numberOfBytes );
129     SetCount( Count() - 1u );
130   }
131 }
132
133 char* VectorBase::Erase( char* first, char* last, SizeType elementSize )
134 {
135   char* next = NULL;
136
137   if( mData )
138   {
139     char* startAddress = last;
140     const char* endAddress = reinterpret_cast< char* >( mData ) + Count() * elementSize;
141     SizeType numberOfBytes = endAddress - startAddress;
142     // addresses overlap so use memmove
143     memmove( first, startAddress, numberOfBytes );
144     SetCount( Count() - ( last - first ) / elementSize );
145
146     next = first;
147   }
148
149   return next;
150 }
151
152 void VectorBase::CopyMemory( char* destination, const char* source, size_t numberOfBytes )
153 {
154   if( ( ( source < destination ) && ( source + numberOfBytes > destination ) ) ||
155       ( ( destination < source ) && ( destination + numberOfBytes > source ) ) )
156   {
157     // If there is overlap, use memmove.
158     memmove( destination, source, numberOfBytes );
159   }
160   else
161   {
162     // It's safe to use memcpy if there isn't overlap.
163     memcpy( destination, source, numberOfBytes );
164   }
165 }
166
167 } // namespace Dali
168