summaryrefslogtreecommitdiff
path: root/mbglib/common/mbg_cof.h
blob: 0c0e96f92397eb583c77a61462879a9702bb6c42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

/**************************************************************************
 *
 *  $Id: mbg_cof.h 1.1.1.3 2016/08/10 14:19:00 martin TEST $
 *
 *  Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
 *
 *  Description:
 *    Container macros (see Linux Kernel)
 *
 * -----------------------------------------------------------------------
 *  $Log: mbg_cof.h $
 *  Revision 1.1.1.3  2016/08/10 14:19:00  martin
 *  *** empty log message ***
 *  Revision 1.1.1.2  2016/08/10 14:15:14  martin
 *  *** empty log message ***
 *  Revision 1.1.1.1  2016/08/09 15:59:01  martin
 *  Preliminary compatibility changes.
 *  Revision 1.1  2015/09/09 10:42:27  martin
 *  Initial revision by philipp.
 *
 **************************************************************************/

#ifndef _MBG_COF_H
#define _MBG_COF_H

/* Other headers to be included */

#include <mbg_tgt.h>

#if !defined( MBG_TGT_KERNEL )
  #include <stddef.h>   // for offsetof()
#endif


#ifdef _MBG_COF
 #define _ext
 #define _DO_INIT
#else
 #define _ext extern
#endif


/* Start of header body */

#ifdef __cplusplus
extern "C" {
#endif


#if defined( MBG_TGT_POSIX )

  // A special construct supported by gcc/clang and and implemented
  // e.g. in the Linux kernel, which is said to be very type-safe:
  #define mbg_container_of( _ptr, _type, _member ) ({          \
    const typeof( ((_type *)0)->_member ) *__mptr = (_ptr);    \
    (_type *)((char *)__mptr - offsetof(_type,_member));})

#else

  // A different implementation in ANSI C, which supports type-checking anyway:
  #define mbg_container_of( _ptr, _type, _member ) \
    ( (_type *)( (char *)(1 ? (_ptr) : &((_type *)0)->_member) - offsetof( _type, _member )))

#endif


/* function prototypes: */

/* ----- function prototypes begin ----- */

/* This section was generated automatically */
/* by MAKEHDR, do not remove the comments. */

/* (no header definitions found) */

/* ----- function prototypes end ----- */

#ifdef __cplusplus
}
#endif

/* End of header body */

#undef _ext
#undef _DO_INIT

#endif  /* _MBG_COF_H */