From 3e1ed8a5f7813b36105cb09534c9bc9549d642c9 Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Wed, 6 Mar 2019 08:31:06 +0000 Subject: [PATCH] Don't reserve more space than we need --- GL/clip.c | 5 +++++ containers/aligned_vector.c | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/GL/clip.c b/GL/clip.c index a3b3d76..9406c89 100644 --- a/GL/clip.c +++ b/GL/clip.c @@ -1,5 +1,6 @@ #include #include +#include #ifdef _arch_dreamcast #include @@ -165,6 +166,8 @@ void clipTriangleStrip2(AlignedVector* vertices, uint32_t offset, uint8_t fladeS uint32_t i = 0; /* Skip the header */ + + assert(offset < vertices->size); ClipVertex* header = (ClipVertex*) aligned_vector_at(vertices, offset); ClipVertex* vertex = header + 1; @@ -174,6 +177,8 @@ void clipTriangleStrip2(AlignedVector* vertices, uint32_t offset, uint8_t fladeS /* Start at 3 due to the header */ for(i = 3; i < count; ++i, ++triangle) { + assert(offset + i < vertices->size); + vertex = aligned_vector_at(vertices, offset + i); uint8_t even = (triangle % 2) == 0; diff --git a/containers/aligned_vector.c b/containers/aligned_vector.c index 67f2804..011d0df 100644 --- a/containers/aligned_vector.c +++ b/containers/aligned_vector.c @@ -1,6 +1,7 @@ #include #include #include +#include #if defined(__APPLE__) || defined(__WIN32__) /* Linux + Kos define this, OSX does not, so just use malloc there */ @@ -64,9 +65,7 @@ void* aligned_vector_resize(AlignedVector* vector, const unsigned int element_co } if(vector->capacity < element_count) { - /* Reserve more than we need so that a subsequent push_back doesn't trigger yet another - * resize */ - aligned_vector_reserve(vector, (int) ceil(((float)element_count) * 1.5f)); + aligned_vector_reserve(vector, element_count); } vector->size = element_count; @@ -79,6 +78,7 @@ void* aligned_vector_resize(AlignedVector* vector, const unsigned int element_co } void* aligned_vector_at(const AlignedVector* vector, const unsigned int index) { + assert(index < vector->size); return &vector->data[index * vector->element_size]; }