From 1a5c55eb89c22e8822ec057a3731a6d753f13859 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Sat, 30 Apr 2022 00:23:31 +0100 Subject: Centralise engine access, add Portal FOV changer - A bunch of stuff is now defined in one header, engineapi.h - engineapi.c is responsible for setting up any interfaces/stuff that's used in more than one place - mkgamedata is pretty much rewritten and now supports nested conditionals - gamedata variables no longer have the gamedata_ prefix because it was just annoyingly long all the time - vcall macros are somewhat revamped and support dynamic (gamedata) indices - Portal 1 FOV can be set anywhere from 75-120 using fov_desired - tested in both the main versions currently used by runners - A few typos were also fixed ("intput," "writeable," "indexes") --- src/build/vec.h | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/build/vec.h (limited to 'src/build/vec.h') diff --git a/src/build/vec.h b/src/build/vec.h new file mode 100644 index 0000000..50b0a3b --- /dev/null +++ b/src/build/vec.h @@ -0,0 +1,96 @@ +/* This file is dedicated to the public domain. */ + +#ifndef INC_VEC_H +#define INC_VEC_H + +#include +#include +#include + +#include "../intdefs.h" + +struct _vec { + uint sz; + uint max; + void *data; +}; + +/* + * A dynamic array with push, pop and concatenate operations. + * + * Usage: struct VEC(my_type) myvec = {0}; + * Or: struct myvec VEC(my_type); + * Or: typedef struct VEC(my_type) myvec; + */ +#define VEC(type) { \ + uint sz; \ + uint max; \ + type *data; \ +} + +#if defined(__GNUC__) || defined(__clang__) +__attribute__((unused)) // heck off gcc +#endif +static bool _vec_ensure(struct _vec *v, uint tsize, uint newmax) { + // FIXME: potential overflow at least on 32-bit hosts (if any!?). + // should use reallocarray or something but didn't feel like porting right + // now. consider doing later. + void *new = realloc(v->data, tsize * newmax); + if (new) { v->data = new; v->max = newmax; } + return !!new; +} + +#if defined(__GNUC__) || defined(__clang__) +__attribute__((unused)) // heck off gcc 2 +#endif +static bool _vec_make_room(struct _vec *v, uint tsize, uint addcnt) { + // this overflow check is probably unnecessary, but just in case + u64 chk = v->max + addcnt; + if (chk > 1u << 30) { errno = ENOMEM; return false; } + u32 x = chk; + if (x < 16) { + x = 16; + } + else { + // round up to next 2*n + --x; + x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; + x++; + } + return _vec_ensure(v, tsize, x); +} + +// internal: for reuse by vec0 +#define _vec_push(v, val, slack) ( \ + ((v)->sz + (slack) < (v)->max || \ + _vec_make_room((struct _vec *)(v), sizeof(val), 1)) && \ + ((v)->data[(v)->sz++ - slack] = (val), true) \ +) + +#define _vec_pushall(v, vals, n, slack) ( \ + ((v)->sz + (n) + (slack) <= (v)->max || \ + _vec_make_room((struct _vec *)(v), sizeof(*(vals)), (n))) && \ + (memcpy((v)->data + (v)->sz - (slack), (vals), (n) * sizeof(*(vals))), \ + (v)->sz += (n), true) \ +) + +/* + * Appends an item to the end of a vector. Gives true on success and false if + * memory allocation fails. + */ +#define vec_push(v, val) _vec_push(v, val, 0) + +/* + * Appends n items from an array to the end of a vector. Gives true on success + * and false if memory allocation fails. + */ +#define vec_pushall(v, vals, n) _vec_pushall(v, vals, n, 0) + +/* + * Removes an item from the end of a vector and gives that item. + */ +#define vec_pop(v) ((v)->data[--(v)->sz]) + +#endif + +// vi: sw=4 ts=4 noet tw=80 cc=80 -- cgit v1.2.3