From 9edcd68e7ddcdc4deca8c07b5b1c3c59cef13255 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Wed, 25 May 2022 00:57:32 -0700 Subject: pregenerate segment names to avoid constant mallocs --- board.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'board.c') 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) -- cgit v1.2.3-70-g09d2