Implemented more STL and page allocation for malloc.
This commit is contained in:
@@ -6,13 +6,14 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <new>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
namespace std
|
||||
{
|
||||
template<typename T, typename TAlloc = void> // TODO: allocator
|
||||
template<typename T, typename Allocator = allocator<T>>
|
||||
class vector
|
||||
{
|
||||
public:
|
||||
@@ -29,9 +30,10 @@ private:
|
||||
T* _elements = nullptr;
|
||||
size_type _size = 0;
|
||||
size_type _capacity = 0;
|
||||
Allocator _allocator = {};
|
||||
public:
|
||||
constexpr vector() noexcept = default;
|
||||
constexpr vector(const vector& other) noexcept
|
||||
constexpr vector(const vector& other) noexcept : _allocator(other._allocator)
|
||||
{
|
||||
resize(other.size());
|
||||
for (size_type idx = 0; idx < size(); ++idx)
|
||||
@@ -42,16 +44,22 @@ public:
|
||||
constexpr vector(vector&& other) noexcept
|
||||
: _elements(exchange(other._elements, nullptr)),
|
||||
_size(exchange(other._size, 0)),
|
||||
_capacity(exchange(other._capacity, 0))
|
||||
_capacity(exchange(other._capacity, 0)),
|
||||
_allocator(exchange(other._allocator, {}))
|
||||
{
|
||||
}
|
||||
constexpr explicit vector(size_type count, const value_type& value = value_type())
|
||||
constexpr explicit vector(const Allocator& allocator) noexcept : _allocator(allocator) {}
|
||||
constexpr explicit vector(size_type count, const value_type& value = value_type(), const Allocator& allocator = {}) : _allocator(allocator)
|
||||
{
|
||||
resize(count, value);
|
||||
}
|
||||
constexpr ~vector() noexcept
|
||||
{
|
||||
delete _elements;
|
||||
for (size_type idx = 0; idx < size(); ++idx)
|
||||
{
|
||||
_elements[idx].~T();
|
||||
}
|
||||
_allocator.deallocate(_elements, _capacity);
|
||||
}
|
||||
constexpr vector& operator=(const vector& other)
|
||||
{
|
||||
@@ -302,7 +310,7 @@ private:
|
||||
if (capacity() == newCapacity) {
|
||||
return;
|
||||
}
|
||||
T* newElements = static_cast<T*>(malloc(newCapacity * sizeof(T)));
|
||||
T* newElements = _allocator.allocate(newCapacity * sizeof(T));
|
||||
if (newElements == nullptr)
|
||||
{
|
||||
__ba_throw bad_alloc();
|
||||
@@ -312,7 +320,7 @@ private:
|
||||
::new (&newElements[idx]) T(move(_elements[idx]));
|
||||
_elements[idx].~T();
|
||||
}
|
||||
free(_elements);
|
||||
_allocator.deallocate(_elements, _capacity);
|
||||
_elements = newElements;
|
||||
_capacity = newCapacity;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user