blob: 6444a033e1f0fc1d8371230a5af7efc5bf0eccc7 [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
Fan Xu1c16df52018-11-19 16:27:27 -080017#include <bufferhub/BufferHubIdGenerator.h>
Fan Xuffde7862018-11-08 16:29:13 -080018
19namespace android {
20namespace frameworks {
21namespace bufferhub {
22namespace V1_0 {
23namespace implementation {
24
Fan Xu1c16df52018-11-19 16:27:27 -080025constexpr uint32_t BufferHubIdGenerator::kInvalidId;
Fan Xuffde7862018-11-08 16:29:13 -080026
Fan Xu1c16df52018-11-19 16:27:27 -080027BufferHubIdGenerator& BufferHubIdGenerator::getInstance() {
28 static BufferHubIdGenerator generator;
29
30 return generator;
31}
32
33uint32_t BufferHubIdGenerator::getId() {
Fan Xuffde7862018-11-08 16:29:13 -080034 std::lock_guard<std::mutex> lock(mIdsInUseMutex);
35
36 do {
37 if (++mLastId >= std::numeric_limits<uint32_t>::max()) {
38 mLastId = kInvalidId + 1;
39 }
40 } while (mIdsInUse.find(mLastId) != mIdsInUse.end());
41
42 mIdsInUse.insert(mLastId);
43 return mLastId;
44}
45
Fan Xu1c16df52018-11-19 16:27:27 -080046bool BufferHubIdGenerator::freeId(uint32_t id) {
Fan Xuffde7862018-11-08 16:29:13 -080047 std::lock_guard<std::mutex> lock(mIdsInUseMutex);
48 auto iter = mIdsInUse.find(id);
49 if (iter != mIdsInUse.end()) {
50 mIdsInUse.erase(iter);
51 return true;
52 }
53
54 return false;
55}
56
57} // namespace implementation
58} // namespace V1_0
59} // namespace bufferhub
60} // namespace frameworks
61} // namespace android