[U-Boot] [PATCH] display_buffer: fix misaligned buffer

Wolfgang Denk wd at denx.de
Thu Sep 2 09:39:50 CEST 2010


Dear Reinhard Meyer,

In message <4C7C9B85.6080202 at emk-elektronik.de> you wrote:
> 
> making the change to the union, I also realized that
> 
> /* Copy from memory into linebuf and print hex values */
> for (i = 0; i < linelen; i++) {
> 	uint32_t x;
> 	if (width == 4)
> 		x = lb.u32[i] = *(volatile uint32_t *)data;
> 	else if (width == 2)
> 		x = lb.u16[i] = *(volatile uint16_t *)data;
> 	else
> 		x = lb.u8[i] = *(volatile uint8_t *)data;
> 	printf(" %0*x", width * 2, x);
> 	data += width;
> }
> 
> is still a bit "ugly". What about:
> 
> union data {
> 	u_int32_t *u32;
> 	u_int16_t *u16;
> 	u_int8_t *u8;
> 	void *v;
> } dp;
> dp.v = data;

This is missing the extremely important "volatile" attribute.

> /* Copy from memory into linebuf and print hex values */
> for (i = 0; i < linelen; i++) {
> 	if (width == 4)
> 		x = lb.u32[i] = *(dp.u32)++;
> 	else if (width == 2)
> 		x = lb.u16[i] = *(dp.u16)++;
> 	else
> 		x = lb.u8[i] = *(dp.u8)++;
> 	printf(" %0*x", width * 2, x);
> }
> 
> optionally calling printf inside the if:
> 
> /* Copy from memory into linebuf and print hex values */
> for (i = 0; i < linelen; i++) {
> 	if (width == 4)
> 		printf(" %08x", lb.u32[i] = *(dp.u32)++);
> 	else if (width == 2)
> 		printf(" %04x", lb.u16[i] = *(dp.u16)++);
> 	else
> 		printf(" %02x", lb.u8[i] = *(dp.u8)++);
> }

This will increase the code size, right?

> maybe it would even be more effective to swap for and if:
> 
> /* Copy from memory into linebuf and print hex values */
> if (width == 4) {
> 	for (i = 0; i < linelen; i++)
> 		printf(" %08x", lb.u32[i] = *(dp.u32)++);
> } else if (width == 2) {
> 	for (i = 0; i < linelen; i++)
> 		printf(" %04x", lb.u16[i] = *(dp.u16)++);
> } else {
> 	for (i = 0; i < linelen; i++)
> 		printf(" %02x", lb.u8[i] = *(dp.u8)++);
> }

This is much harder to read and to understand. NAK.

Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
"In the face of entropy and nothingness, you kind of have to  pretend
it's  not  there  if  you  want  to  keep writing good code."
- Karl Lehenbauer


More information about the U-Boot mailing list