rpcsx/orbis-kernel/include/orbis/KernelAllocator.hpp

63 lines
1.9 KiB
C++
Raw Normal View History

2023-07-04 18:19:17 +02:00
#pragma once
#include "utils/Rc.hpp"
#include <deque>
#include <map>
2023-07-06 18:16:25 +02:00
#include <string>
2023-07-04 18:19:17 +02:00
#include <unordered_map>
2023-07-06 18:16:25 +02:00
#include <utility>
#include <vector>
2023-07-04 18:19:17 +02:00
namespace orbis {
inline namespace utils {
void *kalloc(std::size_t size, std::size_t align);
2023-07-06 18:16:25 +02:00
void kfree(void *ptr, std::size_t size);
2023-07-04 18:19:17 +02:00
template <typename T> struct kallocator {
using value_type = T;
template <typename U> struct rebind {
using other = kallocator<U>;
};
2023-07-04 18:19:17 +02:00
T *allocate(std::size_t n) {
return static_cast<T *>(kalloc(sizeof(T) * n, alignof(T)));
}
2023-07-06 18:16:25 +02:00
void deallocate(T *p, std::size_t n) { kfree(p, sizeof(T) * n); }
2023-07-04 18:19:17 +02:00
template <typename U>
friend constexpr bool operator==(const kallocator &,
const kallocator<U> &) noexcept {
return true;
}
};
2023-07-06 18:16:25 +02:00
using kstring =
std::basic_string<char, std::char_traits<char>, kallocator<char>>;
2023-07-04 18:19:17 +02:00
template <typename T> using kvector = std::vector<T, kallocator<T>>;
template <typename T> using kdeque = std::deque<T, kallocator<T>>;
template <typename K, typename T, typename Cmp = std::less<>>
using kmap = std::map<K, T, Cmp, kallocator<std::pair<const K, T>>>;
template <typename K, typename T, typename Cmp = std::less<>>
using kmultimap = std::multimap<K, T, Cmp, kallocator<std::pair<const K, T>>>;
2023-07-04 18:19:17 +02:00
template <typename K, typename T, typename Hash = std::hash<K>,
typename Pred = std::equal_to<K>>
using kunmap =
std::unordered_map<K, T, Hash, Pred, kallocator<std::pair<const K, T>>>;
} // namespace utils
template <typename T, typename... Args> T *knew(Args &&...args) {
auto loc = static_cast<T *>(utils::kalloc(sizeof(T), alignof(T)));
auto res = std::construct_at(loc, std::forward<Args>(args)...);
if constexpr (requires(T *t) { t->_total_size = sizeof(T); })
2023-07-04 18:19:17 +02:00
res->_total_size = sizeof(T);
return res;
}
template <typename T> void kdelete(T *ptr) {
ptr->~T();
utils::kfree(ptr, sizeof(T));
}
} // namespace orbis