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 free( 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*)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 );
83 SizeType* metaData = reinterpret_cast< SizeType* >( wholeData );
84 *metaData++ = capacity;
85 *metaData++ = oldCount;
88 // copy over the old data
89 memcpy( metaData, mData, oldCount * elementSize );
97 void VectorBase::Copy( const VectorBase& vector, SizeType elementSize )
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 );
111 void VectorBase::Swap( VectorBase& vector )
113 // just swap the data pointers, metadata will swap as side effect
114 std::swap( mData, vector.mData );
117 void VectorBase::Erase( char* address, SizeType elementSize )
119 // erase can be called on an unallocated vector
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 );
131 char* VectorBase::Erase( char* first, char* last, SizeType elementSize )
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 );
150 void VectorBase::CopyMemory( char* destination, const char* source, size_t numberOfBytes )
152 if( ( ( source < destination ) && ( source + numberOfBytes > destination ) ) ||
153 ( ( destination < source ) && ( destination + numberOfBytes > source ) ) )
155 // If there is overlap, use memmove.
156 memmove( destination, source, numberOfBytes );
160 // It's safe to use memcpy if there isn't overlap.
161 memcpy( destination, source, numberOfBytes );