• Home
  • History
  • Annotate
  • 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 <bufferhub/BufferHubIdGenerator.h>
18  #include <log/log.h>
19  
20  namespace android {
21  namespace frameworks {
22  namespace bufferhub {
23  namespace V1_0 {
24  namespace implementation {
25  
getInstance()26  BufferHubIdGenerator& BufferHubIdGenerator::getInstance() {
27      static BufferHubIdGenerator generator;
28  
29      return generator;
30  }
31  
getId()32  int BufferHubIdGenerator::getId() {
33      std::lock_guard<std::mutex> lock(mIdsInUseMutex);
34  
35      do {
36          if (++mLastId >= std::numeric_limits<int>::max()) {
37              mLastId = 0;
38          }
39      } while (mIdsInUse.find(mLastId) != mIdsInUse.end());
40  
41      mIdsInUse.insert(mLastId);
42      return mLastId;
43  }
44  
freeId(int id)45  void BufferHubIdGenerator::freeId(int id) {
46      std::lock_guard<std::mutex> lock(mIdsInUseMutex);
47      auto iter = mIdsInUse.find(id);
48      if (iter != mIdsInUse.end()) {
49          mIdsInUse.erase(iter);
50      } else {
51          ALOGW("%s: Cannot free nonexistent id #%d", __FUNCTION__, id);
52      }
53  }
54  
55  } // namespace implementation
56  } // namespace V1_0
57  } // namespace bufferhub
58  } // namespace frameworks
59  } // namespace android
60