1 /*
2  * Copyright (C) 2020 ARM Limited. All rights reserved.
3  *
4  * Copyright 2016 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include "RegisteredHandlePool.h"
20 
add(buffer_handle_t bufferHandle)21 bool RegisteredHandlePool::add(buffer_handle_t bufferHandle)
22 {
23     std::lock_guard<std::mutex> lock(mutex);
24     return bufPool.insert(bufferHandle).second;
25 }
26 
remove(void * buffer)27 native_handle_t* RegisteredHandlePool::remove(void* buffer)
28 {
29     auto bufferHandle = static_cast<native_handle_t*>(buffer);
30 
31     std::lock_guard<std::mutex> lock(mutex);
32     return bufPool.erase(bufferHandle) == 1 ? bufferHandle : nullptr;
33 }
34 
get(const void * buffer)35 buffer_handle_t RegisteredHandlePool::get(const void* buffer)
36 {
37     auto bufferHandle = static_cast<buffer_handle_t>(buffer);
38 
39     std::lock_guard<std::mutex> lock(mutex);
40     return bufPool.count(bufferHandle) == 1 ? bufferHandle : nullptr;
41 }
42 
for_each(std::function<void (const buffer_handle_t &)> fn)43 void RegisteredHandlePool::for_each(std::function<void(const buffer_handle_t &)> fn)
44 {
45     std::lock_guard<std::mutex> lock(mutex);
46     std::for_each(bufPool.begin(), bufPool.end(), fn);
47 }