1 /* $OpenBSD: name2id.c,v 1.4 2015/01/22 17:42:09 reyk Exp $ */
4 * Copyright (c) 2004, 2005 Henning Brauer <henning@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
15 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/queue.h>
28 #define IDVAL_MAX 50000
31 TAILQ_ENTRY(n2id_label) entry;
37 TAILQ_HEAD(n2id_labels, n2id_label);
39 u_int16_t _name2id(struct n2id_labels *, const char *);
40 const char *_id2name(struct n2id_labels *, u_int16_t);
41 void _unref(struct n2id_labels *, u_int16_t);
42 void _ref(struct n2id_labels *, u_int16_t);
44 struct n2id_labels relay_labels = TAILQ_HEAD_INITIALIZER(relay_labels);
45 struct n2id_labels relay_tags = TAILQ_HEAD_INITIALIZER(relay_tags);
48 tag_name2id(const char *name)
50 return (_name2id(&relay_tags, name));
54 tag_id2name(u_int16_t id)
56 return (_id2name(&relay_tags, id));
60 tag_unref(u_int16_t id)
62 _unref(&relay_tags, id);
68 _ref(&relay_tags, id);
72 label_name2id(const char *name)
74 return (_name2id(&relay_labels, name));
78 label_id2name(u_int16_t id)
80 return (_id2name(&relay_labels, id));
84 label_unref(u_int16_t id)
86 _unref(&relay_labels, id);
90 label_ref(u_int16_t id)
92 _ref(&relay_labels, id);
96 _name2id(struct n2id_labels *head, const char *name)
98 struct n2id_label *label, *p = NULL;
106 TAILQ_FOREACH(label, head, entry)
107 if (strcmp(name, label->name) == 0) {
113 * to avoid fragmentation, we do a linear search from the beginning
114 * and take the first free slot we find. if there is none or the list
115 * is empty, append a new entry at the end.
118 if (!TAILQ_EMPTY(head))
119 for (p = TAILQ_FIRST(head); p != NULL &&
120 p->id == new_id; p = TAILQ_NEXT(p, entry))
123 if (new_id > IDVAL_MAX) {
128 if ((label = calloc(1, sizeof(struct n2id_label))) == NULL)
130 if ((label->name = strdup(name)) == NULL) {
137 if (p != NULL) /* insert new entry before p */
138 TAILQ_INSERT_BEFORE(p, label, entry);
139 else /* either list empty or no free slot in between */
140 TAILQ_INSERT_TAIL(head, label, entry);
146 _id2name(struct n2id_labels *head, u_int16_t id)
148 struct n2id_label *label;
153 TAILQ_FOREACH(label, head, entry)
155 return (label->name);
161 _unref(struct n2id_labels *head, u_int16_t id)
163 struct n2id_label *p, *next;
168 for (p = TAILQ_FIRST(head); p != NULL; p = next) {
169 next = TAILQ_NEXT(p, entry);
172 TAILQ_REMOVE(head, p, entry);
182 _ref(struct n2id_labels *head, u_int16_t id)
184 struct n2id_label *label;
189 TAILQ_FOREACH(label, head, entry)
190 if (label->id == id) {