From 00da0ef87d9b58199e2b0b1803d2a1bea048fca0 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Sat, 17 May 2025 09:07:33 +0200 Subject: [PATCH] Add CAPACITYCHECK to static_vector --- include/opp/static_vector.h | 19 +++++++++++++++++++ include/opp/vector.h | 9 ++++++--- include/oscar.c | 7 +++++++ include/oscar.h | 2 ++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/include/opp/static_vector.h b/include/opp/static_vector.h index 00cb1d2..dc5c617 100644 --- a/include/opp/static_vector.h +++ b/include/opp/static_vector.h @@ -4,6 +4,7 @@ #include #include #include +#include namespace opp { @@ -20,6 +21,9 @@ public: static_vector(size_t n) : _size(n) { +#ifdef CAPACITYCHECK + if (n > N) debugcrash(); +#endif T * data = (T*)_space; for(size_t i=0; i::clear(void) template void static_vector::resize(size_t n) { +#ifdef CAPACITYCHECK + if (n > N) debugcrash(); +#endif T * data = (T*)_space; if (n < _size) { @@ -219,12 +226,18 @@ void static_vector::resize(size_t n) template void static_vector::push_back(const T & t) { +#ifdef CAPACITYCHECK + if (_size >= N) debugcrash(); +#endif new ((T*)_space + _size++)T(t); } template void static_vector::push_back(T && t) { +#ifdef CAPACITYCHECK + if (_size >= N) debugcrash(); +#endif new ((T*)_space + _size++)T(t); } @@ -232,6 +245,9 @@ template template void static_vector::emplace_back(const P&... p) { +#ifdef CAPACITYCHECK + if (_size >= N) debugcrash(); +#endif new ((T*)_space + _size++)T(p...); } @@ -271,6 +287,9 @@ void static_vector::erase(size_t at, size_t n) template T * static_vector::insert(T * at, const T & t) { +#ifdef CAPACITYCHECK + if (_size >= N) debugcrash(); +#endif T * data = (T*)_space; T * dp = data + _size; new (dp)T(); diff --git a/include/opp/vector.h b/include/opp/vector.h index b8dc1bb..1238b9b 100644 --- a/include/opp/vector.h +++ b/include/opp/vector.h @@ -27,7 +27,8 @@ public: vector(const vector & v) : _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