blob: b020280ef0b13052e3cd706003a9eca976c4c5cf [file] [log] [blame]
Thierry FOURNIER3c65b7a2017-08-31 20:35:18 +02001#ifndef __XREF_H__
2#define __XREF_H__
3
4/* xref is used to create relation between two elements.
5 * Once an element is released, it breaks the relation. If the
6 * relation is already broken, it frees the xref struct.
7 * The pointer between two elements is sort of a refcount with
8 * max value 1. The relation is only between two elements.
9 * The pointer and the type of elements a and b are conventional.
10 */
11
12struct xref {
13 struct xref *peer;
14};
15
16static inline void xref_create(struct xref *xref_a, struct xref *xref_b)
17{
18 xref_a->peer = xref_b;
19 xref_b->peer = xref_a;
20}
21
22static inline struct xref *xref_get_peer(struct xref *xref)
23{
24 if (!xref->peer)
25 return NULL;
26 return xref->peer;
27}
28
29static inline void xref_disconnect(struct xref *xref)
30{
31 if (!xref->peer)
32 return;
33
34 xref->peer->peer = NULL;
35 xref->peer = NULL;
36}
37
38#endif /* __XREF_H__ */