some vec tweaks

This commit is contained in:
cathugger 2017-10-10 03:14:45 +00:00
parent 586d977fa0
commit f862c7897e
2 changed files with 13 additions and 11 deletions

10
vec.c
View File

@ -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();

14
vec.h
View File

@ -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)); \
}