From f3c1a6b19c1ee1b5640348d846ae1ee2898cfb72 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 16 Jul 2015 12:10:38 -0700 Subject: [PATCH 1/3] Reduce lsDive stack usage to the minimum --- Marlin/cardreader.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/Marlin/cardreader.cpp b/Marlin/cardreader.cpp index b8a940f4b..7e4f3ffe3 100644 --- a/Marlin/cardreader.cpp +++ b/Marlin/cardreader.cpp @@ -56,22 +56,28 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m // If the entry is a directory and the action is LS_SerialPrint if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) { - // Allocate enough stack space for the full path to a folder, trailing slash, and nul - int len = strlen(prepend) + FILENAME_LENGTH + 1 + 1; - char path[len]; - // Get the short name for the item, which we know is a folder char lfilename[FILENAME_LENGTH]; createFilename(lfilename, p); + // Allocate enough stack space for the full path to a folder, trailing slash, and nul + boolean prepend_is_empty = (prepend[0] == '\0'); + int len = strlen(prepend) + (prepend_is_empty ? 1 : 0) + strlen(lfilename) + 1; + char path[len]; + // Append the FOLDERNAME12/ to the passed string. // It contains the full path to the "parent" argument. // We now have the full path to the item in this folder. - path[0] = '\0'; - if (prepend[0] == '\0') strcat(path, "/"); // a root slash if prepend is empty - strcat(path, prepend); - strcat(path, lfilename); - strcat(path, "/"); + if (prepend_is_empty) { + path[0] = '/'; // a root slash if prepend is empty + path[1] = '\0'; + } + else + path[0] = '\0'; + + strcat(path, prepend); // 1 character minimum + strcat(path, lfilename); // FILENAME_LENGTH-1 characters maximum + strcat(path, "/"); // 1 character // Serial.print(path); From 417706e5788e3cd307c99aafa3f112dfb5546031 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 16 Jul 2015 12:18:26 -0700 Subject: [PATCH 2/3] Include space for the terminating nul in lsDive --- Marlin/cardreader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/cardreader.cpp b/Marlin/cardreader.cpp index 7e4f3ffe3..8478f0077 100644 --- a/Marlin/cardreader.cpp +++ b/Marlin/cardreader.cpp @@ -62,7 +62,7 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m // Allocate enough stack space for the full path to a folder, trailing slash, and nul boolean prepend_is_empty = (prepend[0] == '\0'); - int len = strlen(prepend) + (prepend_is_empty ? 1 : 0) + strlen(lfilename) + 1; + int len = strlen(prepend) + (prepend_is_empty ? 1 : 0) + strlen(lfilename) + 1 + 1; char path[len]; // Append the FOLDERNAME12/ to the passed string. From 944090c19c26499b001e0d0ab3cc58c5d767bf83 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 16 Jul 2015 12:42:57 -0700 Subject: [PATCH 3/3] Optimize lsDive logic --- Marlin/cardreader.cpp | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Marlin/cardreader.cpp b/Marlin/cardreader.cpp index 8478f0077..12d2a5da3 100644 --- a/Marlin/cardreader.cpp +++ b/Marlin/cardreader.cpp @@ -62,20 +62,13 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m // Allocate enough stack space for the full path to a folder, trailing slash, and nul boolean prepend_is_empty = (prepend[0] == '\0'); - int len = strlen(prepend) + (prepend_is_empty ? 1 : 0) + strlen(lfilename) + 1 + 1; + int len = (prepend_is_empty ? 1 : strlen(prepend)) + strlen(lfilename) + 1 + 1; char path[len]; // Append the FOLDERNAME12/ to the passed string. // It contains the full path to the "parent" argument. // We now have the full path to the item in this folder. - if (prepend_is_empty) { - path[0] = '/'; // a root slash if prepend is empty - path[1] = '\0'; - } - else - path[0] = '\0'; - - strcat(path, prepend); // 1 character minimum + strcpy(path, prepend_is_empty ? "/" : prepend); // root slash if prepend is empty strcat(path, lfilename); // FILENAME_LENGTH-1 characters maximum strcat(path, "/"); // 1 character