summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'automotive/vehicle/2.0/default/common/src/VehicleObjectPool.cpp')
-rw-r--r--automotive/vehicle/2.0/default/common/src/VehicleObjectPool.cpp163
1 files changed, 163 insertions, 0 deletions
diff --git a/automotive/vehicle/2.0/default/common/src/VehicleObjectPool.cpp b/automotive/vehicle/2.0/default/common/src/VehicleObjectPool.cpp
new file mode 100644
index 00000000..ac1245a0
--- /dev/null
+++ b/automotive/vehicle/2.0/default/common/src/VehicleObjectPool.cpp
@@ -0,0 +1,163 @@
1/*
2 * Copyright (C) 2016 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#define LOG_TAG "automotive.vehicle@2.0-impl"
18
19#include "VehicleObjectPool.h"
20
21#include <log/log.h>
22
23#include "VehicleUtils.h"
24
25namespace android {
26namespace hardware {
27namespace automotive {
28namespace vehicle {
29namespace V2_0 {
30
31VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtain(
32 VehiclePropertyType type, size_t vecSize) {
33 return isDisposable(type, vecSize)
34 ? obtainDisposable(type, vecSize)
35 : obtainRecylable(type, vecSize);
36}
37
38VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtain(
39 const VehiclePropValue& src) {
40 if (src.prop == toInt(VehicleProperty::INVALID)) {
41 ALOGE("Unable to obtain an object from pool for unknown property");
42 return RecyclableType();
43 }
44 VehiclePropertyType type = getPropType(src.prop);
45 size_t vecSize = getVehicleRawValueVectorSize(src.value, type);;
46 auto dest = obtain(type, vecSize);
47
48 dest->prop = src.prop;
49 dest->areaId = src.areaId;
50 dest->timestamp = src.timestamp;
51 copyVehicleRawValue(&dest->value, src.value);
52
53 return dest;
54}
55
56VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtainInt32(
57 int32_t value) {
58 auto val = obtain(VehiclePropertyType::INT32);
59 val->value.int32Values[0] = value;
60 return val;
61}
62
63VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtainInt64(
64 int64_t value) {
65 auto val = obtain(VehiclePropertyType::INT64);
66 val->value.int64Values[0] = value;
67 return val;
68}
69
70VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtainFloat(
71 float value) {
72 auto val = obtain(VehiclePropertyType::FLOAT);
73 val->value.floatValues[0] = value;
74 return val;
75}
76
77VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtainString(
78 const char* cstr) {
79 auto val = obtain(VehiclePropertyType::STRING);
80 val->value.stringValue = cstr;
81 return val;
82}
83
84VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtainComplex() {
85 return obtain(VehiclePropertyType::COMPLEX);
86}
87
88VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtainRecylable(
89 VehiclePropertyType type, size_t vecSize) {
90 // VehiclePropertyType is not overlapping with vectorSize.
91 int32_t key = static_cast<int32_t>(type)
92 | static_cast<int32_t>(vecSize);
93
94 std::lock_guard<std::mutex> g(mLock);
95 auto it = mValueTypePools.find(key);
96
97 if (it == mValueTypePools.end()) {
98 auto newPool(std::make_unique<InternalPool>(type, vecSize));
99 it = mValueTypePools.emplace(key, std::move(newPool)).first;
100 }
101 return it->second->obtain();
102}
103
104VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtainBoolean(
105 bool value) {
106 return obtainInt32(value);
107}
108
109VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtainDisposable(
110 VehiclePropertyType valueType, size_t vectorSize) const {
111 return RecyclableType {
112 createVehiclePropValue(valueType, vectorSize).release(),
113 mDisposableDeleter
114 };
115}
116
117VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtain(
118 VehiclePropertyType type) {
119 return obtain(type, 1);
120}
121
122
123void VehiclePropValuePool::InternalPool::recycle(VehiclePropValue* o) {
124 if (o == nullptr) {
125 ALOGE("Attempt to recycle nullptr");
126 return;
127 }
128
129 if (!check(&o->value)) {
130 ALOGE("Discarding value for prop 0x%x because it contains "
131 "data that is not consistent with this pool. "
132 "Expected type: %d, vector size: %zu",
133 o->prop, mPropType, mVectorSize);
134 delete o;
135 } else {
136 ObjectPool<VehiclePropValue>::recycle(o);
137 }
138}
139
140bool VehiclePropValuePool::InternalPool::check(VehiclePropValue::RawValue* v) {
141 return check(&v->int32Values,
142 (VehiclePropertyType::INT32 == mPropType
143 || VehiclePropertyType::INT32_VEC == mPropType
144 || VehiclePropertyType::BOOLEAN == mPropType))
145 && check(&v->floatValues,
146 (VehiclePropertyType::FLOAT == mPropType
147 || VehiclePropertyType::FLOAT_VEC == mPropType))
148 && check(&v->int64Values,
149 VehiclePropertyType::INT64 == mPropType)
150 && check(&v->bytes,
151 VehiclePropertyType::BYTES == mPropType)
152 && v->stringValue.size() == 0;
153}
154
155VehiclePropValue* VehiclePropValuePool::InternalPool::createObject() {
156 return createVehiclePropValue(mPropType, mVectorSize).release();
157}
158
159} // namespace V2_0
160} // namespace vehicle
161} // namespace automotive
162} // namespace hardware
163} // namespace android