New upstream version 18.11-rc1
[deb_dpdk.git] / lib / librte_eal / common / eal_common_class.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018 GaĆ«tan Rivet
3  */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <sys/queue.h>
8
9 #include <rte_class.h>
10 #include <rte_debug.h>
11
12 static struct rte_class_list rte_class_list =
13         TAILQ_HEAD_INITIALIZER(rte_class_list);
14
15 __rte_experimental void
16 rte_class_register(struct rte_class *class)
17 {
18         RTE_VERIFY(class);
19         RTE_VERIFY(class->name && strlen(class->name));
20
21         TAILQ_INSERT_TAIL(&rte_class_list, class, next);
22         RTE_LOG(DEBUG, EAL, "Registered [%s] device class.\n", class->name);
23 }
24
25 __rte_experimental void
26 rte_class_unregister(struct rte_class *class)
27 {
28         TAILQ_REMOVE(&rte_class_list, class, next);
29         RTE_LOG(DEBUG, EAL, "Unregistered [%s] device class.\n", class->name);
30 }
31
32 __rte_experimental
33 struct rte_class *
34 rte_class_find(const struct rte_class *start, rte_class_cmp_t cmp,
35                const void *data)
36 {
37         struct rte_class *cls;
38
39         if (start != NULL)
40                 cls = TAILQ_NEXT(start, next);
41         else
42                 cls = TAILQ_FIRST(&rte_class_list);
43         while (cls != NULL) {
44                 if (cmp(cls, data) == 0)
45                         break;
46                 cls = TAILQ_NEXT(cls, next);
47         }
48         return cls;
49 }
50
51 static int
52 cmp_class_name(const struct rte_class *class, const void *_name)
53 {
54         const char *name = _name;
55
56         return strcmp(class->name, name);
57 }
58
59 __rte_experimental
60 struct rte_class *
61 rte_class_find_by_name(const char *name)
62 {
63         return rte_class_find(NULL, cmp_class_name, (const void *)name);
64 }