Updates to rstl; rename some symbols; update CFLAGS

This commit is contained in:
2022-04-15 15:24:52 -04:00
parent 64057eede7
commit b8c0242ea7
21 changed files with 342 additions and 271 deletions

View File

@@ -6,13 +6,15 @@
namespace rstl {
template < typename T >
inline void construct(void* dest, const T& src) {
new(dest) T(src);
new (dest) T(src);
}
template < typename T >
inline void destroy(T* in) {
in->~T();
}
// template < typename T >
// inline void destroy(const T* in) {}
template < typename Iter >
inline void destroy(Iter begin, Iter end) {
@@ -22,16 +24,47 @@ inline void destroy(Iter begin, Iter end) {
++current;
}
}
template < typename S >
inline void destroy(S* begin, S* end) {
S* current = begin;
while (current != end) {
destroy(current);
++current;
}
}
template < typename Iter, typename T >
inline void uninitialized_copy(Iter begin, Iter end, T* in) {
inline void uninitialized_copy(Iter begin, Iter end, T* out) {
Iter current = begin;
while (current != end) {
current = *in;
construct(out, *current);
current.destroy();
++out;
++current;
}
}
template < typename S, typename D >
inline void uninitialized_copy(D* out, S* begin, S* end) {
while (begin != end) {
construct(out, *begin);
++out;
++begin;
}
// rstl::destroy(begin, end);
}
// FIXME this is a hack around regalloc
// need to figure out the proper types/positions for all of these funcs
template < typename S, typename D >
inline void uninitialized_copy_2(S* begin, D* out, S* end) {
while (begin != end) {
construct(out, *begin);
++out;
++begin;
}
}
template < typename S, typename D >
inline void uninitialized_copy_n(D* dest, S* src, size_t count) {
for (size_t i = 0; i < count; ++dest, ++i, ++src) {

View File

@@ -21,10 +21,27 @@ public:
inline iterator end() { return iterator(x4_items + x0_count); }
inline const_iterator end() const { return const_iterator(x4_items + x0_count); }
~reserved_vector() {
for (u32 i = x0_count; i > 0; --i) {
reserved_vector(const reserved_vector& other) {
x0_count = other.size();
rstl::uninitialized_copy_n(data(), other.data(), size());
}
reserved_vector& operator=(const reserved_vector& other) {
if (this != &other) {
clear();
rstl::uninitialized_copy_2(other.data(), data(), other.data() + other.size());
x0_count = other.x0_count;
}
return *this;
}
void clear() {
for (size_t i = 0; i < x0_count; ++i) {
rstl::destroy(&x4_items[i]);
}
x0_count = 0;
}
~reserved_vector() {
clear();
}
void push_back(const T& in) {

View File

@@ -44,16 +44,8 @@ public:
x0_allocator.deallocate(xc_items);
}
void reserve(size_t size); /* {
if (x8_capacity >= size) return;
T* newData;
Alloc::allocate(newData, size);
uninitialized_copy_n(newData, x4_count, xc_items);
rstl::destroy(begin(), end());
Alloc::deallocate(xc_items);
xc_items = newData;
x8_capacity = size;
}*/
void reserve(size_t size);
void push_back(const T& in) {
if (x4_count >= x8_capacity) {
reserve(x8_capacity != 0 ? x8_capacity * 2 : 4);
@@ -63,6 +55,28 @@ public:
++x4_count;
}
vector& operator=(const vector& other) {
if (this == &other)
return *this;
clear();
if (other.size() == 0) {
x0_allocator.deallocate(xc_items);
x4_count = 0;
x8_capacity = 0;
xc_items = nullptr;
} else {
reserve(other.size());
rstl::uninitialized_copy(data(), other.data(), other.data() + other.size());
x4_count = other.x4_count;
}
return *this;
}
void clear() {
rstl::destroy(begin(), end());
x4_count = 0;
}
inline T* data() { return xc_items; }
inline const T* data() const { return xc_items; }
inline size_t size() const { return x4_count; }
@@ -74,6 +88,24 @@ public:
inline T& operator[](size_t idx) { return xc_items[idx]; }
inline const T& operator[](size_t idx) const { return xc_items[idx]; }
};
template < typename T, typename Alloc >
void vector< T, Alloc >::reserve(size_t size) {
if (size <= x8_capacity)
return;
size_t sz = size * sizeof(T);
T* newData;
if (sz == 0) {
newData = nullptr;
} else {
x0_allocator.allocate(newData, sz);
}
rstl::uninitialized_copy(begin(), end(), newData);
rstl::destroy(xc_items, xc_items + x4_count);
x0_allocator.deallocate(xc_items);
xc_items = newData;
x8_capacity = size;
}
} // namespace rstl
#endif