From 656be88ba3b0db383101449d7d2f09348601d7d7 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Wed, 25 May 2022 22:03:59 -0700 Subject: handle visits memory entirely from match file --- comp.c | 4 +--- dartboat.c | 4 +--- match.c | 13 +++++++++---- match.h | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/comp.c b/comp.c index 4f40609..012eea1 100644 --- a/comp.c +++ b/comp.c @@ -78,9 +78,7 @@ 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++; + struct visit *v = leg_visit(l); v->darts = calloc(3, sizeof(*v->darts)); v->ccoords = calloc(3, sizeof(*v->ccoords)); diff --git a/dartboat.c b/dartboat.c index 743c91b..fdaf6d1 100644 --- a/dartboat.c +++ b/dartboat.c @@ -33,9 +33,7 @@ void test_match(int start_points) void user_visit(struct leg *l, char *name) { - if (l->n_visits == l->size_visits) - leg_grow_visits(l); - struct visit *v = l->visits + l->n_visits++; + struct visit *v = leg_visit(l); char status[100]; int len = sprintf(status, " %s has %d remaining", name, l->rem); diff --git a/match.c b/match.c index 977bfe1..edcd405 100644 --- a/match.c +++ b/match.c @@ -29,7 +29,7 @@ void leg_free(struct leg *l) free(l); } -void leg_grow_visits(struct leg *l) +static void leg_grow_visits(struct leg *l) { size_t bytes = l->size_visits * sizeof(*l->visits); l->size_visits *= 2; @@ -37,6 +37,13 @@ void leg_grow_visits(struct leg *l) memset((char *)l->visits + bytes, 0, bytes); } +struct visit *leg_visit(struct leg *l) +{ + if (l->n_visits == l->size_visits) + leg_grow_visits(l); + return l->visits + l->n_visits++; +} + void leg_undo_visit(struct leg *l) { l->rem += l->visits[--l->n_visits].points; @@ -81,9 +88,7 @@ struct visit *leg_pts_visit(struct leg *l, int pts) if (l->undone_visits) leg_clear_first_undone(l); - if (l->n_visits == l->size_visits) - leg_grow_visits(l); - struct visit *v = l->visits + l->n_visits++; + struct visit *v = leg_visit(l); v->points = pts; l->rem -= pts; v->rem = l->rem; diff --git a/match.h b/match.h index 76a3db4..f86ba16 100644 --- a/match.h +++ b/match.h @@ -39,8 +39,8 @@ struct match { struct leg *leg_init(int pts); void leg_free(struct leg *l); -void leg_grow_visits(struct leg *l); +struct visit *leg_visit(struct leg *l); void leg_undo_visit(struct leg *l); struct visit *leg_redo_visit(struct leg *l); struct visit *leg_comp_visit(struct leg *l, bool redo_undone); -- cgit v1.2.3-70-g09d2