Adding basic memory resource

This commit is contained in:
Dynamitos
2025-03-20 20:15:38 +01:00
parent 51d759639e
commit e7ba74e258
47 changed files with 398 additions and 300 deletions
+19 -17
View File
@@ -1,6 +1,7 @@
#pragma once
#include "EngineTypes.h"
#include "Concepts.h"
#include "EngineTypes.h"
#include "MemoryResource.h"
#include <algorithm>
#include <assert.h>
#include <initializer_list>
@@ -12,7 +13,7 @@
#endif
namespace Seele {
template <typename T, typename Allocator = std::pmr::polymorphic_allocator<T>> struct Array {
template <typename T> struct Array {
public:
template <typename X> class IteratorBase {
public:
@@ -67,7 +68,7 @@ template <typename T, typename Allocator = std::pmr::polymorphic_allocator<T>> s
};
using value_type = T;
using allocator_type = Allocator;
using allocator_type = std::pmr::polymorphic_allocator<T>;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using pointer = T*;
@@ -82,12 +83,8 @@ template <typename T, typename Allocator = std::pmr::polymorphic_allocator<T>> s
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
constexpr Array() noexcept(noexcept(Allocator())) : arraySize(0), allocated(DEFAULT_ALLOC_SIZE), allocator(Allocator()) {
_data = allocateArray(DEFAULT_ALLOC_SIZE);
assert(_data != nullptr);
}
constexpr explicit Array(const allocator_type& alloc) noexcept : arraySize(0), allocated(DEFAULT_ALLOC_SIZE), allocator(alloc) {
constexpr Array(const allocator_type& alloc = allocator_type(debug_resource::get("Array"))) noexcept
: arraySize(0), allocated(DEFAULT_ALLOC_SIZE), allocator(alloc) {
_data = allocateArray(DEFAULT_ALLOC_SIZE);
assert(_data != nullptr);
}
@@ -138,7 +135,8 @@ template <typename T, typename Allocator = std::pmr::polymorphic_allocator<T>> s
assert(_data != nullptr);
std::uninitialized_copy(other.begin(), other.end(), begin());
}
constexpr Array(const Array& other, const Allocator& alloc) : arraySize(other.arraySize), allocated(other.allocated), allocator(alloc) {
constexpr Array(const Array& other, const allocator_type& alloc)
: arraySize(other.arraySize), allocated(other.allocated), allocator(alloc) {
_data = allocateArray(other.allocated);
assert(_data != nullptr);
std::uninitialized_copy(other.begin(), other.end(), begin());
@@ -150,7 +148,7 @@ template <typename T, typename Allocator = std::pmr::polymorphic_allocator<T>> s
other.allocated = 0;
other.arraySize = 0;
}
constexpr Array(Array&& other, const Allocator& alloc) noexcept
constexpr Array(Array&& other, const allocator_type& alloc) noexcept
: arraySize(std::move(other.arraySize)), allocated(std::move(other.allocated)), allocator(alloc) {
_data = allocateArray(other.allocated);
std::uninitialized_move(other.begin(), other.end(), begin());
@@ -179,8 +177,8 @@ template <typename T, typename Allocator = std::pmr::polymorphic_allocator<T>> s
}
return *this;
}
Array& operator=(Array&& other) noexcept(std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value ||
std::allocator_traits<Allocator>::is_always_equal::value) {
Array& operator=(Array&& other) noexcept(std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value ||
std::allocator_traits<allocator_type>::is_always_equal::value) {
if (this != &other) {
if (_data != nullptr) {
clear();
@@ -294,7 +292,7 @@ template <typename T, typename Allocator = std::pmr::polymorphic_allocator<T>> s
requires std::predicate<Pred, value_type>
constexpr void remove_if(Pred&& pred, bool keepOrder = true) {
Iterator it;
while((it = find(pred)) != end()) {
while ((it = find(pred)) != end()) {
erase(it, keepOrder);
}
}
@@ -389,7 +387,11 @@ template <typename T, typename Allocator = std::pmr::polymorphic_allocator<T>> s
assert(result != nullptr);
return result;
}
void deallocateArray(T* ptr, size_type size) { if(ptr == nullptr) return; allocator.deallocate(ptr, size); }
void deallocateArray(T* ptr, size_type size) {
if (ptr == nullptr)
return;
allocator.deallocate(ptr, size);
}
template <typename Type> T& addInternal(Type&& t) noexcept {
if (arraySize == allocated) {
size_type newSize = arraySize + 1;
@@ -449,7 +451,7 @@ template <typename T, typename Allocator = std::pmr::polymorphic_allocator<T>> s
allocator_type allocator;
};
template <class Type, class Alloc> constexpr bool operator==(const Array<Type, Alloc>& lhs, const Array<Type, Alloc>& rhs) {
template <class Type> constexpr bool operator==(const Array<Type>& lhs, const Array<Type>& rhs) {
if (lhs.size() != rhs.size()) {
return false;
}
@@ -461,7 +463,7 @@ template <class Type, class Alloc> constexpr bool operator==(const Array<Type, A
return true;
}
template <class Type, class Alloc> constexpr auto operator<=>(const Array<Type, Alloc>& lhs, const Array<Type, Alloc>& rhs) {
template <class Type> constexpr auto operator<=>(const Array<Type>& lhs, const Array<Type>& rhs) {
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
+3
View File
@@ -3,6 +3,8 @@ target_sources(Engine
Array.h
List.h
Map.h
MemoryResource.h
MemoryResource.cpp
Pair.h
Set.h
Tree.h)
@@ -13,6 +15,7 @@ target_sources(Engine
Array.h
List.h
Map.h
MemoryResource.h
Pair.h
Set.h
Tree.h)
+3 -4
View File
@@ -5,12 +5,11 @@
#include "Tree.h"
#include <utility>
namespace Seele {
template <typename KeyType, typename PairType> struct _KeyFun {
const KeyType& operator()(const PairType& pair) const { return pair.key; }
};
template <typename K, typename V, typename Compare = std::less<K>, typename Allocator = std::pmr::polymorphic_allocator<Pair<const K, V>>>
template <typename K, typename V, typename Compare = std::less<K>, typename Allocator = std::pmr::polymorphic_allocator<Pair<K, V>>>
struct Map : public Tree<K, Pair<K, V>, _KeyFun<K, Pair<K, V>>, Compare, Allocator> {
using Super = Tree<K, Pair<K, V>, _KeyFun<K, Pair<K, V>>, Compare, Allocator>;
using key_type = Super::key_type;
@@ -35,8 +34,8 @@ struct Map : public Tree<K, Pair<K, V>, _KeyFun<K, Pair<K, V>>, Compare, Allocat
: Super(comp, alloc) {}
constexpr explicit Map(const Allocator& alloc) noexcept(noexcept(Compare())) : Super(alloc) {}
constexpr Map(std::initializer_list<Pair<K, V>> init) : Super(init) {}
constexpr mapped_type& operator[](const key_type& key) {
auto [it, inserted] = Super::insert(Pair<K, V>(key, V()));
constexpr mapped_type& operator[](const key_type& key) {
auto [it, _] = Super::insert(Pair<K, V>(key));
return it->value;
}
constexpr const mapped_type& operator[](const key_type& key) const { return Super::find(key)->value; }
+34
View File
@@ -0,0 +1,34 @@
#include "MemoryResource.h"
#include "Map.h"
#include <iostream>
using namespace Seele;
std::mutex allocationMutex;
std::map<std::string, std::atomic_uint64_t> allocationCounters;
std::mutex resourcesMutex;
std::map<std::string, debug_resource> debugResources;
std::atomic_uint64_t& MemoryManager::getAllocationCounter(const std::string& name) {
std::unique_lock l(allocationMutex);
return allocationCounters[name];
}
void MemoryManager::printAllocations() {
std::unique_lock l(allocationMutex);
for (const auto& [name, counter] : allocationCounters) {
if (name.empty())
{
std::cout << name << ": " << counter << std::endl;
}
else
{
std::cout << name << ": " << counter << std::endl;
}
}
}
debug_resource* debug_resource::get(const std::string& name) {
std::unique_lock l(resourcesMutex);
return &debugResources[name];
}
+39
View File
@@ -0,0 +1,39 @@
#pragma once
#include <memory_resource>
namespace Seele {
class MemoryManager {
public:
static std::atomic_uint64_t& getAllocationCounter(const std::string& name);
static void printAllocations();
private:
};
class debug_resource : public std::pmr::memory_resource {
public:
static debug_resource* get(const std::string& name);
debug_resource() : name(""), upstream(std::pmr::get_default_resource()), counter(MemoryManager::getAllocationCounter(name)) {}
debug_resource(std::string name, std::pmr::memory_resource* up = std::pmr::get_default_resource())
: name(name), upstream(up), counter(MemoryManager::getAllocationCounter(name)) {}
debug_resource(const debug_resource& other) = delete;
debug_resource& operator=(const debug_resource& other) = delete;
void* do_allocate(size_t bytes, size_t alignment) override {
counter += bytes;
return upstream->allocate(bytes, alignment);
}
void do_deallocate(void* ptr, size_t bytes, size_t alignment) override {
upstream->deallocate(ptr, bytes, alignment);
counter -= bytes;
}
bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override { return this == &other; }
private:
std::string name;
std::pmr::memory_resource* upstream;
std::atomic_uint64_t& counter;
};
} // namespace Seele
-8
View File
@@ -3,14 +3,6 @@
namespace Seele {
template <typename K, typename V> struct Pair {
public:
constexpr Pair() : key(K()), value(V()) {}
constexpr Pair(const K& key, const V& value) : key(key), value(value) {}
template <class U1 = K, class U2 = V> constexpr Pair(U1&& x, U2&& y) : key(std::forward<U1>(x)), value(std::forward<U2>(y)) {}
Pair(const Pair& other) = default;
Pair(Pair&& other) = default;
~Pair() {}
Pair& operator=(const Pair& other) = default;
Pair& operator=(Pair&& other) = default;
constexpr friend bool operator<(const Pair& left, const Pair& right) {
return left.key < right.key || (!(right.key < left.key) && left.value < right.value);
}
+125 -93
View File
@@ -1,15 +1,21 @@
#pragma once
#include "Pair.h"
#include "Array.h"
#include "Pair.h"
#include <map>
#include <memory_resource>
#include <memory>
namespace Seele {
template <typename KeyType, typename NodeData, typename KeyFun, typename Compare, typename Allocator> struct Tree {
protected:
struct Node {
Node(Node* left, Node* right, const NodeData& nodeData) : leftChild(left), rightChild(right), data(nodeData) {}
Node(Node* left, Node* right, NodeData&& nodeData) : leftChild(left), rightChild(right), data(std::move(nodeData)) {}
Node(const NodeData& data)
: data(data)
{}
Node(NodeData&& data)
: data(std::move(data)) {}
Node* parent = nullptr;
Node* leftChild;
Node* rightChild;
NodeData data;
@@ -17,79 +23,6 @@ template <typename KeyType, typename NodeData, typename KeyFun, typename Compare
using NodeAlloc = std::allocator_traits<Allocator>::template rebind_alloc<Node>;
public:
template <typename IterType> class IteratorBase {
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = IterType;
using difference_type = std::ptrdiff_t;
using reference = IterType&;
using pointer = IterType*;
constexpr IteratorBase(Node* x = nullptr, Array<Node*>&& beginIt = Array<Node*>()) : node(x), traversal(std::move(beginIt)) {}
constexpr IteratorBase(const IteratorBase& i) : node(i.node), traversal(i.traversal) {}
constexpr IteratorBase(IteratorBase&& i) noexcept : node(std::move(i.node)), traversal(std::move(i.traversal)) {}
constexpr IteratorBase& operator=(const IteratorBase& other) {
if (this != &other) {
node = other.node;
traversal = other.traversal;
}
return *this;
}
constexpr IteratorBase& operator=(IteratorBase&& other) noexcept {
if (this != &other) {
node = std::move(other.node);
traversal = std::move(other.traversal);
}
return *this;
}
constexpr reference operator*() const { return node->data; }
constexpr pointer operator->() const { return &(node->data); }
constexpr bool operator!=(const IteratorBase& other) { return node != other.node; }
constexpr bool operator==(const IteratorBase& other) { return node == other.node; }
constexpr std::strong_ordering operator<=>(const IteratorBase& other) { return node <=> other.node; }
constexpr IteratorBase& operator++() {
node = node->rightChild;
while (node != nullptr && node->leftChild != nullptr) {
traversal.add(node);
node = node->leftChild;
}
if (node == nullptr && traversal.size() > 0) {
node = traversal.back();
traversal.pop();
}
return *this;
}
constexpr IteratorBase& operator--() {
node = node->leftChild;
while (node != nullptr && node->rightChild != nullptr) {
traversal.add(node);
node = node->rightchild;
}
if (node == nullptr && traversal.size() > 0) {
node = traversal.back();
traversal.pop();
}
return *this;
}
constexpr IteratorBase operator--(int) {
IteratorBase tmp(*this);
--*this;
return tmp;
}
constexpr IteratorBase operator++(int) {
IteratorBase tmp(*this);
++*this;
return tmp;
}
Node* getNode() { return node; }
private:
Node* node;
Array<Node*> traversal;
};
using Iterator = IteratorBase<NodeData>;
using ConstIterator = IteratorBase<const NodeData>;
using key_type = KeyType;
using value_type = NodeData;
using allocator_type = Allocator;
@@ -100,6 +33,95 @@ template <typename KeyType, typename NodeData, typename KeyFun, typename Compare
using pointer = std::allocator_traits<Allocator>::pointer;
using const_pointer = std::allocator_traits<Allocator>::const_pointer;
class ConstIterator {
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = value_type;
using difference_type = difference_type;
using pointer = const_pointer;
using reference = const value_type&;
constexpr ConstIterator(Node* x = nullptr) : node(x) {}
constexpr ConstIterator(const ConstIterator& i) = default;
constexpr ConstIterator(ConstIterator&& i) noexcept = default;
constexpr ConstIterator& operator=(const ConstIterator& other) = default;
constexpr ConstIterator& operator=(ConstIterator&& other) noexcept = default;
constexpr reference operator*() const noexcept { return node->data; }
constexpr pointer operator->() const noexcept { return std::pointer_traits<pointer>::pointer_to(**this); }
constexpr bool operator==(const ConstIterator& other) { return node == other.node; }
constexpr bool operator!=(const ConstIterator& other) { return node != other.node; }
constexpr std::strong_ordering operator<=>(const ConstIterator& other) { return node <=> other.node; }
constexpr ConstIterator& operator++() noexcept {
node = node->rightChild;
while (node != nullptr && node->leftChild != nullptr) {
node = node->leftChild;
}
if (node == nullptr && node->parent != nullptr) {
node = node->parent;
}
return *this;
}
constexpr ConstIterator& operator--() {
node = node->leftChild;
while (node != nullptr && node->rightChild != nullptr) {
node = node->rightchild;
}
if (node == nullptr && node->parent != nullptr) {
node = node->parent;
}
return *this;
}
constexpr ConstIterator operator--(int) {
ConstIterator tmp(*this);
--*this;
return tmp;
}
constexpr ConstIterator operator++(int) {
ConstIterator tmp(*this);
++*this;
return tmp;
}
Node* getNode() { return node; }
protected:
Node* node;
};
class Iterator : public ConstIterator {
public:
using value_type = value_type;
using difference_type = difference_type;
using pointer = pointer;
using reference = value_type&;
constexpr Iterator(Node* x = nullptr) : ConstIterator(x) {}
constexpr Iterator(const Iterator& i) = default;
constexpr Iterator(Iterator&& i) noexcept = default;
constexpr Iterator& operator=(const Iterator& other) = default;
constexpr Iterator& operator=(Iterator&& other) noexcept = default;
constexpr reference operator*() const noexcept { return const_cast<reference>(ConstIterator::operator*()); }
constexpr pointer operator->() const noexcept { return std::pointer_traits<pointer>::pointer_to(**this); }
constexpr Iterator& operator++() noexcept {
ConstIterator::operator++();
return *this;
}
constexpr Iterator& operator--() noexcept {
ConstIterator::operator--();
return *this;
}
constexpr Iterator& operator++(int) noexcept {
Iterator tmp = *this;
ConstIterator::operator++();
return tmp;
}
constexpr Iterator& operator--(int) noexcept {
Iterator tmp = *this;
ConstIterator::operator--();
return tmp;
}
};
using iterator = Iterator;
using const_iterator = ConstIterator;
using reverse_iterator = std::reverse_iterator<iterator>;
@@ -212,12 +234,12 @@ template <typename KeyType, typename NodeData, typename KeyFun, typename Compare
return iterator(it);
}
constexpr Pair<iterator, bool> insert(const NodeData& data) {
auto [r, inserted] = _insert(root, data);
const auto& [r, inserted] = _insert(root, data);
root = r;
return Pair<iterator, bool>(iterator(root), inserted);
}
constexpr Pair<iterator, bool> insert(NodeData&& data) {
auto [r, inserted] = _insert(root, std::move(data));
const auto& [r, inserted] = _insert(root, std::move(data));
root = r;
return Pair<iterator, bool>(iterator(root), inserted);
}
@@ -242,25 +264,20 @@ template <typename KeyType, typename NodeData, typename KeyFun, typename Compare
}
constexpr Iterator calcBeginIterator() const {
Node* begin = root;
Array<Node*> beginTraversal;
while (begin != nullptr) {
beginTraversal.add(begin);
begin = begin->leftChild;
}
if (!beginTraversal.empty()) {
begin = beginTraversal.back();
beginTraversal.pop();
if (begin->parent != nullptr) {
begin = begin->parent;
}
return Iterator(begin, std::move(beginTraversal));
return Iterator(begin);
}
constexpr Iterator calcEndIterator() const {
Node* endIndex = root;
Array<Node*> endTraversal;
while (endIndex != nullptr) {
endTraversal.add(endIndex);
endIndex = endIndex->rightChild;
Node* endNode = root;
while (endNode != nullptr) {
endNode = endNode->rightChild;
}
return Iterator(endIndex, std::move(endTraversal));
return Iterator(endNode);
}
NodeAlloc alloc;
Node* root;
@@ -273,20 +290,24 @@ template <typename KeyType, typename NodeData, typename KeyFun, typename Compare
Node* rotateLeft(Node* x) {
Node* y = x->rightChild;
x->rightChild = y->leftChild;
x->rightChild->parent = x;
y->leftChild = x;
y->leftChild->parent = y;
return y;
}
Node* rotateRight(Node* x) {
Node* y = x->leftChild;
x->leftChild = y->rightChild;
x->leftChild->parent = x;
y->rightChild = x;
y->rightChild->parent = y;
return y;
}
template <class NodeDataType> Pair<Node*, bool> _insert(Node* r, NodeDataType&& data) {
markIteratorsDirty();
if (r == nullptr) {
root = alloc.allocate(1);
std::allocator_traits<NodeAlloc>::construct(alloc, root, nullptr, nullptr, std::forward<NodeDataType>(data));
std::allocator_traits<NodeAlloc>::construct(alloc, root, std::forward<NodeDataType>(data));
_size++;
return Pair<Node*, bool>(root, true);
}
@@ -296,15 +317,19 @@ template <typename KeyType, typename NodeData, typename KeyFun, typename Compare
return Pair<Node*, bool>(r, false);
Node* newNode = alloc.allocate(1);
std::allocator_traits<NodeAlloc>::construct(alloc, newNode, nullptr, nullptr, std::forward<NodeDataType>(data));
std::allocator_traits<NodeAlloc>::construct(alloc, newNode, std::forward<NodeDataType>(data));
if (comp(keyFun(newNode->data), keyFun(r->data))) {
newNode->rightChild = r;
newNode->rightChild->parent = newNode;
newNode->leftChild = r->leftChild;
newNode->leftChild->parent = newNode;
r->leftChild = nullptr;
} else {
newNode->leftChild = r;
newNode->leftChild->parent = newNode;
newNode->rightChild = r->rightChild;
newNode->rightChild->parent = newNode;
r->rightChild = nullptr;
}
_size++;
@@ -329,6 +354,7 @@ template <typename KeyType, typename NodeData, typename KeyFun, typename Compare
r = _splay(r->leftChild, key);
r->rightChild = temp->rightChild;
r->rightChild->parent = r;
}
std::allocator_traits<NodeAlloc>::destroy(alloc, temp);
alloc.deallocate(temp, 1);
@@ -346,13 +372,16 @@ template <typename KeyType, typename NodeData, typename KeyFun, typename Compare
if (comp(key, keyFun(r->leftChild->data))) {
r->leftChild->leftChild = _splay(r->leftChild->leftChild, key);
r->leftChild->leftChild->parent = r->leftChild;
r = rotateRight(r);
} else if (comp(keyFun(r->leftChild->data), key)) {
r->leftChild->rightChild = _splay(r->leftChild->rightChild, key);
r->leftChild->rightChild->parent = r->leftChild;
if (r->leftChild->rightChild != nullptr) {
r->leftChild = rotateLeft(r->leftChild);
r->leftChild->parent = r;
}
}
return (r->leftChild == nullptr) ? r : rotateRight(r);
@@ -362,12 +391,15 @@ template <typename KeyType, typename NodeData, typename KeyFun, typename Compare
if (comp(key, keyFun(r->rightChild->data))) {
r->rightChild->leftChild = _splay(r->rightChild->leftChild, key);
r->rightChild->leftChild->parent = r->rightChild;
if (r->rightChild->leftChild != nullptr) {
r->rightChild = rotateRight(r->rightChild);
r->rightChild->parent = r;
}
} else if (comp(keyFun(r->rightChild->data), key)) {
r->rightChild->rightChild = _splay(r->rightChild->rightChild, key);
r->rightChild->rightChild->parent = r->rightChild;
r = rotateLeft(r);
}