From 50ab84fa370ac247dfe57a65f9d9b1ed0384e7fa Mon Sep 17 00:00:00 2001 From: Nico Huber Date: Mon, 24 Jul 2017 15:49:14 +0200 Subject: 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 Reviewed-on: https://review.coreboot.org/20750 Tested-by: build bot (Jenkins) Reviewed-by: Julius Werner --- payloads/libpayload/libc/memory.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'payloads/libpayload/libc') diff --git a/payloads/libpayload/libc/memory.c b/payloads/libpayload/libc/memory.c index 8d0172cc44..25c2b3af4f 100644 --- a/payloads/libpayload/libc/memory.c +++ b/payloads/libpayload/libc/memory.c @@ -33,31 +33,29 @@ #include -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; - void *ret = s; + u8 *dst = s; unsigned long w = c & 0xff; - u8 *p = s; - s = (void *)ALIGN_UP((uintptr_t)s, sizeof(unsigned long)); - while (p != (u8 *)s && n) { - *p++ = c; - n--; - } + const u8 *const aligned_start = + (const u8 *)ALIGN_UP((uintptr_t)dst, sizeof(unsigned long)); + for (; n > 0 && dst != aligned_start; --n, ++dst) + *dst = (u8)c; for (i = 1; i < sizeof(unsigned long); i <<= 1) w = (w << (i * 8)) | w; 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++) - ((u8 *)s)[i] = (u8)c; + dst[i] = (u8)c; - return ret; + return s; } void *memset(void *s, int c, size_t n) -- cgit v1.2.3