mirror of
https://github.com/justinian/jsix.git
synced 2025-12-10 08:24:32 -08:00
[util] Move util::vector to allocator api
Now that the allocator API exists for node_map, have vector use it as well.
This commit is contained in:
@@ -72,7 +72,7 @@ public:
|
|||||||
virtual ~node_map() {
|
virtual ~node_map() {
|
||||||
for (size_t i = 0; i < m_capacity; ++i)
|
for (size_t i = 0; i < m_capacity; ++i)
|
||||||
m_nodes[i].~node_type();
|
m_nodes[i].~node_type();
|
||||||
delete [] reinterpret_cast<uint8_t*>(m_nodes);
|
alloc::free(m_nodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
class iterator
|
class iterator
|
||||||
|
|||||||
@@ -2,24 +2,24 @@
|
|||||||
/// \file vector.h
|
/// \file vector.h
|
||||||
/// Definition of a simple dynamic vector collection for use in kernel space
|
/// Definition of a simple dynamic vector collection for use in kernel space
|
||||||
|
|
||||||
#include <new>
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include <util/allocator.h>
|
||||||
#include <util/util.h>
|
#include <util/util.h>
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
|
|
||||||
/// A dynamic array.
|
/// A dynamic array.
|
||||||
template <typename T, typename S = uint32_t>
|
template <typename T, typename S = uint32_t, typename alloc = default_allocator>
|
||||||
class vector
|
class vector
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
using count_t = S;
|
using count_t = S;
|
||||||
static constexpr count_t min_capacity = 4;
|
static constexpr count_t min_capacity = 4;
|
||||||
static constexpr count_t cap_mask = static_cast<S>(-1) >> 1;
|
static constexpr count_t cap_mask = static_cast<S>(-1) >> 1;
|
||||||
|
|
||||||
public:
|
|
||||||
/// Default constructor. Creates an empty vector with no capacity.
|
/// Default constructor. Creates an empty vector with no capacity.
|
||||||
vector() :
|
vector() :
|
||||||
m_size(0),
|
m_size(0),
|
||||||
@@ -75,7 +75,7 @@ public:
|
|||||||
|
|
||||||
bool was_static = m_capacity & ~cap_mask;
|
bool was_static = m_capacity & ~cap_mask;
|
||||||
if (!was_static)
|
if (!was_static)
|
||||||
delete [] m_elements;
|
alloc::free(m_elements);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the size of the array.
|
/// Get the size of the array.
|
||||||
@@ -272,20 +272,19 @@ public:
|
|||||||
/// \arg capacity Number of elements to allocate
|
/// \arg capacity Number of elements to allocate
|
||||||
void set_capacity(count_t capacity)
|
void set_capacity(count_t capacity)
|
||||||
{
|
{
|
||||||
|
while (capacity < m_size) remove();
|
||||||
|
|
||||||
bool was_static = m_capacity & ~cap_mask;
|
bool was_static = m_capacity & ~cap_mask;
|
||||||
T *new_array = reinterpret_cast<T*>(new uint8_t [capacity * sizeof(T)]);
|
if (was_static) {
|
||||||
count_t size = capacity > m_size ? m_size : capacity;
|
T *new_array = reinterpret_cast<T*>(alloc::allocate(capacity * sizeof(T)));
|
||||||
|
memcpy(new_array, m_elements, m_size * sizeof(T));
|
||||||
memcpy(new_array, m_elements, size * sizeof(T));
|
|
||||||
|
|
||||||
while (size < m_size) remove();
|
|
||||||
m_size = size;
|
|
||||||
m_capacity = capacity;
|
|
||||||
|
|
||||||
if (!was_static)
|
|
||||||
delete [] m_elements;
|
|
||||||
|
|
||||||
m_elements = new_array;
|
m_elements = new_array;
|
||||||
|
} else {
|
||||||
|
m_elements = reinterpret_cast<T*>(
|
||||||
|
alloc::realloc(m_elements, m_capacity * sizeof(T), capacity * sizeof(T)));
|
||||||
|
}
|
||||||
|
|
||||||
|
m_capacity = capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
Reference in New Issue
Block a user