]> git.proxmox.com Git - mirror_frr.git/blob - lib/qobj.c
Merge pull request #5 from freerangerouting/dev/osr/fixes_plus_vty_keywords
[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
17 * along with Quagga; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22 #include <zebra.h>
23
24 #include "thread.h"
25 #include "memory.h"
26 #include "hash.h"
27 #include "log.h"
28 #include "qobj.h"
29
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 do
48 {
49 node->nid = (uint64_t)random();
50 node->nid ^= (uint64_t)random() << 32;
51 }
52 while (!node->nid || hash_get (nodes, node, hash_alloc_intern) != node);
53 }
54
55 void qobj_unreg(struct qobj_node *node)
56 {
57 hash_release (nodes, node);
58 }
59
60 struct qobj_node *qobj_get(uint64_t id)
61 {
62 struct qobj_node dummy = { .nid = id };
63 return hash_lookup (nodes, &dummy);
64 }
65
66 void *qobj_get_typed(uint64_t id, struct qobj_nodetype *type)
67 {
68 struct qobj_node *node = qobj_get(id);
69 if (!node || node->type != type)
70 return NULL;
71 return (char *)node - node->type->node_member_offset;
72 }
73
74 void qobj_init (void)
75 {
76 if (!nodes)
77 nodes = hash_create (qobj_key, qobj_cmp);
78 }
79
80 void qobj_finish (void)
81 {
82 hash_clean (nodes, NULL);
83 hash_free (nodes);
84 nodes = NULL;
85 }