BLT/include/blt/std/memory.h

344 lines
9.5 KiB
C
Raw Normal View History

/*
* Created by Brett on 08/02/23.
* Licensed under GNU General Public License V3.0
* See LICENSE file for license detail
*/
#ifndef BLT_TESTS_MEMORY_H
#define BLT_TESTS_MEMORY_H
2023-07-20 21:44:52 -04:00
#include <initializer_list>
#include <iterator>
2023-09-21 23:23:27 -04:00
#include <cstring>
#include <type_traits>
#include "queue.h"
2023-07-20 21:44:52 -04:00
2023-09-21 23:23:27 -04:00
namespace blt
{
2023-07-20 21:44:52 -04:00
template<typename V>
2023-09-21 23:23:27 -04:00
struct ptr_iterator
{
2023-07-20 21:44:52 -04:00
public:
using iterator_category = std::random_access_iterator_tag;
2023-09-21 23:23:27 -04:00
using difference_type = std::ptrdiff_t;
using value_type = V;
using pointer = value_type*;
using reference = value_type&;
2023-07-20 21:44:52 -04:00
2023-09-21 23:23:27 -04:00
explicit ptr_iterator(V* v): _v(v)
{}
2023-07-20 21:44:52 -04:00
2023-09-21 23:23:27 -04:00
reference operator*() const
{ return *_v; }
pointer operator->()
{ return _v; }
ptr_iterator& operator++()
{
2023-07-20 21:44:52 -04:00
_v++;
return *this;
}
2023-09-21 23:23:27 -04:00
ptr_iterator& operator--()
{
2023-07-20 21:44:52 -04:00
_v--;
return *this;
}
2023-09-21 23:23:27 -04:00
ptr_iterator operator++(int)
{
2023-07-20 21:44:52 -04:00
auto tmp = *this;
++(*this);
return tmp;
}
2023-09-21 23:23:27 -04:00
ptr_iterator operator--(int)
{
2023-07-20 21:44:52 -04:00
auto tmp = *this;
--(*this);
return tmp;
}
2023-09-21 23:23:27 -04:00
friend bool operator==(const ptr_iterator& a, const ptr_iterator& b)
{
2023-07-20 21:44:52 -04:00
return a._v == b._v;
}
2023-09-21 23:23:27 -04:00
friend bool operator!=(const ptr_iterator& a, const ptr_iterator& b)
{
2023-07-20 21:44:52 -04:00
return a._v != b._v;
}
private:
V* _v;
};
2023-09-21 23:23:27 -04:00
/**
* Creates an encapsulation of a T array which will be automatically deleted when this object goes out of scope.
* This is a simple buffer meant to be used only inside of a function and not moved around, with a few minor exceptions.
* The internal buffer is allocated on the heap.
2023-07-24 02:55:03 -04:00
* The operator * has been overloaded to return the internal buffer.
* @tparam T type that is stored in buffer eg char
*/
template<typename T>
2023-09-21 23:23:27 -04:00
struct scoped_buffer
{
private:
T* _buffer;
size_t _size;
public:
2023-09-21 23:23:27 -04:00
explicit scoped_buffer(size_t size): _size(size)
{
_buffer = new T[size];
}
scoped_buffer(const scoped_buffer& copy) = delete;
2023-09-21 23:23:27 -04:00
scoped_buffer(scoped_buffer&& move) noexcept
{
_buffer = move._buffer;
_size = move.size();
move._buffer = nullptr;
}
scoped_buffer operator=(scoped_buffer& copyAssignment) = delete;
2023-09-21 23:23:27 -04:00
scoped_buffer& operator=(scoped_buffer&& moveAssignment) noexcept
{
_buffer = moveAssignment._buffer;
_size = moveAssignment.size();
moveAssignment._buffer = nullptr;
return *this;
}
2023-09-21 23:23:27 -04:00
inline T& operator[](unsigned long index)
{
return _buffer[index];
}
2023-09-21 23:23:27 -04:00
inline const T& operator[](unsigned long index) const
{
return _buffer[index];
}
2023-09-21 23:23:27 -04:00
inline T* operator*()
{
return _buffer;
}
2023-09-21 23:23:27 -04:00
[[nodiscard]] inline size_t size() const
{
return _size;
}
2023-09-21 23:23:27 -04:00
inline T* ptr()
{
return _buffer;
}
2023-09-21 23:23:27 -04:00
ptr_iterator<T> begin()
{
return ptr_iterator{_buffer};
}
2023-09-21 23:23:27 -04:00
ptr_iterator<T> end()
{
return ptr_iterator{&_buffer[_size]};
}
2023-09-21 23:23:27 -04:00
~scoped_buffer()
{
delete[] _buffer;
}
};
2023-03-05 12:57:57 -05:00
template<typename T>
2023-09-21 23:23:27 -04:00
struct nullptr_initializer
{
2023-03-05 12:57:57 -05:00
private:
T* m_ptr = nullptr;
public:
nullptr_initializer() = default;
2023-09-21 23:23:27 -04:00
explicit nullptr_initializer(T* ptr): m_ptr(ptr)
{}
nullptr_initializer(const nullptr_initializer<T>& ptr): m_ptr(ptr.m_ptr)
{}
nullptr_initializer(nullptr_initializer<T>&& ptr) noexcept: m_ptr(ptr.m_ptr)
{}
nullptr_initializer<T>& operator=(const nullptr_initializer<T>& ptr)
{
2023-03-05 12:57:57 -05:00
if (&ptr == this)
return *this;
this->m_ptr = ptr.m_ptr;
return *this;
}
2023-09-21 23:23:27 -04:00
nullptr_initializer<T>& operator=(nullptr_initializer<T>&& ptr) noexcept
{
2023-03-05 12:57:57 -05:00
if (&ptr == this)
return *this;
this->m_ptr = ptr.m_ptr;
return *this;
}
2023-09-21 23:23:27 -04:00
inline T* operator->()
{
2023-03-05 12:57:57 -05:00
return m_ptr;
}
~nullptr_initializer() = default;
};
2023-07-20 21:44:52 -04:00
/**
* Creates a hash-map like association between an enum key and any arbitrary value.
* The storage is backed by a contiguous array for faster access.
* @tparam K enum value
* @tparam V associated value
*/
template<typename K, typename V>
2023-09-21 23:23:27 -04:00
class enum_storage
{
2023-07-20 21:44:52 -04:00
private:
2023-09-21 23:23:27 -04:00
V* m_values;
size_t m_size = 0;
2023-07-20 21:44:52 -04:00
public:
2023-09-21 23:23:27 -04:00
enum_storage(std::initializer_list<std::pair<K, V>> init)
{
2023-07-20 21:44:52 -04:00
for (auto& i : init)
2023-09-21 23:23:27 -04:00
m_size = std::max((size_t) i.first, m_size);
m_values = new V[m_size];
for (auto& v : init)
2023-09-21 23:23:27 -04:00
m_values[(size_t) v.first] = v.second;
2023-07-20 21:44:52 -04:00
}
2023-09-21 23:23:27 -04:00
inline V& operator[](size_t index)
{
return m_values[index];
2023-07-20 21:44:52 -04:00
}
2023-09-21 23:23:27 -04:00
inline const V& operator[](size_t index) const
{
return m_values[index];
2023-07-20 21:44:52 -04:00
}
2023-09-21 23:23:27 -04:00
[[nodiscard]] inline size_t size() const
{
return m_size;
}
ptr_iterator<V> begin()
{
return ptr_iterator{m_values};
}
ptr_iterator<V> end()
{
return ptr_iterator{&m_values[m_size]};
2023-07-20 21:44:52 -04:00
}
2023-09-21 23:23:27 -04:00
~enum_storage()
{
delete[] m_values;
2023-07-20 21:44:52 -04:00
}
2023-09-21 23:23:27 -04:00
};
template<typename T>
class area_allocator
{
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef void* void_pointer;
typedef const void* const_void_pointer;
private:
struct pointer_view
{
const_pointer p;
size_t n;
};
2023-07-20 21:44:52 -04:00
2023-09-21 23:23:27 -04:00
void expand()
{
size_t new_size = m_size * 2;
T* data = new T[new_size];
if constexpr (std::is_trivially_copyable_v<T>)
std::memcpy(data, m_data, m_size);
else if constexpr (std::is_move_assignable_v<T>)
{
for (size_t i = 0; i < m_size; i++)
data[i] = std::move(m_data[i]);
} else if constexpr (std::is_move_constructible_v<T>)
{
// is this bad? probably
for (size_t i = 0; i < m_size; i++)
data[i] = T(std::move(m_data));
} else if constexpr (std::is_copy_assignable_v<T>)
{
for (size_t i = 0; i < m_size; i++)
data[i] = m_data[i];
} else
{
static_assert("Unable to use this type with this allocator!");
}
delete[] m_data;
m_data = data;
m_size = new_size;
2023-07-20 21:44:52 -04:00
}
2023-09-21 23:23:27 -04:00
void realign()
{
}
public:
area_allocator()
{
m_data = new T[m_size];
2023-07-20 21:44:52 -04:00
}
2023-09-21 23:23:27 -04:00
[[nodiscard]] pointer* allocate(size_t n)
{
if (m_last + n > m_size)
expand();
pointer loc = &m_data[m_last];
m_last += n;
return loc;
}
void deallocate(pointer* p, size_t n) noexcept
{
deallocated_blocks.push({p, n});
m_deallocated += n;
// TODO: magic number
if (static_cast<double>(m_deallocated) / static_cast<double>(m_last) > 0.25)
realign();
}
~area_allocator()
{
delete[] m_data;
}
private:
// current size of the data
size_t m_size = 1;
// last allocated location
size_t m_last = 0;
// how many values have been deallocated
size_t m_deallocated = 0;
T* m_data = nullptr;
blt::flat_queue<pointer_view> deallocated_blocks;
2023-07-20 21:44:52 -04:00
};
}
#endif //BLT_TESTS_MEMORY_H