summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNico Huber <nico.huber@secunet.com>2017-07-24 15:49:14 +0200
committerNico Huber <nico.h@gmx.de>2017-07-26 19:34:02 +0000
commit50ab84fa370ac247dfe57a65f9d9b1ed0384e7fa (patch)
treed9031f4fc15a6aff136f6a41a490815305a39d64
parentc5362029e17f69d265178b7c6312a95e5d47a54b (diff)
downloadcoreboot-50ab84fa370ac247dfe57a65f9d9b1ed0384e7fa.tar.xz
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>
-rw-r--r--payloads/libpayload/libc/memory.c22
1 files changed, 10 insertions, 12 deletions
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 <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;
- 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)