Xenomai  3.1
reference.h
1 /*
2  * Copyright (C) 2010 Philippe Gerum <rpm@xenomai.org>.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18 #ifndef _COPPERPLATE_REFERENCE_H
19 #define _COPPERPLATE_REFERENCE_H
20 
21 #include <boilerplate/limits.h>
22 #include <boilerplate/scope.h>
23 #include <boilerplate/setup.h>
24 
25 #define libcopperplate_tag 0 /* Library tag - unique and constant. */
26 #define libcopperplate_cbi 1 /* Callback binary interface level. */
27 
28 #ifdef CONFIG_XENO_PSHARED
29 /*
30  * Layout of a function reference handle in shared memory (32-bit
31  * value):
32  *
33  * xxHHHHHHHHHHHHHHHHHHHHLLLLLPPPPP
34  *
35  * where: 'P' => function index in the per-library array
36  * 'L' => library tag
37  * 'H' => symbol hash value (symname + cbi)
38  * 'x' => unassigned
39  *
40  * NOTE: handle value -1 is kept for representing a NULL function
41  * pointer; bit #31 should remain unassigned and cleared for this
42  * purpose.
43  */
44 
45 struct __fnref {
46  void (*fn)(void);
47  unsigned int hash;
48 };
49 
50 #define __refvar(l, s) __ ## l ## __ref__ ## s
51 #define __refmangle(l, h, p) (((h & 0xfffff) << 10)|((l & 0x1f) << 5)|(p & 0x1f))
52 #define __refhash(r) (((r) >> 10) & 0xfffffU)
53 #define __reftag(r) (((r) >> 5) & 0x1f)
54 #define __refpos(r) ((r) & 0x1f)
55 #define __refchk(v, r) \
56  ({ \
57  int __tag = __reftag(r), __pos = __refpos(r); \
58  typeof(v) __p = (typeof(v))__fnrefs[__tag][__pos].fn; \
59  assert(__fnrefs[__tag][__pos].hash == __refhash(r)); \
60  assert(__p != NULL); \
61  __p; \
62  })
63 #define fnref_type(t) int
64 #define fnref_null -1
65 static inline int __fnref_nofn(void *fnaddr)
66 {
67  return fnaddr == NULL;
68 }
69 #define fnref_put(l, s) (__fnref_nofn((void *)(s)) ? fnref_null : __refvar(l, s))
70 #define fnref_get(v, r) ((v) = (r) < 0 ? NULL : __refchk(v, r))
71 #define fnref_register(l, s) \
72  int __refvar(l, s); \
73  static void __early_ctor __ifnref_ ## s(void) \
74  { \
75  __refvar(l, s) = __fnref_register(#l, l ## _tag, \
76  l ## _cbi, \
77  #s, (void (*)(void))s); \
78  }
79 #define fnref_declare(l, s) extern int __refvar(l, s)
80 
81 #define MAX_FNLIBS 16 /* max=32 */
82 #define MAX_FNREFS 16 /* max=32 */
83 
84 extern struct __fnref __fnrefs[MAX_FNLIBS][MAX_FNREFS];
85 
86 int __fnref_register(const char *libname,
87  int libtag, int cbirev,
88  const char *symname, void (*fn)(void));
89 
90 #else /* !CONFIG_XENO_PSHARED */
91 
92 #define fnref_type(t) __typeof__(t)
93 #define fnref_null NULL
94 #define fnref_put(l, s) (s)
95 #define fnref_get(v, r) ((v) = (r))
96 #define fnref_register(l, s)
97 #define fnref_declare(l, s)
98 
99 #endif /* !CONFIG_XENO_PSHARED */
100 
101 #endif /* _COPPERPLATE_REFERENCE_H */