Update copyright year to 2015 for public api: core
[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     free( 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*)malloc( wholeAllocation );
79 #if defined( DEBUG_ENABLED )
80     // in debug build this will help identify a vector of uninitialized data
81     memset( wholeData, 0xaa, wholeAllocation );
82 #endif
83     SizeType* metaData = reinterpret_cast< SizeType* >( wholeData );
84     *metaData++ = capacity;
85     *metaData++ = oldCount;
86     if( mData )
87     {
88       // copy over the old data
89       memcpy( metaData, mData, oldCount * elementSize );
90       // release old buffer
91       Release();
92     }
93     mData = metaData;
94   }
95 }
96
97 void VectorBase::Copy( const VectorBase& vector, SizeType elementSize )
98 {
99   // release old data
100   Release();
101   // reserve space based on source capacity
102   const SizeType capacity = vector.Capacity();
103   Reserve( capacity, elementSize );
104   // copy over whole data
105   const SizeType wholeAllocation = sizeof(SizeType) * 2u + capacity * elementSize;
106   SizeType* srcData = reinterpret_cast< SizeType* >( vector.mData );
107   SizeType* dstData = reinterpret_cast< SizeType* >( mData );
108   memcpy( dstData - 2u, srcData - 2u, wholeAllocation );
109 }
110
111 void VectorBase::Swap( VectorBase& vector )
112 {
113   // just swap the data pointers, metadata will swap as side effect
114   std::swap( mData, vector.mData );
115 }
116
117 void VectorBase::Erase( char* address, SizeType elementSize )
118 {
119   // erase can be called on an unallocated vector
120   if( mData )
121   {
122     char* startAddress = address + elementSize;
123     const char* endAddress = reinterpret_cast< char* >( mData ) + Count() * elementSize;
124     SizeType numberOfBytes = endAddress - startAddress;
125     // addresses overlap so use memmove
126     memmove( address, startAddress, numberOfBytes );
127     SetCount( Count() - 1u );
128   }
129 }
130
131 char* VectorBase::Erase( char* first, char* last, SizeType elementSize )
132 {
133   char* next = NULL;
134
135   if( mData )
136   {
137     char* startAddress = last;
138     const char* endAddress = reinterpret_cast< char* >( mData ) + Count() * elementSize;
139     SizeType numberOfBytes = endAddress - startAddress;
140     // addresses overlap so use memmove
141     memmove( first, startAddress, numberOfBytes );
142     SetCount( Count() - ( last - first ) / elementSize );
143
144     next = first;
145   }
146
147   return next;
148 }
149
150 void VectorBase::CopyMemory( char* destination, const char* source, size_t numberOfBytes )
151 {
152   if( ( ( source < destination ) && ( source + numberOfBytes > destination ) ) ||
153       ( ( destination < source ) && ( destination + numberOfBytes > source ) ) )
154   {
155     // If there is overlap, use memmove.
156     memmove( destination, source, numberOfBytes );
157   }
158   else
159   {
160     // It's safe to use memcpy if there isn't overlap.
161     memcpy( destination, source, numberOfBytes );
162   }
163 }
164
165 } // namespace Dali
166