[Snek] [PATCH 6/6] Preallocate string color constants on startup

Mikhail Gusarov dottedmag at dottedmag.net
Fri Feb 21 15:10:20 PST 2020


---
 ports/ev3/Makefile    |  2 +-
 ports/ev3/sensors.c   | 25 +++++++++++++------------
 ports/ev3/sensors.h   | 21 +++++++++++++++++++++
 ports/ev3/snek-main.c |  3 +++
 4 files changed, 38 insertions(+), 13 deletions(-)
 create mode 100644 ports/ev3/sensors.h

diff --git a/ports/ev3/Makefile b/ports/ev3/Makefile
index e8b8096..bdfce97 100644
--- a/ports/ev3/Makefile
+++ b/ports/ev3/Makefile
@@ -28,7 +28,7 @@ SNEK_LOCAL_SRC = \
 	sensors.c \
 	utils.c
 
-SNEK_LOCAL_INC = snek-ev3.h utils.h
+SNEK_LOCAL_INC = snek-ev3.h utils.h sensors.h
 SNEK_LOCAL_CFLAGS = -DSNEK_USE_GLIBC_2_4_MATH
 SNEK_LOCAL_BUILTINS = \
 	snek-ev3.builtin \
diff --git a/ports/ev3/sensors.c b/ports/ev3/sensors.c
index 0c3d297..5e04b8f 100644
--- a/ports/ev3/sensors.c
+++ b/ports/ev3/sensors.c
@@ -13,6 +13,7 @@
  */
 #include "snek.h"
 #include "utils.h"
+#include "sensors.h"
 #include <fcntl.h>
 #include <errno.h>
 #include <unistd.h>
@@ -60,6 +61,8 @@ static const char *COLORS[] = {
 
 #define COLORS_NUM (sizeof(COLORS) / sizeof(COLORS[0]))
 
+static int colors_stack_pos[COLORS_NUM];
+
 #define RGB_MAX 1020
 
 /* global data */
@@ -356,18 +359,16 @@ light_read_intensity(int fd)
 	return snek_float_to_poly(((float) val) / INTENSITY_MAX);
 }
 
-/* TODO: avoid allocations if possible */
-static snek_poly_t
-sm(const char *s)
+void
+snek_ev3_init_colors(void)
 {
-	size_t len = strlen(s);
-
-	char *new = snek_alloc(len + 1);
-	if (new == NULL)
-		return SNEK_NULL;
-
-	memcpy(new, s, len + 1);
-	return snek_string_to_poly(new);
+	for (unsigned long i = 1; i < COLORS_NUM; i++) {
+		size_t len = strlen(COLORS[i]);
+		char * p = snek_alloc(len + 1);
+		memcpy(p, COLORS[i], len + 1);
+		colors_stack_pos[i] = snek_stackp;
+		snek_stack_push_string(p);
+	}
 }
 
 static snek_poly_t
@@ -382,7 +383,7 @@ light_read_color_name(int fd)
 	if (val == 0)
 		return SNEK_NULL;
 
-	return sm(COLORS[val]);
+	return snek_stack[colors_stack_pos[val]];
 }
 
 static snek_poly_t
diff --git a/ports/ev3/sensors.h b/ports/ev3/sensors.h
new file mode 100644
index 0000000..2038a2f
--- /dev/null
+++ b/ports/ev3/sensors.h
@@ -0,0 +1,21 @@
+/*
+ * Copyright © 2020 Mikhail Gusarov <dottedmag at dottedmag.net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+#ifndef _SNEK_EV3_SENSORS_H_
+#define _SNEK_EV3_SENSORS_H_
+
+/* Initializes constants for EV3 light sensor */
+void
+snek_ev3_init_colors(void);
+
+#endif /* _SNEK_EV3_SENSORS_H_ */
diff --git a/ports/ev3/snek-main.c b/ports/ev3/snek-main.c
index b230323..0d984bd 100644
--- a/ports/ev3/snek-main.c
+++ b/ports/ev3/snek-main.c
@@ -14,6 +14,7 @@
 
 #include "snek.h"
 #include <getopt.h>
+#include "sensors.h"
 
 static FILE *snek_posix_input;
 
@@ -87,6 +88,8 @@ main(int argc, char **argv)
 		}
 	}
 
+	snek_ev3_init_colors();
+
 	snek_init();
 
 	if (file) {
-- 
2.24.0



More information about the Snek mailing list