mirror of
https://github.com/pnx/m16vm
synced 2026-07-03 11:43:39 +02:00
lib: adding vector module
This commit is contained in:
parent
adbe0006f5
commit
a305c34b39
2 changed files with 95 additions and 0 deletions
41
lib/include/vector.h
Normal file
41
lib/include/vector.h
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
/* vector.h
|
||||||
|
*
|
||||||
|
* Copyright (C) 2018 Henrik Hautakoski <henrik@fiktivkod.org>
|
||||||
|
*
|
||||||
|
* 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 <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
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 */
|
||||||
54
lib/src/vector.c
Normal file
54
lib/src/vector.c
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
/* vector.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2018 Henrik Hautakoski <henrik@fiktivkod.org>
|
||||||
|
*
|
||||||
|
* 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 <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <vector.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue