mirror of
https://github.com/cathugger/mkp224o.git
synced 2025-01-09 11:07:19 -03:00
some vec tweaks
This commit is contained in:
parent
586d977fa0
commit
f862c7897e
2 changed files with 13 additions and 11 deletions
10
vec.c
10
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)
|
void vec_addn(struct vec_basestruct *ctl,size_t sz,size_t n)
|
||||||
{
|
{
|
||||||
if (!ctl->alen)
|
if (!ctl->alen) {
|
||||||
ctl->alen = 8;
|
if (SIZE_MAX / 8 >= sz)
|
||||||
|
ctl->alen = 8;
|
||||||
|
else
|
||||||
|
ctl->alen = 1;
|
||||||
|
}
|
||||||
size_t nlen = ctl->alen;
|
size_t nlen = ctl->alen;
|
||||||
ctl->len += n;
|
ctl->len += n;
|
||||||
while (ctl->len > nlen)
|
while (ctl->len > nlen)
|
||||||
|
@ -39,8 +43,6 @@ void vec_addn(struct vec_basestruct *ctl,size_t sz,size_t n)
|
||||||
if (!ctl->buf)
|
if (!ctl->buf)
|
||||||
abort();
|
abort();
|
||||||
} else if (!ctl->buf) {
|
} else if (!ctl->buf) {
|
||||||
if (SIZE_MAX / ctl->alen < sz)
|
|
||||||
abort();
|
|
||||||
ctl->buf = malloc(ctl->alen * sz);
|
ctl->buf = malloc(ctl->alen * sz);
|
||||||
if (!ctl->buf)
|
if (!ctl->buf)
|
||||||
abort();
|
abort();
|
||||||
|
|
14
vec.h
14
vec.h
|
@ -1,7 +1,7 @@
|
||||||
#define VEC_STRUCT(typename,inttype) \
|
#define VEC_STRUCT(typename,inttype) \
|
||||||
struct typename { \
|
struct typename { \
|
||||||
inttype *buf; \
|
inttype *buf; \
|
||||||
size_t len, alen; \
|
size_t len,alen; \
|
||||||
}
|
}
|
||||||
VEC_STRUCT(vec_basestruct,void) ;
|
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) { \
|
#define VEC_REMOVEN(ctl,n,m) { \
|
||||||
(ctl).len -= m; \
|
(ctl).len -= m; \
|
||||||
memmove( \
|
memmove( \
|
||||||
&(ctl).buf[(n)], \
|
&(ctl).buf[n], \
|
||||||
&(ctl).buf[(n + m)], \
|
&(ctl).buf[(n) + (m)], \
|
||||||
((ctl).len - (n)) * VEC_ELSIZE(ctl)); \
|
((ctl).len - (n)) * VEC_ELSIZE(ctl)); \
|
||||||
}
|
}
|
||||||
#define VEC_REMOVE(ctl,n) VEC_REMOVEN(ctl,n,1)
|
#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) { \
|
#define VEC_INSERT1(ctl,n) { \
|
||||||
VEC_ADD1(ctl); \
|
VEC_ADD1(ctl); \
|
||||||
memmove( \
|
memmove( \
|
||||||
&(ctl).buf[(n + 1)], \
|
&(ctl).buf[(n) + 1], \
|
||||||
&(ctl).buf[(n)], \
|
&(ctl).buf[n], \
|
||||||
((ctl).len - (n) - 1) * VEC_ELSIZE(ctl)); \
|
((ctl).len - (n) - 1) * VEC_ELSIZE(ctl)); \
|
||||||
}
|
}
|
||||||
#define VEC_INSERT(ctl,n,val) { \
|
#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) { \
|
#define VEC_INSERTN(ctl,n,m) { \
|
||||||
VEC_ADDN(ctl,m); \
|
VEC_ADDN(ctl,m); \
|
||||||
memmove( \
|
memmove( \
|
||||||
&(ctl).buf[(n + m)], \
|
&(ctl).buf[(n) + (m)], \
|
||||||
&(ctl).buf[(n)], \
|
&(ctl).buf[n], \
|
||||||
((ctl).len - (n) - (m)) * VEC_ELSIZE(ctl)); \
|
((ctl).len - (n) - (m)) * VEC_ELSIZE(ctl)); \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue