2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/public-api/common/dali-vector.h>
22 #include <cstring> // for memcpy & memmove
27 VectorBase::VectorBase()
32 VectorBase::~VectorBase()
36 VectorBase::SizeType VectorBase::Capacity() const
38 SizeType capacity = 0u;
41 SizeType* metadata = reinterpret_cast< SizeType* >( mData );
42 capacity = *(metadata - 2u);
48 void VectorBase::Release()
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 );
61 void VectorBase::SetCount( SizeType count )
63 // someone can call Resize( 0u ) before ever populating the vector
66 SizeType* metadata = reinterpret_cast< SizeType* >( mData );
67 *(metadata - 1u) = count;
71 void VectorBase::Reserve( SizeType capacity, SizeType elementSize )
73 SizeType oldCapacity = Capacity();
74 SizeType oldCount = Count();
75 if( capacity > oldCapacity )
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" );
81 #if defined( DEBUG_ENABLED )
82 // in debug build this will help identify a vector of uninitialized data
83 memset( wholeData, 0xaa, wholeAllocation );
85 SizeType* metaData = reinterpret_cast< SizeType* >( wholeData );
86 *metaData++ = capacity;
87 *metaData++ = oldCount;
90 // copy over the old data
91 memcpy( metaData, mData, oldCount * elementSize );
99 void VectorBase::Copy( const VectorBase& vector, SizeType elementSize )
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 );
113 void VectorBase::Swap( VectorBase& vector )
115 // just swap the data pointers, metadata will swap as side effect
116 std::swap( mData, vector.mData );
119 void VectorBase::Erase( char* address, SizeType elementSize )
121 // erase can be called on an unallocated vector
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 );
133 char* VectorBase::Erase( char* first, char* last, SizeType elementSize )
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 );
152 void VectorBase::CopyMemory( char* destination, const char* source, size_t numberOfBytes )
154 if( ( ( source < destination ) && ( source + numberOfBytes > destination ) ) ||
155 ( ( destination < source ) && ( destination + numberOfBytes > source ) ) )
157 // If there is overlap, use memmove.
158 memmove( destination, source, numberOfBytes );
162 // It's safe to use memcpy if there isn't overlap.
163 memcpy( destination, source, numberOfBytes );