• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
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 #include "chre/util/dynamic_vector_base.h"
18 
19 #include <cstdint>
20 #include <cstring>
21 
22 #include "chre/util/container_support.h"
23 
24 namespace chre {
25 
DynamicVectorBase(DynamicVectorBase && other)26 DynamicVectorBase::DynamicVectorBase(DynamicVectorBase &&other)
27     : mData(other.mData), mSize(other.mSize), mCapacity(other.mCapacity) {
28   other.mData = nullptr;
29   other.mSize = 0;
30   other.mCapacity = 0;
31 }
32 
doReserve(size_t newCapacity,size_t elementSize)33 bool DynamicVectorBase::doReserve(size_t newCapacity, size_t elementSize) {
34   bool success = (newCapacity <= mCapacity);
35   if (!success) {
36     void *newData = memoryAlloc(newCapacity * elementSize);
37     if (newData != nullptr) {
38       memcpy(newData, mData, mSize * elementSize);
39       memoryFree(mData);
40       mData = newData;
41       mCapacity = newCapacity;
42       success = true;
43     }
44   }
45 
46   return success;
47 }
48 
doPrepareForPush(size_t elementSize)49 bool DynamicVectorBase::doPrepareForPush(size_t elementSize) {
50   return doReserve(getNextGrowthCapacity(), elementSize);
51 }
52 
getNextGrowthCapacity() const53 size_t DynamicVectorBase::getNextGrowthCapacity() const {
54   size_t newCapacity;
55   if (mCapacity == 0) {
56     newCapacity = 1;
57   } else if (mSize == mCapacity) {
58     newCapacity = mCapacity * 2;
59   } else {
60     newCapacity = mCapacity;
61   }
62 
63   return newCapacity;
64 }
65 
doErase(size_t index,size_t elementSize)66 void DynamicVectorBase::doErase(size_t index, size_t elementSize) {
67   mSize--;
68   size_t moveAmount = (mSize - index) * elementSize;
69   memmove(static_cast<uint8_t *>(mData) + (index * elementSize),
70           static_cast<uint8_t *>(mData) + ((index + 1) * elementSize),
71           moveAmount);
72 }
73 
doPushBack(const void * element,size_t elementSize)74 bool DynamicVectorBase::doPushBack(const void *element, size_t elementSize) {
75   bool spaceAvailable = doPrepareForPush(elementSize);
76   if (spaceAvailable) {
77     memcpy(static_cast<uint8_t *>(mData) + (mSize * elementSize), element,
78            elementSize);
79     mSize++;
80   }
81 
82   return spaceAvailable;
83 }
84 
85 }  // namespace chre
86