mbgtools-lx  4.2.8
mbg_cof.h
Go to the documentation of this file.
1 
2 /**************************************************************************
3  *
4  * $Id: mbg_cof.h 1.2 2017/07/05 14:25:12 martin REL_M $
5  *
6  * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
7  *
8  * Description:
9  * Container macros (see Linux Kernel)
10  *
11  * -----------------------------------------------------------------------
12  * $Log: mbg_cof.h $
13  * Revision 1.2 2017/07/05 14:25:12 martin
14  * Reformatted code to conform to standard header file format.
15  * Changes tfor improved cross-platform compatibility.
16  * Revision 1.1 2015/09/09 10:42:27 martin
17  * Initial revision by philipp.
18  *
19  **************************************************************************/
20 
21 #ifndef _MBG_COF_H
22 #define _MBG_COF_H
23 
24 /* Other headers to be included */
25 
26 #include <mbg_tgt.h>
27 
28 #if !defined( MBG_TGT_KERNEL )
29  #include <stddef.h> // for offsetof()
30 #endif
31 
32 
33 #ifdef _MBG_COF
34  #define _ext
35  #define _DO_INIT
36 #else
37  #define _ext extern
38 #endif
39 
40 
41 /* Start of header body */
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 
48 #if defined( MBG_TGT_POSIX )
49 
50  // A special construct supported by gcc/clang and and implemented
51  // e.g. in the Linux kernel, which is said to be very type-safe:
52  #define mbg_container_of( _ptr, _type, _member ) ({ \
53  const typeof( ((_type *)0)->_member ) *__mptr = (_ptr); \
54  (_type *)((char *)__mptr - offsetof(_type,_member));})
55 
56 #else
57 
58  // A different implementation in ANSI C, which supports type-checking anyway:
59  #define mbg_container_of( _ptr, _type, _member ) \
60  ( (_type *)( (char *)(1 ? (_ptr) : &((_type *)0)->_member) - offsetof( _type, _member )))
61 
62 #endif
63 
64 
65 /* ----- function prototypes begin ----- */
66 
67 /* This section was generated automatically */
68 /* by MAKEHDR, do not remove the comments. */
69 
70 /* (no header definitions found) */
71 
72 /* ----- function prototypes end ----- */
73 
74 #ifdef __cplusplus
75 }
76 #endif
77 
78 /* End of header body */
79 
80 #undef _ext
81 #undef _DO_INIT
82 
83 #endif /* _MBG_COF_H */