diff --git a/lib/include/vector.h b/lib/include/vector.h new file mode 100644 index 0000000..cf2d2b9 --- /dev/null +++ b/lib/include/vector.h @@ -0,0 +1,41 @@ +/* vector.h + * + * Copyright (C) 2018 Henrik Hautakoski + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +#ifndef VECTOR_H +#define VECTOR_H + +#include +#include + +struct vector { + size_t blk_sz; + size_t alloc; + size_t size; + void *base; +}; + +#define VECTOR_INIT(block_size) { block_size, 0, 0, NULL } + +void vector_init(struct vector* v, size_t block_size); + +void vector_destory(struct vector* v); + +void vector_append(struct vector* v, const void *ptr, size_t n); + +#endif /* VECTOR_H */ diff --git a/lib/src/vector.c b/lib/src/vector.c new file mode 100644 index 0000000..b49e020 --- /dev/null +++ b/lib/src/vector.c @@ -0,0 +1,54 @@ +/* vector.c + * + * Copyright (C) 2018 Henrik Hautakoski + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +#include +#include +#include + +static void ensure_size(struct vector* v, size_t n) { + + size_t old_alloc = v->alloc; + while(v->alloc < v->size + n) + v->alloc += v->blk_sz; + + if (old_alloc != v->alloc) + v->base = realloc(v->base, v->alloc); +} + +void vector_init(struct vector* v, size_t block_size) { + + v->blk_sz = block_size > 0 ? block_size : 64; + v->alloc = 0; + v->size = 0; + v->base = NULL; +} + +void vector_destory(struct vector *v) { + + if (v->base) + free(v->base); +} + +void vector_append(struct vector *v, const void *ptr, size_t n) { + + ensure_size(v, n); + + memcpy(v->base + v->size, ptr, n); + v->size += n; +}