blob: 362a026f6c7568a7902660126e919166ee4abdb2 [file] [log] [blame]
Fan Xuffde7862018-11-08 16:29:13 -08001/*
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 <bufferhub/UniqueIdGenerator.h>
18
19namespace android {
20namespace frameworks {
21namespace bufferhub {
22namespace V1_0 {
23namespace implementation {
24
25constexpr uint32_t UniqueIdGenerator::kInvalidId;
26
27uint32_t UniqueIdGenerator::getId() {
28 std::lock_guard<std::mutex> lock(mIdsInUseMutex);
29
30 do {
31 if (++mLastId >= std::numeric_limits<uint32_t>::max()) {
32 mLastId = kInvalidId + 1;
33 }
34 } while (mIdsInUse.find(mLastId) != mIdsInUse.end());
35
36 mIdsInUse.insert(mLastId);
37 return mLastId;
38}
39
40bool UniqueIdGenerator::freeId(uint32_t id) {
41 std::lock_guard<std::mutex> lock(mIdsInUseMutex);
42 auto iter = mIdsInUse.find(id);
43 if (iter != mIdsInUse.end()) {
44 mIdsInUse.erase(iter);
45 return true;
46 }
47
48 return false;
49}
50
51} // namespace implementation
52} // namespace V1_0
53} // namespace bufferhub
54} // namespace frameworks
55} // namespace android