From f862c7897e868180e9224e1e62d3f3abd8f02aac Mon Sep 17 00:00:00 2001 From: cathugger Date: Tue, 10 Oct 2017 03:14:45 +0000 Subject: [PATCH] some vec tweaks --- vec.c | 10 ++++++---- vec.h | 14 +++++++------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/vec.c b/vec.c index 634bed6..73f8446 100644 --- a/vec.c +++ b/vec.c @@ -25,8 +25,12 @@ void vec_add1(struct vec_basestruct *ctl,size_t sz) void vec_addn(struct vec_basestruct *ctl,size_t sz,size_t n) { - if (!ctl->alen) - ctl->alen = 8; + if (!ctl->alen) { + if (SIZE_MAX / 8 >= sz) + ctl->alen = 8; + else + ctl->alen = 1; + } size_t nlen = ctl->alen; ctl->len += n; while (ctl->len > nlen) @@ -39,8 +43,6 @@ void vec_addn(struct vec_basestruct *ctl,size_t sz,size_t n) if (!ctl->buf) abort(); } else if (!ctl->buf) { - if (SIZE_MAX / ctl->alen < sz) - abort(); ctl->buf = malloc(ctl->alen * sz); if (!ctl->buf) abort(); diff --git a/vec.h b/vec.h index 51cc710..0b0ee1a 100644 --- a/vec.h +++ b/vec.h @@ -1,7 +1,7 @@ #define VEC_STRUCT(typename,inttype) \ struct typename { \ inttype *buf; \ - size_t len, alen; \ + size_t len,alen; \ } VEC_STRUCT(vec_basestruct,void) ; @@ -24,8 +24,8 @@ void vec_addn(struct vec_basestruct *ctl,size_t sz,size_t n); #define VEC_REMOVEN(ctl,n,m) { \ (ctl).len -= m; \ memmove( \ - &(ctl).buf[(n)], \ - &(ctl).buf[(n + m)], \ + &(ctl).buf[n], \ + &(ctl).buf[(n) + (m)], \ ((ctl).len - (n)) * VEC_ELSIZE(ctl)); \ } #define VEC_REMOVE(ctl,n) VEC_REMOVEN(ctl,n,1) @@ -33,8 +33,8 @@ void vec_addn(struct vec_basestruct *ctl,size_t sz,size_t n); #define VEC_INSERT1(ctl,n) { \ VEC_ADD1(ctl); \ memmove( \ - &(ctl).buf[(n + 1)], \ - &(ctl).buf[(n)], \ + &(ctl).buf[(n) + 1], \ + &(ctl).buf[n], \ ((ctl).len - (n) - 1) * VEC_ELSIZE(ctl)); \ } #define VEC_INSERT(ctl,n,val) { \ @@ -45,8 +45,8 @@ void vec_addn(struct vec_basestruct *ctl,size_t sz,size_t n); #define VEC_INSERTN(ctl,n,m) { \ VEC_ADDN(ctl,m); \ memmove( \ - &(ctl).buf[(n + m)], \ - &(ctl).buf[(n)], \ + &(ctl).buf[(n) + (m)], \ + &(ctl).buf[n], \ ((ctl).len - (n) - (m)) * VEC_ELSIZE(ctl)); \ }