Commit 64419e47518bbba059c80b77558f93ad4804145c

Authored by Mike Frysinger
Committed by Wolfgang Denk
1 parent 8faba4894c

print_buffer: optimize & shrink

Applying a little creative format string allows us to shrink the initial
data read & display loop by only calling printf once.  Re-using the local
data buffer to generate the string we want to display then allows us to
output everything with just one printf call instead of multiple calls to
the putc function.

The local stack buffer needs increasing by 1 byte, but the resulting code
shrink and speed up is worth it I think.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

Showing 1 changed file with 13 additions and 14 deletions Side-by-side Diff

lib/display_options.c
... ... @@ -101,7 +101,7 @@
101 101 #define DEFAULT_LINE_LENGTH_BYTES (16)
102 102 int print_buffer (ulong addr, void* data, uint width, uint count, uint linelen)
103 103 {
104   - uint8_t linebuf[MAX_LINE_LENGTH_BYTES];
  104 + uint8_t linebuf[MAX_LINE_LENGTH_BYTES + 1];
105 105 uint32_t *uip = (void*)linebuf;
106 106 uint16_t *usp = (void*)linebuf;
107 107 uint8_t *ucp = (void*)linebuf;
108 108  
109 109  
... ... @@ -121,24 +121,23 @@
121 121  
122 122 /* Copy from memory into linebuf and print hex values */
123 123 for (i = 0; i < linelen; i++) {
124   - if (width == 4) {
125   - uip[i] = *(volatile uint32_t *)data;
126   - printf(" %08x", uip[i]);
127   - } else if (width == 2) {
128   - usp[i] = *(volatile uint16_t *)data;
129   - printf(" %04x", usp[i]);
130   - } else {
131   - ucp[i] = *(volatile uint8_t *)data;
132   - printf(" %02x", ucp[i]);
133   - }
  124 + uint32_t x;
  125 + if (width == 4)
  126 + x = uip[i] = *(volatile uint32_t *)data;
  127 + else if (width == 2)
  128 + x = usp[i] = *(volatile uint16_t *)data;
  129 + else
  130 + x = ucp[i] = *(volatile uint8_t *)data;
  131 + printf(" %0*x", width * 2, x);
134 132 data += width;
135 133 }
136 134  
137 135 /* Print data in ASCII characters */
138   - puts(" ");
139 136 for (i = 0; i < linelen * width; i++)
140   - putc(isprint(ucp[i]) && (ucp[i] < 0x80) ? ucp[i] : '.');
141   - putc ('\n');
  137 + if (!isprint(ucp[i]) || ucp[i] >= 0x80)
  138 + ucp[i] = '.';
  139 + ucp[i] = '\0';
  140 + printf(" %s\n", ucp);
142 141  
143 142 /* update references */
144 143 addr += linelen * width;