From ca7b20068c9d373e41045a2e899a9c697576262c Mon Sep 17 00:00:00 2001 From: Anjan Chanda <anjan.chanda@genexis.eu> Date: Mon, 19 May 2025 13:50:30 +0200 Subject: [PATCH] define offsetof() and container_of() --- hlist.h | 12 ++++++++++++ utils.h | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/hlist.h b/hlist.h index 100fca1..b68343c 100644 --- a/hlist.h +++ b/hlist.h @@ -14,6 +14,18 @@ #ifndef _HLIST_H #define _HLIST_H +#ifndef offsetof +#define offsetof(type, member) ((size_t) &((type *)0)->member) +#endif + +#ifndef container_of +#define container_of(ptr, type, member) \ +({ \ + const __typeof__(((type *) NULL)->member) *__mptr = (ptr); \ + (type *) ((char *) __mptr - offsetof(type, member)); \ +}) +#endif + struct hlist_node { struct hlist_node *next; }; diff --git a/utils.h b/utils.h index 0cfb836..c14e770 100644 --- a/utils.h +++ b/utils.h @@ -136,6 +136,14 @@ extern int get_ifoperstatus(const char *ifname, ifopstatus_t *opstatus); #define offsetof(type, member) ((size_t) &((type *)0)->member) #endif +#ifndef container_of +#define container_of(ptr, type, member) \ +({ \ + const __typeof__(((type *) NULL)->member) *__mptr = (ptr); \ + (type *) ((char *) __mptr - offsetof(type, member)); \ +}) +#endif + #ifndef bit #define bit(_n) (1 << (_n)) #endif -- GitLab