libpayload: Clean up unaligned memset() support

Use a `for` instead of a `while` loop and use meaningful identifiers.
Also, don't use more than one variable for one and the same purpose,
don't use more (non-const) variables than necessary, don't alter more
than one variable per statement, don't compare pointers of different
types and don't do pointer arithmetic on `void *`.

This was meant as a fix up to a regression but that has already been
fixed.

Change-Id: I0c8fd118d127a26cfcf68bfb0bf681495821e80a
Signed-off-by: Nico Huber <nico.huber@secunet.com>
Reviewed-on: https://review.coreboot.org/20750
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
This commit is contained in:
Nico Huber 2017-07-24 15:49:14 +02:00 committed by Nico Huber
parent c5362029e1
commit 50ab84fa37
1 changed files with 10 additions and 12 deletions

View File

@ -33,31 +33,29 @@
#include <libpayload.h> #include <libpayload.h>
static void *default_memset(void *s, int c, size_t n) static void *default_memset(void *const s, const int c, size_t n)
{ {
size_t i; size_t i;
void *ret = s; u8 *dst = s;
unsigned long w = c & 0xff; unsigned long w = c & 0xff;
u8 *p = s;
s = (void *)ALIGN_UP((uintptr_t)s, sizeof(unsigned long)); const u8 *const aligned_start =
while (p != (u8 *)s && n) { (const u8 *)ALIGN_UP((uintptr_t)dst, sizeof(unsigned long));
*p++ = c; for (; n > 0 && dst != aligned_start; --n, ++dst)
n--; *dst = (u8)c;
}
for (i = 1; i < sizeof(unsigned long); i <<= 1) for (i = 1; i < sizeof(unsigned long); i <<= 1)
w = (w << (i * 8)) | w; w = (w << (i * 8)) | w;
for (i = 0; i < n / sizeof(unsigned long); i++) for (i = 0; i < n / sizeof(unsigned long); i++)
((unsigned long *)s)[i] = w; ((unsigned long *)dst)[i] = w;
s += i * sizeof(unsigned long); dst += i * sizeof(unsigned long);
for (i = 0; i < n % sizeof(unsigned long); i++) for (i = 0; i < n % sizeof(unsigned long); i++)
((u8 *)s)[i] = (u8)c; dst[i] = (u8)c;
return ret; return s;
} }
void *memset(void *s, int c, size_t n) void *memset(void *s, int c, size_t n)