Xenomai  3.0-rc3
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 
24 #define libcopperplate_tag 0 /* Library tag - unique and constant. */
25 #define libcopperplate_cbi 1 /* Callback binary interface level. */
26 
27 #ifdef CONFIG_XENO_PSHARED
28 /*
29  * Layout of a function reference handle in shared memory (32-bit
30  * value):
31  *
32  * xxHHHHHHHHHHHHHHHHHHHHLLLLLPPPPP
33  *
34  * where: 'P' => function index in the per-library array
35  * 'L' => library tag
36  * 'H' => symbol hash value (symname + cbi)
37  * 'x' => unassigned
38  *
39  * NOTE: handle value -1 is kept for representing a NULL function
40  * pointer; bit #31 should remain unassigned and cleared for this
41  * purpose.
42  */
43 
44 struct __fnref {
45  void (*fn)(void);
46  unsigned int hash;
47 };
48 
49 #define __refvar(l, s) __ ## l ## __ref__ ## s
50 #define __refmangle(l, h, p) (((h & 0xfffff) << 10)|((l & 0x1f) << 5)|(p & 0x1f))
51 #define __refhash(r) (((r) >> 10) & 0xfffffU)
52 #define __reftag(r) (((r) >> 5) & 0x1f)
53 #define __refpos(r) ((r) & 0x1f)
54 #define __refchk(v, r) \
55  ({ \
56  int __tag = __reftag(r), __pos = __refpos(r); \
57  typeof(v) __p = (typeof(v))__fnrefs[__tag][__pos].fn; \
58  assert(__fnrefs[__tag][__pos].hash == __refhash(r)); \
59  assert(__p != NULL); \
60  __p; \
61  })
62 #define fnref_type(t) int
63 #define fnref_null -1
64 static inline int __fnref_nofn(void *fnaddr)
65 {
66  return fnaddr == NULL;
67 }
68 #define fnref_put(l, s) (__fnref_nofn((void *)(s)) ? fnref_null : __refvar(l, s))
69 #define fnref_get(v, r) ((v) = (r) < 0 ? NULL : __refchk(v, r))
70 #define fnref_register(l, s) \
71  int __refvar(l, s); \
72  static void __attribute__ ((constructor)) __ifnref_ ## s(void) \
73  { \
74  __refvar(l, s) = __fnref_register(#l, l ## _tag, \
75  l ## _cbi, \
76  #s, (void (*)(void))s); \
77  }
78 #define fnref_declare(l, s) extern int __refvar(l, s)
79 
80 #define MAX_FNLIBS 16 /* max=32 */
81 #define MAX_FNREFS 16 /* max=32 */
82 
83 extern struct __fnref __fnrefs[MAX_FNLIBS][MAX_FNREFS];
84 
85 int __fnref_register(const char *libname,
86  int libtag, int cbirev,
87  const char *symname, void (*fn)(void));
88 
89 #else /* !CONFIG_XENO_PSHARED */
90 
91 #define fnref_type(t) __typeof__(t)
92 #define fnref_null NULL
93 #define fnref_put(l, s) (s)
94 #define fnref_get(v, r) ((v) = (r))
95 #define fnref_register(l, s)
96 #define fnref_declare(l, s)
97 
98 #endif /* !CONFIG_XENO_PSHARED */
99 
100 #endif /* _COPPERPLATE_REFERENCE_H */