From 8921ce29627b45d78d1870d550418fc3d13dcdbe Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Sat, 30 Apr 2022 05:28:06 -0700 Subject: grow memory for visits as needed --- comp.c | 2 ++ dartbot.c | 2 ++ match.c | 13 ++++++++++++- match.h | 3 ++- web/dartboat_wasm.c | 2 ++ 5 files changed, 20 insertions(+), 2 deletions(-) diff --git a/comp.c b/comp.c index 9427363..7b2c575 100644 --- a/comp.c +++ b/comp.c @@ -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))); diff --git a/dartbot.c b/dartbot.c index 03b4ec5..1759846 100644 --- a/dartbot.c +++ b/dartbot.c @@ -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]; diff --git a/match.c b/match.c index 08c0a3a..6bd1426 100644 --- a/match.c +++ b/match.c @@ -1,13 +1,15 @@ #include "match.h" #include +#include 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); +} diff --git a/match.h b/match.h index 9cdb261..ef75bac 100644 --- a/match.h +++ b/match.h @@ -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; -- cgit v1.2.3-70-g09d2