Skip to content

Commit a7e2515

Browse files
committed
sys/shell: reduce overhead of XFA shell commands
We do not need to add an array of pointers to the shell commands, just an array of shell commands is sufficient. This reduced the overhead of XFA by `sizeof(void *)` per command.
1 parent 2be92b5 commit a7e2515

2 files changed

Lines changed: 10 additions & 12 deletions

File tree

sys/include/shell.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,15 +302,14 @@ int shell_parse_file(const shell_command_t *commands,
302302
* ```
303303
*/
304304
#define SHELL_COMMAND(cmd, help, func) \
305-
XFA_USE_CONST(shell_command_xfa_t*, shell_commands_xfa); \
305+
XFA_USE_CONST(shell_command_xfa_t, shell_commands_xfa_v2); \
306306
static FLASH_ATTR const char _xfa_ ## cmd ## _cmd_name[] = #cmd; \
307307
static FLASH_ATTR const char _xfa_ ## cmd ## _cmd_desc[] = help; \
308-
static const shell_command_xfa_t _xfa_ ## cmd ## _cmd = { \
308+
XFA_CONST(shell_command_xfa_t, shell_commands_xfa_v2, 0) _xfa_ ## cmd ## _cmd = { \
309309
.name = _xfa_ ## cmd ## _cmd_name, \
310310
.desc = _xfa_ ## cmd ## _cmd_desc, \
311311
.handler = &func \
312-
}; \
313-
XFA_ADD_PTR(shell_commands_xfa, cmd, cmd, &_xfa_ ## cmd ## _cmd)
312+
};
314313
#endif /* __cplusplus */
315314

316315
#ifdef __cplusplus

sys/shell/shell.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include <assert.h>
3737
#include <errno.h>
3838

39-
#include "kernel_defines.h"
4039
#include "xfa.h"
4140
#include "shell.h"
4241
#include "shell_lock.h"
@@ -47,7 +46,7 @@
4746
#endif
4847

4948
/* define shell command cross file array */
50-
XFA_INIT_CONST(shell_command_xfa_t*, shell_commands_xfa);
49+
XFA_INIT_CONST(shell_command_xfa_t, shell_commands_xfa_v2);
5150

5251
#define ETX '\x03' /** ASCII "End-of-Text", or Ctrl-C */
5352
#define EOT '\x04' /** ASCII "End-of-Transmission", or Ctrl-D */
@@ -102,10 +101,10 @@ static shell_command_handler_t search_commands(const shell_command_t *entry,
102101

103102
static shell_command_handler_t search_commands_xfa(char *command)
104103
{
105-
unsigned n = XFA_LEN(shell_command_t*, shell_commands_xfa);
104+
unsigned n = XFA_LEN(shell_command_t, shell_commands_xfa_v2);
106105

107106
for (unsigned i = 0; i < n; i++) {
108-
const volatile shell_command_xfa_t *entry = shell_commands_xfa[i];
107+
const volatile shell_command_xfa_t *entry = &shell_commands_xfa_v2[i];
109108
if (flash_strcmp(command, entry->name) == 0) {
110109
return entry->handler;
111110
}
@@ -147,15 +146,15 @@ static void print_commands_json(const shell_command_t *cmd_list)
147146
}
148147
}
149148

150-
unsigned n = XFA_LEN(shell_command_xfa_t*, shell_commands_xfa);
149+
unsigned n = XFA_LEN(shell_command_xfa_t, shell_commands_xfa_v2);
151150
for (unsigned i = 0; i < n; i++) {
152151
if (first) {
153152
first = false;
154153
}
155154
else {
156155
printf(", ");
157156
}
158-
const volatile shell_command_xfa_t *entry = shell_commands_xfa[i];
157+
const volatile shell_command_xfa_t *entry = &shell_commands_xfa_v2[i];
159158
printf("{\"cmd\": \"%s\", \"desc\": \"%s\"}", entry->name, entry->desc);
160159
}
161160
puts("]}");
@@ -170,9 +169,9 @@ static void print_commands(const shell_command_t *entry)
170169

171170
static void print_commands_xfa(void)
172171
{
173-
unsigned n = XFA_LEN(shell_command_xfa_t*, shell_commands_xfa);
172+
unsigned n = XFA_LEN(shell_command_xfa_t, shell_commands_xfa_v2);
174173
for (unsigned i = 0; i < n; i++) {
175-
const volatile shell_command_xfa_t *entry = shell_commands_xfa[i];
174+
const volatile shell_command_xfa_t *entry = &shell_commands_xfa_v2[i];
176175
printf("%-20" PRIsflash " %" PRIsflash "\n",
177176
entry->name, entry->desc);
178177
}

0 commit comments

Comments
 (0)