Commit 5a970809 authored by Alejandro Mery's avatar Alejandro Mery
Browse files

sunxi-tools.h: add container_of() helper

parent b65db76e
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
#ifndef _SUNXI_TOOLS_H #ifndef _SUNXI_TOOLS_H
#define _SUNXI_TOOLS_H #define _SUNXI_TOOLS_H
#include <stddef.h> /* offsetof */
/** flat function argument as unused */ /** flat function argument as unused */
#ifdef UNUSED #ifdef UNUSED
#elif defined(__GNUC__) #elif defined(__GNUC__)
...@@ -25,6 +27,11 @@ ...@@ -25,6 +27,11 @@
# define UNUSED(x) UNUSED_ ## x # define UNUSED(x) UNUSED_ ## x
#endif #endif
/** finds the parent of an struct member */
#ifndef container_of
#define container_of(P,T,M) (T *)((char *)(P) - offsetof(T, M))
#endif
/** shortcut to printf to stderr */ /** shortcut to printf to stderr */
#define errf(...) fprintf(stderr, __VA_ARGS__) #define errf(...) fprintf(stderr, __VA_ARGS__)
...@@ -51,7 +58,7 @@ static inline void list_append(struct list_entry *l0, struct list_entry *l1) ...@@ -51,7 +58,7 @@ static inline void list_append(struct list_entry *l0, struct list_entry *l1)
/** returns list element of a list */ /** returns list element of a list */
static inline struct list_entry *list_last(struct list_entry *l) static inline struct list_entry *list_last(struct list_entry *l)
{ {
return (l->prev == l) ? (void*)0 : l->prev; return (l->prev == l) ? NULL : l->prev;
} }
/** is list empty? */ /** is list empty? */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment