]> git.proxmox.com Git - mirror_frr.git/blob - lib/qobj.c
Merge commit '78986c0' into tmp-3.0-master-merge
[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 pthread_rwlock_t nodes_lock;
30 static struct hash *nodes = NULL;
31
32 static unsigned int qobj_key(void *data)
33 {
34 struct qobj_node *node = data;
35 return (unsigned int)node->nid;
36 }
37
38 static int qobj_cmp(const void *a, const void *b)
39 {
40 const struct qobj_node *na = a, *nb = b;
41 return na->nid == nb->nid;
42 }
43
44 void qobj_reg(struct qobj_node *node, struct qobj_nodetype *type)
45 {
46 node->type = type;
47 pthread_rwlock_wrlock(&nodes_lock);
48 do {
49 node->nid = (uint64_t)random();
50 node->nid ^= (uint64_t)random() << 32;
51 } while (!node->nid
52 || hash_get(nodes, node, hash_alloc_intern) != node);
53 pthread_rwlock_unlock(&nodes_lock);
54 }
55
56 void qobj_unreg(struct qobj_node *node)
57 {
58 pthread_rwlock_wrlock(&nodes_lock);
59 hash_release(nodes, node);
60 pthread_rwlock_unlock(&nodes_lock);
61 }
62
63 struct qobj_node *qobj_get(uint64_t id)
64 {
65 struct qobj_node dummy = {.nid = id}, *rv;
66 pthread_rwlock_rdlock(&nodes_lock);
67 rv = hash_lookup(nodes, &dummy);
68 pthread_rwlock_unlock(&nodes_lock);
69 return rv;
70 }
71
72 void *qobj_get_typed(uint64_t id, struct qobj_nodetype *type)
73 {
74 struct qobj_node dummy = {.nid = id};
75 struct qobj_node *node;
76 void *rv;
77
78 pthread_rwlock_rdlock(&nodes_lock);
79 node = hash_lookup(nodes, &dummy);
80
81 /* note: we explicitly hold the lock until after we have checked the
82 * type.
83 * if the caller holds a lock that for example prevents the deletion of
84 * route-maps, we can still race against a delete of something that
85 * isn't
86 * a route-map. */
87 if (!node || node->type != type)
88 rv = NULL;
89 else
90 rv = (char *)node - node->type->node_member_offset;
91
92 pthread_rwlock_unlock(&nodes_lock);
93 return rv;
94 }
95
96 void qobj_init(void)
97 {
98 if (!nodes) {
99 pthread_rwlock_init(&nodes_lock, NULL);
100 nodes = hash_create(qobj_key, qobj_cmp, NULL);
101 }
102 }
103
104 void qobj_finish(void)
105 {
106 hash_clean(nodes, NULL);
107 hash_free(nodes);
108 nodes = NULL;
109 pthread_rwlock_destroy(&nodes_lock);
110 }