Extract container_of definition out of individual bindings
This will make possible reuse this portion on code in the future.
Testing: no change in functionality; compiles successfuly
Signed-off-by: Przemyslaw Czarnowski <przemyslaw.hawrylewicz.czarnowski@intel.com>
Change-Id: Ie92161c547e275d9aa00d643d8d4879d2f5dd199
diff --git a/astlpc.c b/astlpc.c
index f8cf42e..66fa930 100644
--- a/astlpc.c
+++ b/astlpc.c
@@ -21,6 +21,7 @@
#include "libmctp-alloc.h"
#include "libmctp-log.h"
#include "libmctp-astlpc.h"
+#include "container_of.h"
#ifdef MCTP_HAVE_FILEIO
@@ -60,11 +61,6 @@
uint8_t txbuf[256];
};
-#ifndef container_of
-#define container_of(ptr, type, member) \
- (type *)((char *)(ptr) - (char *)&((type *)0)->member)
-#endif
-
#define binding_to_astlpc(b) \
container_of(b, struct mctp_binding_astlpc, binding)
diff --git a/container_of.h b/container_of.h
new file mode 100644
index 0000000..a0e181a
--- /dev/null
+++ b/container_of.h
@@ -0,0 +1,9 @@
+#ifndef _CONTAINER_OF_H
+#define _CONTAINER_OF_H
+
+#ifndef container_of
+#define container_of(ptr, type, member) \
+ (type *)((char *)(ptr) - (char *)&((type *)0)->member)
+#endif
+
+#endif
diff --git a/serial.c b/serial.c
index f24148a..007a377 100644
--- a/serial.c
+++ b/serial.c
@@ -38,6 +38,7 @@
#include "libmctp-alloc.h"
#include "libmctp-log.h"
#include "libmctp-serial.h"
+#include "container_of.h"
struct mctp_binding_serial {
struct mctp_binding binding;
@@ -67,11 +68,6 @@
uint8_t txbuf[256];
};
-#ifndef container_of
-#define container_of(ptr, type, member) \
- (type *)((char *)(ptr) - (char *)&((type *)0)->member)
-#endif
-
#define binding_to_serial(b) \
container_of(b, struct mctp_binding_serial, binding)
diff --git a/tests/test-utils.h b/tests/test-utils.h
index e8c74f1..4b000e1 100644
--- a/tests/test-utils.h
+++ b/tests/test-utils.h
@@ -4,11 +4,7 @@
#define _MCTP_TESTS_TEST_UTILS_H
#include <libmctp.h>
-
-#ifndef container_of
-#define container_of(ptr, type, member) \
- (type *)((char *)(ptr) - (char *)&((type *)0)->member)
-#endif
+#include <container_of.h>
/* test binding implementation */
diff --git a/tests/test_astlpc.c b/tests/test_astlpc.c
index efa38ab..2d637de 100644
--- a/tests/test_astlpc.c
+++ b/tests/test_astlpc.c
@@ -6,6 +6,7 @@
#include "libmctp-astlpc.h"
#include "libmctp-log.h"
+#include "container_of.h"
#ifdef NDEBUG
#undef NDEBUG
@@ -58,11 +59,6 @@
uint8_t *lpc;
};
-#ifndef container_of
-#define container_of(ptr, type, member) \
- (type *)((char *)(ptr) - (char *)&((type *)0)->member)
-#endif
-
#define binding_to_mmio(b) \
container_of(b, struct mctp_binding_astlpc_mmio, astlpc)