summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-25 00:57:32 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-25 00:58:15 -0700
commit9edcd68e7ddcdc4deca8c07b5b1c3c59cef13255 (patch)
tree1a7a0069e8d51709238d991dc43f0323ec32bb9c
parent9a02c0367351c311734f42ab2afe2b377ca12908 (diff)
downloaddartboat-9edcd68e7ddcdc4deca8c07b5b1c3c59cef13255.tar.gz
dartboat-9edcd68e7ddcdc4deca8c07b5b1c3c59cef13255.tar.xz
pregenerate segment names to avoid constant mallocs
-rw-r--r--board.c33
-rw-r--r--board.h12
-rw-r--r--curses.c8
-rw-r--r--dartboat.c8
-rw-r--r--web/Makefile2
-rw-r--r--web/web_control.c5
-rw-r--r--web/web_scoreboard.c7
7 files changed, 38 insertions, 37 deletions
diff --git a/board.c b/board.c
index 1a543ee..f61bd42 100644
--- a/board.c
+++ b/board.c
@@ -14,10 +14,16 @@ int SECTORS[] = { 20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7,
int SECTOR_INDS[NUM_SECTORS];
double SECTOR_ANGLES[NUM_SECTORS];
-char *RING_NAMES[] = {
- FOREACH_RING(GEN_RING_STRING)
+static const char *SECTOR_NAMES[] = {
+ FOREACH_SECTOR(GEN_SECTOR_STR)
+ FOREACH_SECTOR(GEN_SECTOR_STR_D)
+ FOREACH_SECTOR(GEN_SECTOR_STR_T)
+};
+static const char *RING_NAMES[] = {
+ FOREACH_RING(GEN_RING_STR)
"OUT"
};
+
double OUTER_DISTS[] = {
FOREACH_RING(GEN_RING_OUTER_DIST)
};
@@ -70,22 +76,17 @@ struct segment get_segment(struct pcoords c)
.ring = get_ring(c.r) };
}
-char *segment_name(struct segment seg)
+const char *segment_name(struct segment seg)
{
- char *str = malloc(SEGMENT_MAX_LEN);
-
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wstringop-truncation"
if (seg.ring == R_BULL || seg.ring == R_25 || seg.ring == R_OUT)
- strncpy(str, RING_NAMES[seg.ring], SEGMENT_MAX_LEN);
- else
- snprintf(str, SEGMENT_MAX_LEN,
- (seg.ring == R_TREBLE ? "T%d" :
- (seg.ring == R_DOUBLE ? "D%d" : "%d")),
- seg.sector);
-#pragma GCC diagnostic pop
-
- return str;
+ return RING_NAMES[seg.ring];
+
+ int ind = seg.sector - 1;
+ if (seg.ring == R_DOUBLE)
+ ind += 20;
+ else if (seg.ring == R_TREBLE)
+ ind += 40;
+ return SECTOR_NAMES[ind];
}
int segment_points(struct segment seg)
diff --git a/board.h b/board.h
index e5ffb24..4348070 100644
--- a/board.h
+++ b/board.h
@@ -14,6 +14,14 @@
#define TREBLE_INSIDE_WIDTH 8.0
#define SEGMENT_MAX_LEN 5
+
+#define FOREACH_SECTOR(M) \
+ M(1) M(2) M(3) M(4) M(5) M(6) M(7) M(8) M(9) M(10) \
+ M(11) M(12) M(13) M(14) M(15) M(16) M(17) M(18) M(19) M(20)
+#define GEN_SECTOR_STR(X) #X,
+#define GEN_SECTOR_STR_D(X) "D" #X,
+#define GEN_SECTOR_STR_T(X) "T" #X,
+
// distance from centre to apex of outer wire
// must be ordered outwards from centre
#define FOREACH_RING(M) \
@@ -26,7 +34,7 @@
WIRE_WIDTH/2) \
M(DOUBLE, DOUBLE_OUTER_EDGE - WIRE_WIDTH/2)
#define GEN_RING_ENUM(X, D) R_ ## X,
-#define GEN_RING_STRING(X, D) #X,
+#define GEN_RING_STR(X, D) #X,
#define GEN_RING_OUTER_DIST(X, D) D,
enum ring {
FOREACH_RING(GEN_RING_ENUM)
@@ -52,7 +60,7 @@ struct pcoords {
};
struct segment get_segment(struct pcoords c);
-char *segment_name(struct segment seg);
+const char *segment_name(struct segment seg);
int segment_points(struct segment seg);
bool segment_is_double(struct segment seg);
struct pcoords segment_centre(struct segment seg);
diff --git a/curses.c b/curses.c
index 174a6b9..5e5d934 100644
--- a/curses.c
+++ b/curses.c
@@ -131,12 +131,10 @@ void curses_draw(struct leg *l1, struct leg *l2)
buflen += sprintf(buf + buflen, "%3d", v->points);
flushbuf(buf, &buflen, points_colour(v->points));
- for (int j = 0; j < v->n_darts; ++j) {
- char *n = segment_name(v->darts[j]);
+ for (int j = 0; j < v->n_darts; ++j)
buflen += sprintf(buf + buflen,
- j == 0 ? " %4s" :" %4s", n);
- free(n);
- }
+ j == 0 ? " %4s" : " %4s",
+ segment_name(v->darts[j]));
flushbuf(buf, &buflen, COLOR_PAIR(C_DARTS));
}
diff --git a/dartboat.c b/dartboat.c
index 7061b9a..58cddfc 100644
--- a/dartboat.c
+++ b/dartboat.c
@@ -21,11 +21,9 @@ void test_match(int start_points)
struct visit *v = l->visits + i;
printf("(%2d) %3d %3d", i+1, v->points, v->rem);
- for (int j = 0; j < v->n_darts; ++j) {
- char *n = segment_name(v->darts[j]);
- printf(j == 0 ? " %4s" :" %4s", n);
- free(n);
- }
+ for (int j = 0; j < v->n_darts; ++j)
+ printf(j == 0 ? " %4s" : " %4s",
+ segment_name(v->darts[j]));
printf("\n");
}
diff --git a/web/Makefile b/web/Makefile
index 07b7d78..aa2666a 100644
--- a/web/Makefile
+++ b/web/Makefile
@@ -1,6 +1,6 @@
CC := emcc
-CFLAGS += -O3 -Wall -Wextra -Wno-unknown-warning-option -I../ \
+CFLAGS += -O3 -Wall -Wextra -I../ \
-sNO_EXIT_RUNTIME=1 \
-sENVIRONMENT=web \
-sEXPORTED_FUNCTIONS=_main,_malloc,_free \
diff --git a/web/web_control.c b/web/web_control.c
index a7a53cf..07d52b1 100644
--- a/web/web_control.c
+++ b/web/web_control.c
@@ -81,9 +81,8 @@ static void schedule_boat_visit_draws(struct leg *l, struct visit *v,
for (int i = 0; i < v->n_darts; ++i) {
pts += segment_points(v->darts[i]);
- char *seg_name = segment_name(v->darts[i]);
- len_str += sprintf(str + len_str, i ? "-%s" : "%s", seg_name);
- free(seg_name);
+ len_str += sprintf(str + len_str, i ? "-%s" : "%s",
+ segment_name(v->darts[i]));
struct ccoords c = v->ccoords[i];
diff --git a/web/web_scoreboard.c b/web/web_scoreboard.c
index 2e998cc..ed25b33 100644
--- a/web/web_scoreboard.c
+++ b/web/web_scoreboard.c
@@ -262,12 +262,9 @@ static const char *points_class(int pts)
static void buf_darts(char *buf, size_t size, struct visit *v)
{
char *ptr = buf;
- for (int j = 0; j < v->n_darts; ++j) {
- char *n = segment_name(v->darts[j]);
+ for (int j = 0; j < v->n_darts; ++j)
ptr += snprintf(ptr, size - (ptr - buf),
- j == 0 ? "%s" : "-%s", n);
- free(n);
- }
+ j == 0 ? "%s" : "-%s", segment_name(v->darts[j]));
}
void draw_visits_dense()