25#include <unordered_set>
44 typename std::remove_cv_t<typename std::remove_reference_t<U>>;
61 src.resource_pool_ =
nullptr;
93 return std::hash<const T*>()(std::addressof(resource_wrapper.
Get()));
104 return singleton_allocator;
107 static std::unique_ptr<UniqueResourceAllocator>
New(
108 const std::vector<T>&
pool) {
110 return std::unique_ptr<UniqueResourceAllocator>(new_allocator);
116 std::lock_guard lock(
mutex_);
117 std::vector<T> not_selected;
118 for (
auto& new_item : another_pool) {
121 not_selected.emplace_back(std::move(new_item));
130 std::vector<T> pool_to_add;
131 pool_to_add.emplace_back(std::move(t));
136 std::vector<T> pool_to_add;
137 pool_to_add.emplace_back(t);
142 std::lock_guard<std::mutex> lock(
mutex_);
148 return {std::move(r)};
153 std::lock_guard<std::mutex> lock(
mutex_);
158 for (
int i = 0; i < n; i++) {
162 return {std::move(result)};
165 template <
typename V = T>
166 std::enable_if_t<std::is_integral<V>::value, std::optional<ReservationSet>>
168 static_assert(std::is_same<T, V>::value);
169 std::lock_guard<std::mutex> lock(
mutex_);
175 auto start_inclusive = available_resource;
178 if (!resources_opt) {
181 return resources_opt;
188 std::optional<Reservation>
Take(
const T& t) {
189 std::lock_guard<std::mutex> lock(
mutex_);
198 template <
typename Container>
199 std::optional<ReservationSet>
TakeAll(
const Container& ts) {
200 std::lock_guard<std::mutex> lock(
mutex_);
201 for (
const auto& t : ts) {
207 for (
const auto& t : ts) {
220 template <
typename V = T>
221 std::enable_if_t<std::is_integral<V>::value, std::optional<ReservationSet>>
222 TakeRange(
const T& start_inclusive,
const T& end_exclusive) {
223 static_assert(std::is_same<T, V>::value);
224 std::lock_guard<std::mutex> lock(
mutex_);
229 template <
typename Container>
234 return std::addressof(*
this) == std::addressof(other);
240 std::lock_guard<std::mutex> lock(
mutex_);
244 LOG(
ERROR) <<
"The resource " << t <<
" does not belong to this pool";
250 T tmp = std::move(*itr);
261 template <
typename V = T>
262 std::enable_if_t<std::is_integral<V>::value, std::optional<ReservationSet>>
264 static_assert(std::is_same<T, V>::value);
265 for (
auto cursor = start_inclusive; cursor < end_exclusive; cursor++) {
271 for (
auto cursor = start_inclusive; cursor < end_exclusive; cursor++) {
286 T tmp = std::move(*itr);
289 return std::addressof(*new_itr);
Definition: unique_resource_allocator.h:53
Reservation(Reservation &&src)
Definition: unique_resource_allocator.h:59
Reservation & operator=(Reservation &&src)=delete
UniqueResourceAllocator * resource_pool_
Definition: unique_resource_allocator.h:87
const T & Get() const
Definition: unique_resource_allocator.h:76
Reservation & operator=(const Reservation &)=delete
Reservation(UniqueResourceAllocator &resource_pool, const T &resource)
Definition: unique_resource_allocator.h:79
const T * resource_
Definition: unique_resource_allocator.h:88
~Reservation()
Definition: unique_resource_allocator.h:71
bool operator==(const Reservation &src) const
Definition: unique_resource_allocator.h:66
Reservation(const Reservation &)=delete
Definition: unique_resource_allocator.h:41
std::enable_if_t< std::is_integral< V >::value, std::optional< ReservationSet > > TakeRange(const T &start_inclusive, const T &end_exclusive)
Definition: unique_resource_allocator.h:222
std::unordered_set< T > allocated_resources_
Definition: unique_resource_allocator.h:292
std::vector< T > ExpandPool(std::vector< T > another_pool)
Definition: unique_resource_allocator.h:115
std::optional< ReservationSet > TakeAll(const Container &ts)
Definition: unique_resource_allocator.h:199
static UniqueResourceAllocator & Create(const std::vector< T > &pool)
Definition: unique_resource_allocator.h:102
const T * RemoveFromPool(const typename std::set< T >::iterator itr)
Definition: unique_resource_allocator.h:285
std::enable_if_t< std::is_integral< V >::value, std::optional< ReservationSet > > TakeRangeInternal(const T &start_inclusive, const T &end_exclusive)
Definition: unique_resource_allocator.h:263
std::set< T > available_resources_
Definition: unique_resource_allocator.h:291
std::optional< Reservation > UniqueItem()
Definition: unique_resource_allocator.h:141
void Reclaim(const T &t)
Definition: unique_resource_allocator.h:239
std::vector< T > ExpandPool(const T &t)
Definition: unique_resource_allocator.h:135
UniqueResourceAllocator(const Container &items)
Definition: unique_resource_allocator.h:230
typename std::remove_cv_t< typename std::remove_reference_t< U > > RemoveCvref
Definition: unique_resource_allocator.h:44
std::optional< Reservation > Take(const T &t)
Definition: unique_resource_allocator.h:188
std::unordered_set< Reservation, ReservationHash > ReservationSet
Definition: unique_resource_allocator.h:96
static std::unique_ptr< UniqueResourceAllocator > New(const std::vector< T > &pool)
Definition: unique_resource_allocator.h:107
std::optional< ReservationSet > UniqueItems(const int n)
Definition: unique_resource_allocator.h:152
bool operator==(const UniqueResourceAllocator &other) const
Definition: unique_resource_allocator.h:233
std::enable_if_t< std::is_integral< V >::value, std::optional< ReservationSet > > UniqueConsecutiveItems(const std::size_t n)
Definition: unique_resource_allocator.h:167
std::mutex mutex_
Definition: unique_resource_allocator.h:293
std::vector< T > ExpandPool(T &&t)
Definition: unique_resource_allocator.h:129
#define LOG(severity)
Definition: logging.h:223
@ ERROR
Definition: logging.h:92
std::multimap< int, std::vector< uint8_t > > pool
Definition: cvd_video_frame_buffer.cpp:34
Definition: alloc_utils.cpp:23
constexpr bool Contains(Container &&container, U &&u)
Definition: contains.h:98
Definition: logging.h:464
Definition: unique_resource_allocator.h:91
std::size_t operator()(const Reservation &resource_wrapper) const
Definition: unique_resource_allocator.h:92