nightmaremail

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 8b68e39ca627bf0bbb3432beed0143e088af4307
parent a41404c584fb9c7f6a8d0980fac2dd6016ccf2cc
Author: Rolf Eike Beer <eike@sf-mail.de>
Date:   Fri,  8 May 2020 20:53:59 +0200

genalloc: invert code flow

Save the new length in a local variable, and only change the value in the struct
if allocation succeeds instead of changing it back on failure.

Diffstat:
Mgen_allocdefs.h | 15+++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/gen_allocdefs.h b/gen_allocdefs.h @@ -3,13 +3,16 @@ #define GEN_ALLOC_readyplus(ta,type,field,len,a,base,ta_rplus) \ static int ta_rplus ## _internal (ta *x, unsigned int n, unsigned int pluslen) \ -{ register unsigned int i; \ +{ \ if (x->field) { \ - i = x->a; n += pluslen; \ - if (n > i) { \ - x->a = base + n + (n >> 3); \ - if (alloc_re(&x->field,i * sizeof(type),x->a * sizeof(type))) return 1; \ - x->a = i; return 0; } \ + unsigned int nnum; \ + n += pluslen; \ + if (n <= x->a) \ + return 1; \ + nnum = base + n + (n >> 3); \ + if (!alloc_re(&x->field,x->a * sizeof(type),nnum * sizeof(type))) \ + return 0; \ + x->a = nnum; \ return 1; } \ x->len = 0; \ return !!(x->field = (type *) alloc((x->a = n) * sizeof(type))); } \