2021-01-01 01:39:28 +01:00
|
|
|
#pragma once
|
|
|
|
|
2022-05-08 19:29:42 +02:00
|
|
|
#include <cstdint>
|
|
|
|
#include <forward_list>
|
2021-01-01 01:39:28 +01:00
|
|
|
#include <malloc.h>
|
2022-05-08 19:29:42 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2021-01-01 01:39:28 +01:00
|
|
|
|
2022-05-08 19:29:42 +02:00
|
|
|
template<class T, class... Args>
|
|
|
|
std::unique_ptr<T> make_unique_nothrow(Args &&...args) noexcept(noexcept(T(std::forward<Args>(args)...))) {
|
|
|
|
return std::unique_ptr<T>(new (std::nothrow) T(std::forward<Args>(args)...));
|
|
|
|
}
|
2021-01-01 01:39:28 +01:00
|
|
|
|
2022-05-08 19:30:17 +02:00
|
|
|
template<typename T>
|
|
|
|
inline typename std::_MakeUniq<T>::__array make_unique_nothrow(size_t num) noexcept {
|
|
|
|
return std::unique_ptr<T>(new (std::nothrow) std::remove_extent_t<T>[num]());
|
|
|
|
}
|
|
|
|
|
2022-05-08 19:29:42 +02:00
|
|
|
template<class T, class... Args>
|
|
|
|
std::shared_ptr<T> make_shared_nothrow(Args &&...args) noexcept(noexcept(T(std::forward<Args>(args)...))) {
|
|
|
|
return std::shared_ptr<T>(new (std::nothrow) T(std::forward<Args>(args)...));
|
|
|
|
}
|
2021-01-01 01:39:28 +01:00
|
|
|
|
2022-05-08 19:29:42 +02:00
|
|
|
template<typename T, class Allocator, class Predicate>
|
|
|
|
bool remove_locked_first_if(std::mutex &mutex, std::forward_list<T, Allocator> &list, Predicate pred) {
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
auto oit = list.before_begin(), it = std::next(oit);
|
|
|
|
while (it != list.end()) {
|
|
|
|
if (pred(*it)) {
|
|
|
|
list.erase_after(oit);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
oit = it++;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-01 01:39:28 +01:00
|
|
|
|
|
|
|
// those work only in powers of 2
|
2022-02-04 16:35:35 +01:00
|
|
|
#define ROUNDDOWN(val, align) ((val) & ~(align - 1))
|
2022-05-08 19:29:42 +02:00
|
|
|
#define ROUNDUP(val, align) ROUNDDOWN(((val) + (align - 1)), align)
|