]> git.proxmox.com Git - mirror_frr.git/blob - lib/qobj.c
Merge branch 'stable/3.0'
[mirror_frr.git] / lib / qobj.c
1 /*
2 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
3 *
4 * This file is part of Quagga
5 *
6 * Quagga is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * Quagga is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "thread.h"
24 #include "memory.h"
25 #include "hash.h"
26 #include "log.h"
27 #include "qobj.h"
28
29 static struct hash *nodes = NULL;
30
31 static unsigned int qobj_key (void *data)
32 {
33 struct qobj_node *node = data;
34 return (unsigned int)node->nid;
35 }
36
37 static int qobj_cmp (const void *a, const void *b)
38 {
39 const struct qobj_node *na = a, *nb = b;
40 return na->nid == nb->nid;
41 }
42
43 void qobj_reg(struct qobj_node *node, struct qobj_nodetype *type)
44 {
45 node->type = type;
46 do
47 {
48 node->nid = (uint64_t)random();
49 node->nid ^= (uint64_t)random() << 32;
50 }
51 while (!node->nid || hash_get (nodes, node, hash_alloc_intern) != node);
52 }
53
54 void qobj_unreg(struct qobj_node *node)
55 {
56 hash_release (nodes, node);
57 }
58
59 struct qobj_node *qobj_get(uint64_t id)
60 {
61 struct qobj_node dummy = { .nid = id };
62 return hash_lookup (nodes, &dummy);
63 }
64
65 void *qobj_get_typed(uint64_t id, struct qobj_nodetype *type)
66 {
67 struct qobj_node *node = qobj_get(id);
68 if (!node || node->type != type)
69 return NULL;
70 return (char *)node - node->type->node_member_offset;
71 }
72
73 void qobj_init (void)
74 {
75 if (!nodes)
76 nodes = hash_create (qobj_key, qobj_cmp);
77 }
78
79 void qobj_finish (void)
80 {
81 hash_clean (nodes, NULL);
82 hash_free (nodes);
83 nodes = NULL;
84 }