diff options
author | Hanyuan Zhao | 2024-03-05 15:37:35 +0800 |
---|---|---|
committer | Tom Rini | 2024-03-13 12:40:43 -0400 |
commit | 421359ac52bfc80946929c4ffeacf6a68273e758 (patch) | |
tree | 7107a561bfccef84411b2d04500ae457d03d27f0 /common | |
parent | 44951340309d0dca73c05fc61406a1e839d1da93 (diff) |
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.
Signed-off-by: Hanyuan Zhao <hanyuan-z@qq.com>
Diffstat (limited to 'common')
-rw-r--r-- | common/cli_readline.c | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/common/cli_readline.c b/common/cli_readline.c index 99e7efdfe55..cf4339d0e50 100644 --- a/common/cli_readline.c +++ b/common/cli_readline.c @@ -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) { |