1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Merge pull request #105278 from Ivorforce/reserve-smoke-test

Smoke test: Log an error if `reserve()` is called with fewer elements than `size()`
This commit is contained in:
Thaddeus Crews
2025-04-23 12:01:28 -05:00
7 changed files with 16 additions and 13 deletions

View File

@@ -260,7 +260,6 @@ int64_t AStar3D::get_point_capacity() const {
void AStar3D::reserve_space(int64_t p_num_nodes) {
ERR_FAIL_COND_MSG(p_num_nodes <= 0, vformat("New capacity must be greater than 0, new was: %d.", p_num_nodes));
ERR_FAIL_COND_MSG((uint32_t)p_num_nodes < points.get_capacity(), vformat("New capacity must be greater than current capacity: %d, new was: %d.", points.get_capacity(), p_num_nodes));
points.reserve(p_num_nodes);
}

View File

@@ -117,7 +117,8 @@ StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const c
template <int SHORT_BUFFER_SIZE>
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::reserve(int p_size) {
if (p_size < SHORT_BUFFER_SIZE || p_size < buffer.size() || !p_size) {
ERR_FAIL_COND_V_MSG(p_size < length(), *this, "reserve() called with a capacity smaller than the current size. This is likely a mistake.");
if (p_size <= SHORT_BUFFER_SIZE || p_size <= buffer.size()) {
return *this;
}

View File

@@ -414,12 +414,15 @@ public:
// Reserves space for a number of elements, useful to avoid many resizes and rehashes.
// If adding a known (possibly large) number of elements at once, must be larger than old capacity.
void reserve(uint32_t p_new_capacity) {
ERR_FAIL_COND_MSG(p_new_capacity < get_capacity(), "It is impossible to reserve less capacity than is currently available.");
ERR_FAIL_COND_MSG(p_new_capacity < size(), "reserve() called with a capacity smaller than the current size. This is likely a mistake.");
if (elements == nullptr) {
capacity = MAX(4u, p_new_capacity);
capacity = next_power_of_2(capacity) - 1;
return; // Unallocated yet.
}
if (p_new_capacity <= get_capacity()) {
return;
}
_resize_and_rehash(p_new_capacity);
}
@@ -665,9 +668,7 @@ public:
}
AHashMap(const HashMap<TKey, TValue> &p_other) {
if (p_other.size() > get_capacity()) {
reserve(p_other.size());
}
for (const KeyValue<TKey, TValue> &E : p_other) {
uint32_t hash = _hash(E.key);
_insert_element(E.key, E.value, hash);
@@ -686,9 +687,7 @@ public:
void operator=(const HashMap<TKey, TValue> &p_other) {
reset();
if (p_other.size() > get_capacity()) {
reserve(p_other.size());
}
for (const KeyValue<TKey, TValue> &E : p_other) {
uint32_t hash = _hash(E.key);
_insert_element(E.key, E.value, hash);
@@ -705,9 +704,7 @@ public:
}
AHashMap(std::initializer_list<KeyValue<TKey, TValue>> p_init) {
if (p_init.size() > get_capacity()) {
reserve(p_init.size());
}
for (const KeyValue<TKey, TValue> &E : p_init) {
insert(E.key, E.value);
}

View File

@@ -432,6 +432,7 @@ public:
// Reserves space for a number of elements, useful to avoid many resizes and rehashes.
// If adding a known (possibly large) number of elements at once, must be larger than old capacity.
void reserve(uint32_t p_new_capacity) {
ERR_FAIL_COND_MSG(p_new_capacity < size(), "reserve() called with a capacity smaller than the current size. This is likely a mistake.");
uint32_t new_index = capacity_index;
while (hash_table_size_primes[new_index] < p_new_capacity) {

View File

@@ -294,6 +294,7 @@ public:
// Reserves space for a number of elements, useful to avoid many resizes and rehashes.
// If adding a known (possibly large) number of elements at once, must be larger than old capacity.
void reserve(uint32_t p_new_capacity) {
ERR_FAIL_COND_MSG(p_new_capacity < size(), "reserve() called with a capacity smaller than the current size. This is likely a mistake.");
uint32_t new_index = capacity_index;
while (hash_table_size_primes[new_index] < p_new_capacity) {

View File

@@ -144,6 +144,7 @@ public:
_FORCE_INLINE_ bool is_empty() const { return count == 0; }
_FORCE_INLINE_ U get_capacity() const { return capacity; }
void reserve(U p_size) {
ERR_FAIL_COND_MSG(p_size < size(), "reserve() called with a capacity smaller than the current size. This is likely a mistake.");
if (p_size > capacity) {
if (tight) {
capacity = p_size;

View File

@@ -301,7 +301,10 @@ public:
* capacity.
**/
void reserve(uint32_t p_new_capacity) {
ERR_FAIL_COND(p_new_capacity < capacity);
ERR_FAIL_COND_MSG(p_new_capacity < get_num_elements(), "reserve() called with a capacity smaller than the current size. This is likely a mistake.");
if (p_new_capacity <= capacity) {
return;
}
_resize_and_rehash(p_new_capacity);
}