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 #include "eal_private.h"
13
14 static struct rte_class_list rte_class_list =
15 TAILQ_HEAD_INITIALIZER(rte_class_list);
16
17 void
rte_class_register(struct rte_class * class)18 rte_class_register(struct rte_class *class)
19 {
20 RTE_VERIFY(class);
21 RTE_VERIFY(class->name && strlen(class->name));
22
23 TAILQ_INSERT_TAIL(&rte_class_list, class, next);
24 EAL_LOG(DEBUG, "Registered [%s] device class.", class->name);
25 }
26
27 void
rte_class_unregister(struct rte_class * class)28 rte_class_unregister(struct rte_class *class)
29 {
30 TAILQ_REMOVE(&rte_class_list, class, next);
31 EAL_LOG(DEBUG, "Unregistered [%s] device class.", class->name);
32 }
33
34 struct rte_class *
rte_class_find(const struct rte_class * start,rte_class_cmp_t cmp,const void * data)35 rte_class_find(const struct rte_class *start, rte_class_cmp_t cmp,
36 const void *data)
37 {
38 struct rte_class *cls;
39
40 if (start != NULL)
41 cls = TAILQ_NEXT(start, next);
42 else
43 cls = TAILQ_FIRST(&rte_class_list);
44 while (cls != NULL) {
45 if (cmp(cls, data) == 0)
46 break;
47 cls = TAILQ_NEXT(cls, next);
48 }
49 return cls;
50 }
51
52 static int
cmp_class_name(const struct rte_class * class,const void * _name)53 cmp_class_name(const struct rte_class *class, const void *_name)
54 {
55 const char *name = _name;
56
57 return strcmp(class->name, name);
58 }
59
60 struct rte_class *
rte_class_find_by_name(const char * name)61 rte_class_find_by_name(const char *name)
62 {
63 return rte_class_find(NULL, cmp_class_name, (const void *)name);
64 }
65