cli: allow users to determine history buffer allocation method
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v3 2/3 cli: allow users to determine history buffer allocation method
This commit allows users to choose the appropriate memory allocation method between static allocated and dynamically calloc. The previous static-array way will not obviously contribute to the final binary size since it is uninitialized, and might have better performance than the dynamical one. Now we provide the users with both the two options.
---
This is v3 of patch series cli: allow users to disable history if unused at all. Please ignore the v2 version.
---
Changes v1 -> v3:
- Add more detailed information about the CMD_HISTORY_USE_CALLOC
option both in the Kconfig and the code.
- Update the comments on global history array and flash running
problems.
--- cmd/Kconfig | 11 +++++++++++ common/cli_readline.c | 36 +++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 11 deletions(-)
cmd/Kconfig +11 -0
@@ -189,6 +189,17 @@ config CMD_HISTORY Show the command-line history, i.e. a list of commands that are in the history buffer. +config CMD_HISTORY_USE_CALLOC + bool "dynamically allocate memory" + default y + depends on CMD_HISTORY + help + Saying Y to this will use calloc to get the space for history + storing. Otherwise the history buffer will be an uninitialized + static array directly, without the memory allocation, and it is + writable after relocation to RAM. If u-boot is running from ROM + all the time or unsure, say Y to this. + config CMD_LICENSE bool "license" select BUILD_BIN2C
common/cli_readline.c +25 -11
@@ -86,6 +86,9 @@ static int hist_add_idx;static int hist_cur =-1 ;static unsigned hist_num; +#ifndef CONFIG_CMD_HISTORY_USE_CALLOC +static char hist_data[HIST_MAX ][HIST_SIZE +1 ]; +#endif static char *hist_list[HIST_MAX ];#define add_idx_minus_one () ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
@@ -100,20 +103,26 @@ static void getcmd_putchars(int count, int ch)static int hist_init (void ) { -unsigned char *hist;int i; - hist_max =0 ; - hist_add_idx =0 ; - hist_cur =-1 ; - hist_num =0 ; - - hist =calloc (HIST_MAX ,HIST_SIZE +1 ); +#ifndef CONFIG_CMD_HISTORY_USE_CALLOC +for (i =0 ; i <HIST_MAX ; i++) { + hist_list[i] = hist_data[i]; + hist_list[i][0 ] ='\0' ; + } +#else +unsigned char *hist =calloc (HIST_MAX ,HIST_SIZE +1 );if (!hist)panic ("%s: calloc: out of memory!\n" , __func__);for (i =0 ; i <HIST_MAX ; i++) hist_list[i] = hist + (i * (HIST_SIZE +1 )); +#endif + + hist_max =0 ; + hist_add_idx =0 ; + hist_cur =-1 ; + hist_num =0 ;return 0 ; }
@@ -643,10 +652,15 @@ int cli_readline_into_buffer(const char *const prompt, char *buffer,static int initted;/* -* History uses a global array which is not -* writable until after relocation to RAM. -* Revert to non-history version if still -* running from flash. +* Say N to CMD_HISTORY_USE_CALLOC will skip runtime +* allocation for the history buffer and directly +* use an uninitialized static array as the buffer. +* Doing this might have better performance and not +* increase the binary file's size, as it only marks +* the size. However, the array is only writable after +* relocation to RAM. If u-boot is running from ROM +* all the time, consider say Y to CMD_HISTORY_USE_CALLOC +* or disable CMD_HISTORY. */ if (IS_ENABLED (CONFIG_CMDLINE_EDITING ) && (gd->flags &GD_FLG_RELOC )) {if (!initted) {
Signature · 1 line
2.34.1
Quoted 8 lines · Hanyuan Zhao
This commit allows users to choose the appropriate memory allocation method between static allocated and dynamically calloc. The previous static-array way will not obviously contribute to the final binary size since it is uninitialized, and might have better performance than the dynamical one. Now we provide the users with both the two options.
Signed-off-by Hanyuan Zhao <[email protected]>
Applied to u-boot/next, thanks!
Signature · 1 line
Tom
PATCH v3 3/3 cli: compile history code if and only if history config is selected
In reply to a message not in the archiveThis commit allows user to determine whether to have history recording in command-line. Previously to this commit, the CMD_HISTORY only sets the compiling of cmd/history.c, and the history code in cli_readline.c is always compiled and will take a lot of space to store history even if we say N to CMD_HISTORY.
---
This is v3 of patch series cli: allow users to disable history
if unused at all. Please ignore the v2 version.
---
Changes v1 -> v3:
- Move the #ifdef CONFIG_CMD_HISTORY directives to this patch. These directives
are still necessary when users are not using the history.
--- common/cli_readline.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-)
common/cli_readline.c +25 -12
@@ -67,12 +67,14 @@ static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)#define CTL_BACKSPACE ('\b')#define DEL ((char)255)#define DEL7 ((char)127) -#define CREAD_HIST_CHAR ('!')#define getcmd_putch (ch) putc(ch)#define getcmd_getch () getchar()#define getcmd_cbeep () getcmd_putch('\a') +#ifdef CONFIG_CMD_HISTORY + +#define CREAD_HIST_CHAR ('!')#ifdef CONFIG_SPL_BUILD #define HIST_MAX 3#define HIST_SIZE 32
@@ -93,14 +95,6 @@ static char *hist_list[HIST_MAX];#define add_idx_minus_one () ((hist_add_idx == 0) ? hist_max : hist_add_idx-1) -static void getcmd_putchars (int count,int ch) -{ -int i; - -for (i =0 ; i < count; i++) -getcmd_putch (ch); -} -static int hist_init (void ) {int i;
@@ -201,6 +195,15 @@ void cread_print_hist_list(void) i++; } } +#endif /* CONFIG_CMD_HISTORY */ + +static void getcmd_putchars (int count,int ch) +{ +int i; + +for (i =0 ; i < count; i++) +getcmd_putch (ch); +}#define BEGINNING_OF_LINE () { \ while (cls->num) { \
@@ -374,6 +377,7 @@ int cread_line_process_ch(struct cli_line_state *cls, char ichar) cls->eol_num--; }break ; +#ifdef CONFIG_CMD_HISTORY case CTL_CH ('p' ):case CTL_CH ('n' ):if (cls->history) {
@@ -403,6 +407,7 @@ int cread_line_process_ch(struct cli_line_state *cls, char ichar) break; }break ; +#endif case '\t' :if (IS_ENABLED (CONFIG_AUTO_COMPLETE ) && cls->cmd_complete) {int num2, col;
@@ -499,19 +504,23 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len, } *len = cls->eol_num; +#ifdef CONFIG_CMD_HISTORY if (buf[0 ] && buf[0 ] !=CREAD_HIST_CHAR )cread_add_to_hist (buf); hist_cur = hist_add_idx; +#endif return 0 ; }#else /* !CONFIG_CMDLINE_EDITING */ +#ifdef CONFIG_CMD_HISTORY static inline int hist_init (void ) {return 0 ; } +#endif static int cread_line (const char *const prompt,char *buf,unsigned int *len,int timeout)
@@ -649,7 +658,9 @@ int cli_readline_into_buffer(const char *const prompt, char *buffer,char *p = buffer;uint len =CONFIG_SYS_CBSIZE ;int rc; -static int initted; +#ifdef CONFIG_CMD_HISTORY +static int hist_initted; +#endif /* * Say N to CMD_HISTORY_USE_CALLOC will skip runtime
@@ -663,11 +674,13 @@ int cli_readline_into_buffer(const char *const prompt, char *buffer,* or disable CMD_HISTORY. */ if (IS_ENABLED (CONFIG_CMDLINE_EDITING ) && (gd->flags &GD_FLG_RELOC )) { -if (!initted) { +#ifdef CONFIG_CMD_HISTORY +if (!hist_initted) { rc =hist_init ();if (rc ==0 ) - initted =1 ; + hist_initted =1 ; } +#endif if (prompt)puts (prompt);
Signature · 1 line
2.34.1
Re: [PATCH v3 3/3] cli: compile history code if and only if history config is selected
Quoted 8 lines · Hanyuan Zhao
This commit allows user to determine whether to have history recording in command-line. Previously to this commit, the
CMD_HISTORYonly sets the compiling ofcmd/history.c, and the history code incli_readline.cis always compiled and will take a lot of space to store history even if we say N toCMD_HISTORY.Signed-off-by Hanyuan Zhao <[email protected]>
This part of your series breaks some riscv platforms:
https://source.denx.de/u-boot/u-boot/-/jobs/798333
https://source.denx.de/u-boot/u-boot/-/jobs/798334
Signature · 1 line
Tom