drm/panel: panel-himax-hx83102: support for csot-pna957qt1-1 MIPI-DSI panel
[drm/drm-misc.git] / tools / perf / util / syscalltbl.c
blob928aca4cd6e9f2f26c5c4fd825b4538c064a4cc3
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * System call table mapper
5 * (C) 2016 Arnaldo Carvalho de Melo <acme@redhat.com>
6 */
8 #include "syscalltbl.h"
9 #include <stdlib.h>
10 #include <linux/compiler.h>
11 #include <linux/zalloc.h>
13 #include <string.h>
14 #include "string2.h"
16 #include <syscall_table.h>
17 const int syscalltbl_native_max_id = SYSCALLTBL_MAX_ID;
18 static const char *const *syscalltbl_native = syscalltbl;
20 struct syscall {
21 int id;
22 const char *name;
25 static int syscallcmpname(const void *vkey, const void *ventry)
27 const char *key = vkey;
28 const struct syscall *entry = ventry;
30 return strcmp(key, entry->name);
33 static int syscallcmp(const void *va, const void *vb)
35 const struct syscall *a = va, *b = vb;
37 return strcmp(a->name, b->name);
40 static int syscalltbl__init_native(struct syscalltbl *tbl)
42 int nr_entries = 0, i, j;
43 struct syscall *entries;
45 for (i = 0; i <= syscalltbl_native_max_id; ++i)
46 if (syscalltbl_native[i])
47 ++nr_entries;
49 entries = tbl->syscalls.entries = malloc(sizeof(struct syscall) * nr_entries);
50 if (tbl->syscalls.entries == NULL)
51 return -1;
53 for (i = 0, j = 0; i <= syscalltbl_native_max_id; ++i) {
54 if (syscalltbl_native[i]) {
55 entries[j].name = syscalltbl_native[i];
56 entries[j].id = i;
57 ++j;
61 qsort(tbl->syscalls.entries, nr_entries, sizeof(struct syscall), syscallcmp);
62 tbl->syscalls.nr_entries = nr_entries;
63 tbl->syscalls.max_id = syscalltbl_native_max_id;
64 return 0;
67 struct syscalltbl *syscalltbl__new(void)
69 struct syscalltbl *tbl = malloc(sizeof(*tbl));
70 if (tbl) {
71 if (syscalltbl__init_native(tbl)) {
72 free(tbl);
73 return NULL;
76 return tbl;
79 void syscalltbl__delete(struct syscalltbl *tbl)
81 zfree(&tbl->syscalls.entries);
82 free(tbl);
85 const char *syscalltbl__name(const struct syscalltbl *tbl __maybe_unused, int id)
87 return id <= syscalltbl_native_max_id ? syscalltbl_native[id]: NULL;
90 int syscalltbl__id(struct syscalltbl *tbl, const char *name)
92 struct syscall *sc = bsearch(name, tbl->syscalls.entries,
93 tbl->syscalls.nr_entries, sizeof(*sc),
94 syscallcmpname);
96 return sc ? sc->id : -1;
99 int syscalltbl__id_at_idx(struct syscalltbl *tbl, int idx)
101 struct syscall *syscalls = tbl->syscalls.entries;
103 return idx < tbl->syscalls.nr_entries ? syscalls[idx].id : -1;
106 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
108 int i;
109 struct syscall *syscalls = tbl->syscalls.entries;
111 for (i = *idx + 1; i < tbl->syscalls.nr_entries; ++i) {
112 if (strglobmatch(syscalls[i].name, syscall_glob)) {
113 *idx = i;
114 return syscalls[i].id;
118 return -1;
121 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
123 *idx = -1;
124 return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx);