]> git.proxmox.com Git - mirror_frr.git/blobdiff - lib/qobj.h
isisd: implemented the 'sequence-number-skipped' notification
[mirror_frr.git] / lib / qobj.h
index 4a5c0c01ed3ad007ceac04aa0b3831660a70a18b..b701eeec5fadd221e9a7be7f80de45961911b473 100644 (file)
@@ -1,23 +1,17 @@
 /*
  * Copyright (c) 2015-16  David Lamparter, for NetDEF, Inc.
  *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
  *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
 #ifndef _QOBJ_H
  * this is intentional to prevent the struct from growing beyond the allocated
  * space.
  */
-#define RESERVED_SPACE_STRUCT(name, fieldname, size) \
-       struct { \
-               struct name fieldname; \
-               char padding ## fieldname[size - sizeof(struct name)]; \
+#define RESERVED_SPACE_STRUCT(name, fieldname, size)                           \
+       struct {                                                               \
+               struct name fieldname;                                         \
+               char padding##fieldname[size - sizeof(struct name)];           \
        };
 
 /* don't need struct definitions for these here.  code actually using
  */
 #ifndef HAVE_QOBJ_NODETYPE_CLI
 #define HAVE_QOBJ_NODETYPE_CLI -1
-struct qobj_nodetype_cli { int dummy; };
+struct qobj_nodetype_cli {
+       int dummy;
+};
 #endif
 
 #ifndef HAVE_QOBJ_NODETYPE_CAPNP
 #define HAVE_QOBJ_NODETYPE_CAPNP -1
-struct qobj_nodetype_capnp { int dummy; };
+struct qobj_nodetype_capnp {
+       int dummy;
+};
 #endif
 
 /* each different kind of object will have a global variable of this type,
@@ -77,44 +75,54 @@ struct qobj_node {
        struct qobj_nodetype *type;
 };
 
-#define QOBJ_FIELDS \
-       struct qobj_node qobj_node;
+#define QOBJ_FIELDS struct qobj_node qobj_node;
 
 /* call these at the end of any _create function (QOBJ_REG)
  * and beginning of any _destroy function (QOBJ_UNREG) */
-#define QOBJ_REG(n, structname) \
-       qobj_reg(&n->qobj_node, &qobj_t_ ## structname)
-#define QOBJ_UNREG(n) \
-       qobj_unreg(&n->qobj_node)
+#define QOBJ_REG(n, structname) qobj_reg(&n->qobj_node, &qobj_t_##structname)
+#define QOBJ_UNREG(n) qobj_unreg(&n->qobj_node)
 
-/* internals - should not be directly used without a good reason*/
+/* internals - should not be directly used without a good reason
+ *
+ * note: qobj_get is essentially never safe to use in MT context because
+ * the object could be deleted by another thread -- and worse, it could be
+ * of the "wrong" type and deleted.
+ *
+ * with qobj_get_typed, the type check is done under lock, which means that
+ * it can be used as long as another lock prevents the deletion of objects
+ * of the expected type.
+ *
+ * in the long this may need another touch, e.g. built-in per-object locking.
+ */
 void qobj_reg(struct qobj_node *node, struct qobj_nodetype *type);
 void qobj_unreg(struct qobj_node *node);
 struct qobj_node *qobj_get(uint64_t id);
 void *qobj_get_typed(uint64_t id, struct qobj_nodetype *type);
 
 /* type declarations */
-#define DECLARE_QOBJ_TYPE(structname) \
-       extern struct qobj_nodetype qobj_t_ ## structname;
-#define DEFINE_QOBJ_TYPE(structname) \
-       struct qobj_nodetype qobj_t_ ## structname = { \
-               .node_member_offset = \
-                       (ptrdiff_t)offsetof(struct structname, qobj_node) \
-       };
-#define DEFINE_QOBJ_TYPE_INIT(structname, ...) \
-       struct qobj_nodetype qobj_t_ ## structname = { \
-               .node_member_offset = \
-                       (ptrdiff_t)offsetof(struct structname, qobj_node), \
-               __VA_ARGS__ \
-       };
+#define DECLARE_QOBJ_TYPE(structname)                                          \
+       extern struct qobj_nodetype qobj_t_##structname;
+#define DEFINE_QOBJ_TYPE(structname)                                           \
+       struct qobj_nodetype qobj_t_##structname = {                           \
+               .node_member_offset =                                          \
+                       (ptrdiff_t)offsetof(struct structname, qobj_node)};
+#define DEFINE_QOBJ_TYPE_INIT(structname, ...)                                 \
+       struct qobj_nodetype qobj_t_##structname = {                           \
+               .node_member_offset =                                          \
+                       (ptrdiff_t)offsetof(struct structname, qobj_node),     \
+               __VA_ARGS__};
 
 /* ID dereference with typecheck.
  * will return NULL if id not found or wrong type. */
-#define QOBJ_GET_TYPESAFE(id, structname) \
-       ((struct structname *)qobj_get_typed((id), &qobj_t_ ## structname))
+#define QOBJ_GET_TYPESAFE(id, structname)                                      \
+       ((struct structname *)qobj_get_typed((id), &qobj_t_##structname))
 
-#define QOBJ_ID(ptr) \
-       ((ptr)->qobj_node.nid)
+#define QOBJ_ID(ptr) ((ptr)->qobj_node.nid)
+#define QOBJ_ID_0SAFE(ptr)                                                     \
+       ({                                                                     \
+               typeof(ptr) _ptr = (ptr);                                      \
+               _ptr ? _ptr->qobj_node.nid : 0ULL;                             \
+       })
 
 void qobj_init(void);
 void qobj_finish(void);