feature(console): updated console alloc config to use static config struct

No need to store the config in a separate variable, can re-use the config struct
This commit is contained in:
Marius Vikhammer
2023-08-02 16:17:34 +08:00
parent 9178748ff3
commit 1ec1a69ed0
2 changed files with 8 additions and 9 deletions

View File

@@ -41,14 +41,13 @@ typedef struct cmd_item_ {
static SLIST_HEAD(cmd_list_, cmd_item_) s_cmd_list; static SLIST_HEAD(cmd_list_, cmd_item_) s_cmd_list;
/** run-time configuration options */ /** run-time configuration options */
static esp_console_config_t s_config; static esp_console_config_t s_config = {
.heap_alloc_caps = MALLOC_CAP_DEFAULT
};
/** temporary buffer used for command line parsing */ /** temporary buffer used for command line parsing */
static char *s_tmp_line_buf; static char *s_tmp_line_buf;
/** for internal allocations */
static uint32_t s_heap_alloc_caps = MALLOC_CAP_DEFAULT;
static const cmd_item_t *find_command_by_name(const char *name); static const cmd_item_t *find_command_by_name(const char *name);
esp_err_t esp_console_init(const esp_console_config_t *config) esp_err_t esp_console_init(const esp_console_config_t *config)
@@ -64,9 +63,9 @@ esp_err_t esp_console_init(const esp_console_config_t *config)
s_config.hint_color = ANSI_COLOR_DEFAULT; s_config.hint_color = ANSI_COLOR_DEFAULT;
} }
if (config->heap_alloc_caps != 0) { if (config->heap_alloc_caps != 0) {
s_heap_alloc_caps = config->heap_alloc_caps; s_config.heap_alloc_caps = config->heap_alloc_caps;
} }
s_tmp_line_buf = heap_caps_calloc(config->max_cmdline_length, 1, s_heap_alloc_caps); s_tmp_line_buf = heap_caps_calloc(1, config->max_cmdline_length, s_config.heap_alloc_caps);
if (s_tmp_line_buf == NULL) { if (s_tmp_line_buf == NULL) {
return ESP_ERR_NO_MEM; return ESP_ERR_NO_MEM;
} }
@@ -101,7 +100,7 @@ esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd)
item = (cmd_item_t *)find_command_by_name(cmd->command); item = (cmd_item_t *)find_command_by_name(cmd->command);
if (!item) { if (!item) {
// not registered before // not registered before
item = heap_caps_calloc(1, sizeof(*item), s_heap_alloc_caps); item = heap_caps_calloc(1, sizeof(*item), s_config.heap_alloc_caps);
if (item == NULL) { if (item == NULL) {
return ESP_ERR_NO_MEM; return ESP_ERR_NO_MEM;
} }
@@ -194,7 +193,7 @@ esp_err_t esp_console_run(const char *cmdline, int *cmd_ret)
if (s_tmp_line_buf == NULL) { if (s_tmp_line_buf == NULL) {
return ESP_ERR_INVALID_STATE; return ESP_ERR_INVALID_STATE;
} }
char **argv = (char **) heap_caps_calloc(s_config.max_cmdline_args, sizeof(char *), s_heap_alloc_caps); char **argv = (char **) heap_caps_calloc(s_config.max_cmdline_args, sizeof(char *), s_config.heap_alloc_caps);
if (argv == NULL) { if (argv == NULL) {
return ESP_ERR_NO_MEM; return ESP_ERR_NO_MEM;
} }

View File

@@ -23,7 +23,7 @@ typedef struct linenoiseCompletions linenoiseCompletions;
typedef struct { typedef struct {
size_t max_cmdline_length; //!< length of command line buffer, in bytes size_t max_cmdline_length; //!< length of command line buffer, in bytes
size_t max_cmdline_args; //!< maximum number of command line arguments to parse size_t max_cmdline_args; //!< maximum number of command line arguments to parse
uint32_t heap_alloc_caps; //!< where to place internal allocations (e.g. MALLOC_CAP_SPIRAM) uint32_t heap_alloc_caps; //!< where to (e.g. MALLOC_CAP_SPIRAM) allocate heap objects such as cmds used by esp_console
int hint_color; //!< ASCII color code of hint text int hint_color; //!< ASCII color code of hint text
int hint_bold; //!< Set to 1 to print hint text in bold int hint_bold; //!< Set to 1 to print hint text in bold
} esp_console_config_t; } esp_console_config_t;