]> de.git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
fix vector resize to not always resize and actually use the reallocated place...
authorWolfgang (Blub) Bumiller <blub@speed.at>
Wed, 27 Jun 2012 12:49:36 +0000 (14:49 +0200)
committerWolfgang (Blub) Bumiller <blub@speed.at>
Wed, 27 Jun 2012 12:49:36 +0000 (14:49 +0200)
exec.c

diff --git a/exec.c b/exec.c
index dcc87188ac9cd08115d2a3cd97dc328a1e2fb662..bb4b59cb4d04e77b8d780bd3630df90d6c2c9e26 100644 (file)
--- a/exec.c
+++ b/exec.c
@@ -32,14 +32,23 @@ bool GMQCC_WARN Tself##_##mem##_append(Tself *s, Twhat *p, size_t c) \
 bool GMQCC_WARN Tself##_##mem##_resize(Tself *s, size_t c)       \
 {                                                                \
     Twhat *reall;                                                \
-    reall = (Twhat*)mem_a(sizeof(Twhat) * c);                    \
-    if (c > s->mem##_count) {                                    \
+    if (c > s->mem##_alloc) {                                    \
+        reall = (Twhat*)mem_a(sizeof(Twhat) * c);                \
+        if (!reall) { return false; }                            \
         memcpy(reall, s->mem, sizeof(Twhat) * s->mem##_count);   \
-    } else {                                                     \
-        memcpy(reall, s->mem, sizeof(Twhat) * c);                \
+        s->mem##_alloc = c;                                      \
+        mem_d(s->mem);                                           \
+        s->mem = reall;                                          \
+        return true;                                             \
     }                                                            \
     s->mem##_count = c;                                          \
-    s->mem##_alloc = c;                                          \
+    if (c < (s->mem##_alloc / 2)) {                              \
+        reall = (Twhat*)mem_a(sizeof(Twhat) * c);                \
+        if (!reall) { return false; }                            \
+        memcpy(reall, s->mem, sizeof(Twhat) * c);                \
+        mem_d(s->mem);                                           \
+        s->mem = reall;                                          \
+    }                                                            \
     return true;                                                 \
 }