Skip to content
Snippets Groups Projects
Commit 64419e47 authored by Mike Frysinger's avatar Mike Frysinger Committed by Wolfgang Denk
Browse files

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: default avatarMike Frysinger <vapier@gentoo.org>
parent 8faba489
No related branches found
No related tags found
No related merge requests found
...@@ -101,7 +101,7 @@ void print_size(unsigned long long size, const char *s) ...@@ -101,7 +101,7 @@ void print_size(unsigned long long size, const char *s)
#define DEFAULT_LINE_LENGTH_BYTES (16) #define DEFAULT_LINE_LENGTH_BYTES (16)
int print_buffer (ulong addr, void* data, uint width, uint count, uint linelen) int print_buffer (ulong addr, void* data, uint width, uint count, uint linelen)
{ {
uint8_t linebuf[MAX_LINE_LENGTH_BYTES]; uint8_t linebuf[MAX_LINE_LENGTH_BYTES + 1];
uint32_t *uip = (void*)linebuf; uint32_t *uip = (void*)linebuf;
uint16_t *usp = (void*)linebuf; uint16_t *usp = (void*)linebuf;
uint8_t *ucp = (void*)linebuf; uint8_t *ucp = (void*)linebuf;
...@@ -121,24 +121,23 @@ int print_buffer (ulong addr, void* data, uint width, uint count, uint linelen) ...@@ -121,24 +121,23 @@ int print_buffer (ulong addr, void* data, uint width, uint count, uint linelen)
/* Copy from memory into linebuf and print hex values */ /* Copy from memory into linebuf and print hex values */
for (i = 0; i < linelen; i++) { for (i = 0; i < linelen; i++) {
if (width == 4) { uint32_t x;
uip[i] = *(volatile uint32_t *)data; if (width == 4)
printf(" %08x", uip[i]); x = uip[i] = *(volatile uint32_t *)data;
} else if (width == 2) { else if (width == 2)
usp[i] = *(volatile uint16_t *)data; x = usp[i] = *(volatile uint16_t *)data;
printf(" %04x", usp[i]); else
} else { x = ucp[i] = *(volatile uint8_t *)data;
ucp[i] = *(volatile uint8_t *)data; printf(" %0*x", width * 2, x);
printf(" %02x", ucp[i]);
}
data += width; data += width;
} }
/* Print data in ASCII characters */ /* Print data in ASCII characters */
puts(" ");
for (i = 0; i < linelen * width; i++) for (i = 0; i < linelen * width; i++)
putc(isprint(ucp[i]) && (ucp[i] < 0x80) ? ucp[i] : '.'); if (!isprint(ucp[i]) || ucp[i] >= 0x80)
putc ('\n'); ucp[i] = '.';
ucp[i] = '\0';
printf(" %s\n", ucp);
/* update references */ /* update references */
addr += linelen * width; addr += linelen * width;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment