1
0
Fork 0
mirror of https://github.com/pnx/m16vm synced 2026-06-16 03:44:55 +02:00

move lib/src/vector.c to lib/vector.c

This commit is contained in:
Henrik Hautakoski 2018-12-18 13:25:56 +01:00
parent f385a4c47e
commit 8ef4c7edcd
No known key found for this signature in database
GPG key ID: 839F3A7EAFAEAFAA
2 changed files with 2 additions and 2 deletions

54
lib/vector.c Normal file
View 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;
}