Add CAPACITYCHECK to static_vector

This commit is contained in:
drmortalwombat 2025-05-17 09:07:33 +02:00
parent be9abbf510
commit 00da0ef87d
4 changed files with 34 additions and 3 deletions

View File

@ -4,6 +4,7 @@
#include <new> #include <new>
#include <stdlib.h> #include <stdlib.h>
#include <opp/utility.h> #include <opp/utility.h>
#include <oscar.h>
namespace opp { namespace opp {
@ -20,6 +21,9 @@ public:
static_vector(size_t n) : _size(n) static_vector(size_t n) : _size(n)
{ {
#ifdef CAPACITYCHECK
if (n > N) debugcrash();
#endif
T * data = (T*)_space; T * data = (T*)_space;
for(size_t i=0; i<n; i++) for(size_t i=0; i<n; i++)
new (data + i) T(); new (data + i) T();
@ -202,6 +206,9 @@ void static_vector<T, N>::clear(void)
template <class T, int N> template <class T, int N>
void static_vector<T, N>::resize(size_t n) void static_vector<T, N>::resize(size_t n)
{ {
#ifdef CAPACITYCHECK
if (n > N) debugcrash();
#endif
T * data = (T*)_space; T * data = (T*)_space;
if (n < _size) if (n < _size)
{ {
@ -219,12 +226,18 @@ void static_vector<T, N>::resize(size_t n)
template <class T, int N> template <class T, int N>
void static_vector<T, N>::push_back(const T & t) void static_vector<T, N>::push_back(const T & t)
{ {
#ifdef CAPACITYCHECK
if (_size >= N) debugcrash();
#endif
new ((T*)_space + _size++)T(t); new ((T*)_space + _size++)T(t);
} }
template <class T, int N> template <class T, int N>
void static_vector<T, N>::push_back(T && t) void static_vector<T, N>::push_back(T && t)
{ {
#ifdef CAPACITYCHECK
if (_size >= N) debugcrash();
#endif
new ((T*)_space + _size++)T(t); new ((T*)_space + _size++)T(t);
} }
@ -232,6 +245,9 @@ template <class T, int N>
template <typename ...P> template <typename ...P>
void static_vector<T, N>::emplace_back(const P&... p) void static_vector<T, N>::emplace_back(const P&... p)
{ {
#ifdef CAPACITYCHECK
if (_size >= N) debugcrash();
#endif
new ((T*)_space + _size++)T(p...); new ((T*)_space + _size++)T(p...);
} }
@ -271,6 +287,9 @@ void static_vector<T, N>::erase(size_t at, size_t n)
template <class T, int N> template <class T, int N>
T * static_vector<T, N>::insert(T * at, const T & t) T * static_vector<T, N>::insert(T * at, const T & t)
{ {
#ifdef CAPACITYCHECK
if (_size >= N) debugcrash();
#endif
T * data = (T*)_space; T * data = (T*)_space;
T * dp = data + _size; T * dp = data + _size;
new (dp)T(); new (dp)T();

View File

@ -27,7 +27,8 @@ public:
vector(const vector & v) vector(const vector & v)
: _data((T*)malloc(v._size * sizeof(T))), _size(v._size), _capacity(v._size) : _data((T*)malloc(v._size * sizeof(T))), _size(v._size), _capacity(v._size)
{ {
for(size_t i=0; i<_size; i++) size_t n = _size;
for(size_t i=0; i<n; i++)
new (_data + i)T(v._data[i]); new (_data + i)T(v._data[i]);
} }
@ -50,14 +51,16 @@ public:
{ {
if (this != &v) if (this != &v)
{ {
for(size_t i=0; i<_size; i++) size_t n = _size;
for(size_t i=0; i<n; i++)
_data[i].~T(); _data[i].~T();
free(_data); free(_data);
_data = (T*)malloc(v._size * sizeof(T)); _data = (T*)malloc(v._size * sizeof(T));
_size = v._size; _size = v._size;
_capacity = v._size; _capacity = v._size;
for(size_t i=0; i<_size; i++) n = _size;
for(size_t i=0; i<n; i++)
new (_data + i)T(v._data[i]); new (_data + i)T(v._data[i]);
} }
return *this; return *this;

View File

@ -129,3 +129,10 @@ __native const char * oscar_expand_lzo_buf(char * dp, const char * sp)
return sp + 1; return sp + 1;
} }
void debugcrash(void)
{
__asm volatile {
byt $02
}
}

View File

@ -18,6 +18,8 @@ void breakpoint(void);
#pragma intrinsic(breakpoint) #pragma intrinsic(breakpoint)
void debugcrash(void);
#pragma compile("oscar.c") #pragma compile("oscar.c")
#endif #endif