From: Wolfgang (Blub) Bumiller Date: Wed, 27 Jun 2012 12:49:36 +0000 (+0200) Subject: fix vector resize to not always resize and actually use the reallocated place... X-Git-Tag: 0.1-rc1~371^2~15 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=0dae2898e33bc8c5879ac10b69f7369b69b584e2;p=xonotic%2Fgmqcc.git fix vector resize to not always resize and actually use the reallocated place... --- diff --git a/exec.c b/exec.c index dcc8718..bb4b59c 100644 --- 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; \ }