diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-30 05:28:06 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-30 05:28:06 -0700 |
commit | 8921ce29627b45d78d1870d550418fc3d13dcdbe (patch) | |
tree | 1b5b37a317c93d55247ac3da719a8dcd8eaf072b | |
parent | fbdb393ae1e2ea5d368e4dda1577a10715e26a75 (diff) | |
download | dartboat-8921ce29627b45d78d1870d550418fc3d13dcdbe.tar.gz dartboat-8921ce29627b45d78d1870d550418fc3d13dcdbe.tar.xz |
grow memory for visits as needed
-rw-r--r-- | comp.c | 2 | ||||
-rw-r--r-- | dartbot.c | 2 | ||||
-rw-r--r-- | match.c | 13 | ||||
-rw-r--r-- | match.h | 3 | ||||
-rw-r--r-- | web/dartboat_wasm.c | 2 |
5 files changed, 20 insertions, 2 deletions
@@ -82,6 +82,8 @@ struct segment next_dart(int rem, int darts_in_hand) void comp_visit(struct leg *l) { + if (l->n_visits == l->size_visits) + leg_grow_visits(l); struct visit *v = l->visits + l->n_visits++; v->darts = calloc(3, sizeof(*(v->darts))); v->ccoords = calloc(3, sizeof(*(v->ccoords))); @@ -34,6 +34,8 @@ void test_match(int start_points) void user_visit(struct leg *l) { + if (l->n_visits == l->size_visits) + leg_grow_visits(l); struct visit *v = l->visits + l->n_visits++; char status[100]; @@ -1,13 +1,15 @@ #include "match.h" #include <stdlib.h> +#include <string.h> struct leg *leg_init(int points, char *name) { struct leg *l = calloc(1, sizeof(*l)); l->name = name; l->start = l->rem = points; - l->visits = calloc(1000, sizeof(*(l->visits))); // FIXME + l->size_visits = 16; + l->visits = calloc(l->size_visits, sizeof(*(l->visits))); return l; } @@ -23,3 +25,12 @@ void leg_free(struct leg *l) free(l->visits); free(l); } + +void leg_grow_visits(struct leg *l) +{ + + size_t bytes = l->size_visits * sizeof(*(l->visits)); + l->size_visits *= 2; + l->visits = realloc(l->visits, 2 * bytes); + memset((char *)l->visits + bytes, 0, bytes); +} @@ -15,11 +15,12 @@ struct leg { char *name; int start; int rem; - int n_visits; + int n_visits, size_visits; struct visit *visits; }; struct leg *leg_init(int points, char *name); void leg_free(struct leg *l); +void leg_grow_visits(struct leg *l); #endif diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c index e7f0415..691be77 100644 --- a/web/dartboat_wasm.c +++ b/web/dartboat_wasm.c @@ -321,6 +321,8 @@ EMSCRIPTEN_KEEPALIVE void user_visit(int points) } struct leg *l = state->active_leg; + if (l->n_visits == l->size_visits) + leg_grow_visits(l); struct visit *v = l->visits + l->n_visits++; v->points = points; l->rem -= points; |