-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtl.c
84 lines (71 loc) · 1.68 KB
/
tl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <linux/slab.h>
#include <linux/string.h>
#include "tl.h"
static struct task *task_append(struct tasklist *tl, const char *name, size_t len) {
struct task *t;
t = kzalloc(sizeof(*t), GFP_KERNEL);
if (!t) {
pr_err("failed to create task with name '%s'\n", name);
goto ret;
}
strncpy(t->name, name, TASK_NAME_LEN-1);
t->len = len;
if (!tl->head)
tl->head = t;
else
tl->tail->next = t;
t->prev = tl->tail;
tl->tail = t;
tl->tlen += len;
tl->count++;
ret:
return t;
}
static void task_delete(struct tasklist *tl, const char *name) {
struct task *t = tl->head, *prev, *next;
while (t && strcmp(t->name, name) != 0)
t = t->next;
if (t) {
prev = t->prev;
next = t->next;
if (prev)
prev->next = next;
if (next)
next->prev = prev;
if (t == tl->head)
tl->head = next;
if (t == tl->tail)
tl->tail = prev;
tl->tlen -= t->len;
tl->count--;
kfree(t);
}
}
void tasklist_print_ordered(struct tasklist *tl) {
int i = 0;
struct task *t = tl->head;
while (t) {
pr_info("[%d] %s\n", i, t->name);
t = t->next;
i++;
}
}
int tasklist_destroy(struct tasklist *tl) {
int i = 0;
struct task *t, *next;
t = tl->head;
while (t) {
next = t->next;
kfree(t);
t = next;
i++;
}
return i;
}
int add_handler(struct tasklist *tl, const char *name) {
return (!(task_append(tl, name, strlen(name)) != NULL));
}
int delete_handler(struct tasklist *tl, const char *name) {
task_delete(tl, name);
return 0;
}