[SLOF] [PATCH 2/2] Improve stack usage with libnvram get_partition function

Thomas Huth thuth at redhat.com
Tue Dec 1 20:06:12 AEDT 2015


The Forth-to-C wrapper for get-named-nvram-partition also
uses the STRING_INIT macro. This causes heavy stack usage
in the engine() function due to the static array in that
macro. So let's rework the wrapper to do the string convertion
in a separate function instead.
Now that all users of the STRING_INIT and STRING_FROM_STACK
macros are gone, the macros can be removed, too.

Signed-off-by: Thomas Huth <thuth at redhat.com>
---
 lib/libnvram/libnvram.code | 18 ++----------------
 lib/libnvram/nvram.c       | 11 +++++++++++
 lib/libnvram/nvram.h       |  1 +
 3 files changed, 14 insertions(+), 16 deletions(-)

diff --git a/lib/libnvram/libnvram.code b/lib/libnvram/libnvram.code
index dd2346e..8481f57 100644
--- a/lib/libnvram/libnvram.code
+++ b/lib/libnvram/libnvram.code
@@ -11,17 +11,6 @@
  *****************************************************************************/
 #include <nvram.h>
 
-#define STRING_INIT(str)	\
-	char str[255];		\
-	char * str##_address;	\
-	int  str##_length;
-
-#define STRING_FROM_STACK(str)				\
-	str##_length = TOS.u; POP;				\
-	str##_address = TOS.a; POP;			\
-	memcpy(str, str##_address, str##_length);	\
-	memset(str + str##_length, 0, 255 - str##_length);
-
 PRIM(nvram_X2d_c_X40)
 	unsigned int offset = TOS.u;
 	TOS.u=nvram_read_byte(offset);
@@ -80,21 +69,18 @@ MIRP
 
 /* get-named-nvram-partition ( name.addr name.len -- addr len FAILED? ) */
 PRIM(get_X2d_named_X2d_nvram_X2d_partition)
-	STRING_INIT(name)
 	partition_t partition;
+	int namelen = TOS.n; POP;
 
-	STRING_FROM_STACK(name)
-	partition = get_partition(-1, name);
+	partition = get_partition_fs(TOS.a, namelen);
 
 	if(partition.len && partition.len != -1) {
-		PUSH;
 		TOS.u = partition.addr;
 		PUSH;
 		TOS.u = partition.len;
 		PUSH;
 		TOS.u = 0; // FALSE
 	} else {
-		PUSH;
 		TOS.u = -1; // TRUE
 	}
 MIRP
diff --git a/lib/libnvram/nvram.c b/lib/libnvram/nvram.c
index 1a4f91a..473814e 100644
--- a/lib/libnvram/nvram.c
+++ b/lib/libnvram/nvram.c
@@ -358,6 +358,17 @@ partition_t get_partition(unsigned int type, char *name)
 	return ret;
 }
 
+/* Get partition specified by a Forth string */
+partition_t get_partition_fs(char *name, int namelen)
+{
+	char buf[namelen + 1];
+
+	memcpy(buf, name, namelen);
+	buf[namelen] = 0;
+
+	return get_partition(-1, buf);
+}
+
 void erase_nvram(int offset, int len)
 {
 	int i;
diff --git a/lib/libnvram/nvram.h b/lib/libnvram/nvram.h
index 3fdba98..73fe444 100644
--- a/lib/libnvram/nvram.h
+++ b/lib/libnvram/nvram.h
@@ -51,6 +51,7 @@ char *get_nvram_buffer(int len);
 void free_nvram_buffer(char *buffer);
 int nvramlog_printf(const char* fmt, ...);
 partition_t get_partition(unsigned int type, char *name);
+partition_t get_partition_fs(char *name, int namelen);
 void erase_nvram(int offset, int len);
 int wipe_partition(partition_t partition, int header_only);
 partition_t new_nvram_partition(int type, char *name, int len);
-- 
1.8.3.1



More information about the SLOF mailing list