2017-10-09 22:00:02 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "vec.h"
|
|
|
|
|
|
|
|
void vec_add1(struct vec_basestruct *ctl,size_t sz)
|
|
|
|
{
|
|
|
|
if (!ctl->alen) {
|
|
|
|
ctl->alen = 8;
|
|
|
|
if (SIZE_MAX / 8 < sz)
|
|
|
|
ctl->alen = 1;
|
|
|
|
ctl->buf = malloc(ctl->alen * sz);
|
|
|
|
if (!ctl->buf)
|
|
|
|
abort();
|
|
|
|
} else if (ctl->len >= ctl->alen) {
|
|
|
|
ctl->alen *= 2;
|
|
|
|
if (SIZE_MAX / ctl->alen < sz)
|
|
|
|
abort();
|
|
|
|
ctl->buf = realloc(ctl->buf,ctl->alen * sz);
|
|
|
|
if (!ctl->buf)
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
++ctl->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vec_addn(struct vec_basestruct *ctl,size_t sz,size_t n)
|
|
|
|
{
|
2017-10-10 05:14:45 +02:00
|
|
|
if (!ctl->alen) {
|
|
|
|
if (SIZE_MAX / 8 >= sz)
|
|
|
|
ctl->alen = 8;
|
|
|
|
else
|
|
|
|
ctl->alen = 1;
|
|
|
|
}
|
2017-10-09 22:00:02 +02:00
|
|
|
size_t nlen = ctl->alen;
|
|
|
|
ctl->len += n;
|
|
|
|
while (ctl->len > nlen)
|
|
|
|
nlen *= 2;
|
|
|
|
if (nlen > ctl->alen) {
|
|
|
|
ctl->alen = nlen;
|
|
|
|
if (SIZE_MAX / nlen < sz)
|
|
|
|
abort();
|
|
|
|
ctl->buf = realloc(ctl->buf,nlen * sz);
|
|
|
|
if (!ctl->buf)
|
|
|
|
abort();
|
|
|
|
} else if (!ctl->buf) {
|
|
|
|
ctl->buf = malloc(ctl->alen * sz);
|
|
|
|
if (!ctl->buf)
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|