]>
git.proxmox.com Git - mirror_frr.git/blob - lib/qobj.h
1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
19 /* reserve a specific amount of bytes for a struct, which can grow up to
20 * that size (or be dummy'd out if not needed)
22 * note the padding's array size will be an error if it gets negative or zero;
23 * this is intentional to prevent the struct from growing beyond the allocated
27 #define RESERVED_SPACE_STRUCT(name, fieldname, size) \
29 struct name fieldname; \
30 char padding##fieldname[size - sizeof(struct name)]; \
33 #define RESERVED_SPACE_STRUCT(name, fieldname, size) \
34 struct name fieldname; \
35 char padding##fieldname[size - sizeof(struct name)];
38 /* don't need struct definitions for these here. code actually using
39 * these needs to define the struct *before* including this header.
40 * HAVE_QOBJ_xxx should be defined to +1 in that case, like this:
42 * #if defined(HAVE_QOBJ_NODETYPE_CLI) && HAVE_QOBJ_NODETYPE_CLI < 0
43 * #error include files are in wrong order
45 * #define HAVE_QOBJ_NODETYPE_CLI 1
46 * struct qobj_nodetype_cli { ... }
49 #ifndef HAVE_QOBJ_NODETYPE_CLI
50 #define HAVE_QOBJ_NODETYPE_CLI -1
51 struct qobj_nodetype_cli
{
56 #ifndef HAVE_QOBJ_NODETYPE_CAPNP
57 #define HAVE_QOBJ_NODETYPE_CAPNP -1
58 struct qobj_nodetype_capnp
{
65 /* each different kind of object will have a global variable of this type,
66 * which can be used by various other pieces to store type-related bits.
67 * type equality can be tested as pointer equality. (cf. QOBJ_GET_TYPESAFE)
69 struct qobj_nodetype
{
70 ptrdiff_t node_member_offset
;
71 RESERVED_SPACE_STRUCT(qobj_nodetype_cli
, cli
, 256)
72 RESERVED_SPACE_STRUCT(qobj_nodetype_capnp
, capnp
, 256)
75 PREDECL_HASH(qobj_nodes
);
77 /* anchor to be embedded somewhere in the object's struct */
80 struct qobj_nodes_item nodehash
;
81 const struct qobj_nodetype
*type
;
84 #define QOBJ_FIELDS struct qobj_node qobj_node
86 /* call these at the end of any _create function (QOBJ_REG)
87 * and beginning of any _destroy function (QOBJ_UNREG) */
88 #define QOBJ_REG(n, structname) qobj_reg(&n->qobj_node, &qobj_t_##structname)
89 #define QOBJ_UNREG(n) qobj_unreg(&n->qobj_node)
91 /* internals - should not be directly used without a good reason
93 * note: qobj_get is essentially never safe to use in MT context because
94 * the object could be deleted by another thread -- and worse, it could be
95 * of the "wrong" type and deleted.
97 * with qobj_get_typed, the type check is done under lock, which means that
98 * it can be used as long as another lock prevents the deletion of objects
99 * of the expected type.
101 * in the long this may need another touch, e.g. built-in per-object locking.
103 void qobj_reg(struct qobj_node
*node
, const struct qobj_nodetype
*type
);
104 void qobj_unreg(struct qobj_node
*node
);
105 struct qobj_node
*qobj_get(uint64_t id
);
106 void *qobj_get_typed(uint64_t id
, const struct qobj_nodetype
*type
);
108 /* type declarations */
109 #define DECLARE_QOBJ_TYPE(structname) \
110 extern const struct qobj_nodetype qobj_t_##structname \
112 #define DEFINE_QOBJ_TYPE(structname) \
113 const struct qobj_nodetype qobj_t_##structname = { \
114 .node_member_offset = \
115 (ptrdiff_t)offsetof(struct structname, qobj_node)} \
117 #define DEFINE_QOBJ_TYPE_INIT(structname, ...) \
118 const struct qobj_nodetype qobj_t_##structname = { \
119 .node_member_offset = \
120 (ptrdiff_t)offsetof(struct structname, qobj_node), \
124 /* ID dereference with typecheck.
125 * will return NULL if id not found or wrong type. */
126 #define QOBJ_GET_TYPESAFE(id, structname) \
127 ((struct structname *)qobj_get_typed((id), &qobj_t_##structname))
129 #define QOBJ_ID(ptr) ((ptr)->qobj_node.nid)
130 #define QOBJ_ID_0SAFE(ptr) \
132 typeof(ptr) _ptr = (ptr); \
133 _ptr ? _ptr->qobj_node.nid : 0ULL; \
136 void qobj_init(void);
137 void qobj_finish(void);