Nico Huber has uploaded this change for review.

View Change

lib.h: Add convenient memcpy() variants

Add two macros that should help in cases where the arguments are
objects (not pointers) and the count of bytes to be copied is the
size of one of them (or both).

* memcpy_dlen(dst, src) copies `sizeof(dst)` bytes (destination
length).
* memcpy_elen(dst, src) asserts that both sizes match and copies
as many bytes (equal length).

Change-Id: I2b23066b3bcc29ff679e620f723ce1da5057aa03
Signed-off-by: Nico Huber <nico.h@gmx.de>
---
M src/include/lib.h
1 file changed, 15 insertions(+), 0 deletions(-)

git pull ssh://review.coreboot.org:29418/coreboot refs/changes/40/32740/1
diff --git a/src/include/lib.h b/src/include/lib.h
index 098d62d..6cbe6f3 100644
--- a/src/include/lib.h
+++ b/src/include/lib.h
@@ -18,6 +18,7 @@
#ifndef __LIB_H__
#define __LIB_H__
#include <stdint.h>
+#include <string.h>
#include <types.h>

/* Defined in src/lib/lzma.c. Returns decompressed size or 0 on error. */
@@ -69,4 +70,18 @@
/* Integer binary logarithm (rounding up): log2_ceil(0) == -1, log2(5) == 3 */
static inline int log2_ceil(u32 x) { return (x == 0) ? -1 : log2(x * 2 - 1); }

+/* memcpy destination length (memcpy_dlen) copies `sizeof(dst)` bytes. */
+#define memcpy_dlen(dst, src) memcpy(dst, src, sizeof(dst))
+
+/*
+ * memcpy equal length (memcpy_elen)
+ * Asserts that both objects `dst` and `src`
+ * have the same size and copies as many bytes.
+ */
+#define memcpy_elen(dst, src) do { \
+ _Static_assert(sizeof(dst) == sizeof(src), \
+ "`dst` and `src` need equal length!"); \
+ memcpy_dlen(dst, src); \
+} while (0)
+
#endif /* __LIB_H__ */

To view, visit change 32740. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-Change-Id: I2b23066b3bcc29ff679e620f723ce1da5057aa03
Gerrit-Change-Number: 32740
Gerrit-PatchSet: 1
Gerrit-Owner: Nico Huber <nico.h@gmx.de>
Gerrit-MessageType: newchange