Skip to content

Commit

Permalink
introduce container_.of.h
Browse files Browse the repository at this point in the history
see title

Signed-off-by: Denis Pronin <Pronin.D@raidix.com>
  • Loading branch information
dpronin committed Jul 30, 2023
1 parent 68c7b1b commit eb6ef3c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
26 changes: 26 additions & 0 deletions container_of.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef _CONTAINER_OF_H
#define _CONTAINER_OF_H

#ifdef __cplusplus

#include <cstdlib>
#include <cstddef>

extern "C" {

#define container_of(ptr, type, member) ({ \
auto const *__mptr = static_cast<__typeof__( ((type *)0)->member ) const*>(ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
}

#else // __cplusplus

#include <stdlib.h>
#include <stddef.h>

#define container_of(ptr, type, member) ({ \
const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#endif

#endif // _CONTAINER_OF_H
11 changes: 2 additions & 9 deletions flist.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@
#include <stdlib.h>
#include <stddef.h>

#ifdef __cplusplus
#include "container_of.h"

#ifdef __cplusplus
extern "C" {

#define container_of(ptr, type, member) ({ \
auto const *__mptr = static_cast<__typeof__( ((type *)0)->member ) const*>(ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#else
#define container_of(ptr, type, member) ({ \
const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#endif

/*
Expand Down

0 comments on commit eb6ef3c

Please sign in to comment.