diff options
Diffstat (limited to 'task-list.c')
-rw-r--r-- | task-list.c | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/task-list.c b/task-list.c new file mode 100644 index 0000000..4c820bf --- /dev/null +++ b/task-list.c | |||
@@ -0,0 +1,99 @@ | |||
1 | #include "task-list.h" | ||
2 | |||
3 | static guint get_task_hash_key(gint pid) | ||
4 | { | ||
5 | return trace_hash(pid) % TASK_HASH_SIZE; | ||
6 | } | ||
7 | |||
8 | struct task_list *find_task_hash(struct task_list **tasks, | ||
9 | gint key, gint pid) | ||
10 | { | ||
11 | struct task_list *list; | ||
12 | |||
13 | for (list = tasks[key]; list; list = list->next) { | ||
14 | if (list->pid == pid) | ||
15 | return list; | ||
16 | } | ||
17 | |||
18 | return NULL; | ||
19 | } | ||
20 | |||
21 | /** | ||
22 | * find_task_list - return task_list node for pid, or NULL if not present | ||
23 | */ | ||
24 | struct task_list *find_task_list(struct task_list **tasks, gint pid) | ||
25 | { | ||
26 | guint key = get_task_hash_key(pid); | ||
27 | return find_task_hash(tasks, key, pid); | ||
28 | } | ||
29 | |||
30 | /** | ||
31 | * add_task_hash - add pid to a task_list | ||
32 | * @tasks: The head of the task_list | ||
33 | * @pid: The pid to add | ||
34 | * | ||
35 | * Return the list entry of the added task | ||
36 | */ | ||
37 | struct task_list *add_task_hash(struct task_list **tasks, int pid) | ||
38 | { | ||
39 | struct task_list *list; | ||
40 | guint key = get_task_hash_key(pid); | ||
41 | |||
42 | list = find_task_hash(tasks, key, pid); | ||
43 | if (list) | ||
44 | return list; | ||
45 | |||
46 | list = malloc_or_die(sizeof(*list)); | ||
47 | list->pid = pid; | ||
48 | list->next = tasks[key]; | ||
49 | tasks[key] = list; | ||
50 | |||
51 | return list; | ||
52 | } | ||
53 | |||
54 | /** | ||
55 | * free_task_hash - free all nodes in a task_list | ||
56 | */ | ||
57 | void free_task_hash(struct task_list **tasks) | ||
58 | { | ||
59 | struct task_list *list; | ||
60 | int i; | ||
61 | |||
62 | for (i = 0; i < TASK_HASH_SIZE; i++) { | ||
63 | while (tasks[i]) { | ||
64 | list = tasks[i]; | ||
65 | tasks[i] = list->next; | ||
66 | free(list); | ||
67 | } | ||
68 | } | ||
69 | } | ||
70 | |||
71 | /** | ||
72 | * task_list_pids - return an allocated list of all found tasks | ||
73 | * @ginfo: The graph info structure | ||
74 | * | ||
75 | * Returns an allocated list of pids found in the graph, ending | ||
76 | * with a -1. This array must be freed with free(). | ||
77 | */ | ||
78 | gint *task_list_pids(struct task_list **tasks) | ||
79 | { | ||
80 | struct task_list *list; | ||
81 | gint *pids; | ||
82 | gint count = 0; | ||
83 | gint i; | ||
84 | |||
85 | for (i = 0; i < TASK_HASH_SIZE; i++) { | ||
86 | list = tasks[i]; | ||
87 | while (list) { | ||
88 | if (count) | ||
89 | pids = realloc(pids, sizeof(*pids) * (count + 2)); | ||
90 | else | ||
91 | pids = malloc(sizeof(*pids) * 2); | ||
92 | pids[count++] = list->pid; | ||
93 | pids[count] = -1; | ||
94 | list = list->next; | ||
95 | } | ||
96 | } | ||
97 | |||
98 | return pids; | ||
99 | } | ||