[coreboot-gerrit] Change in coreboot[master]: libpayload: Clean up unaligned memset() support
Nico Huber (Code Review)
gerrit at coreboot.org
Mon Jul 24 16:30:33 CEST 2017
Nico Huber has uploaded this change for review. ( https://review.coreboot.org/20750
Change subject: libpayload: Clean up unaligned memset() support
......................................................................
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 at secunet.com>
---
M payloads/libpayload/libc/memory.c
1 file changed, 10 insertions(+), 12 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/50/20750/1
diff --git a/payloads/libpayload/libc/memory.c b/payloads/libpayload/libc/memory.c
index 8d0172c..25c2b3a 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)
--
To view, visit https://review.coreboot.org/20750
To unsubscribe, visit https://review.coreboot.org/settings
Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I0c8fd118d127a26cfcf68bfb0bf681495821e80a
Gerrit-Change-Number: 20750
Gerrit-PatchSet: 1
Gerrit-Owner: Nico Huber <nico.h at gmx.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.coreboot.org/pipermail/coreboot-gerrit/attachments/20170724/ea58d091/attachment.html>
More information about the coreboot-gerrit
mailing list