-
Notifications
You must be signed in to change notification settings - Fork 11
/
tracer.c
63 lines (47 loc) · 1.01 KB
/
tracer.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
#include <search.h>
#include <stdlib.h> /* malloc */
#include <stdio.h> /* fprintf */
#include "tracer.h"
#include "process.h"
struct tracer
{
void *processes;
};
/*
Should return error if such process already here
*/
int tracer_add_process (struct tracer *tracer, struct process *process)
{
void *tmp;
if (!(tmp = tsearch (process, &tracer->processes, compare_processes)))
{
fprintf (stderr, "Process adding failure\n");
return -1;
}
if (*(struct process **) tmp != process)
{
fprintf (stderr, "Process already exists\n");
return -1;
}
return 0;
}
struct tracer *tracer_alloc (void)
{
struct tracer *tracer;
if (!(tracer = malloc (sizeof (*tracer))))
{
perror ("malloc");
return NULL;
}
tracer->processes = NULL;
return tracer;
}
static void _process_destroy (void *arg)
{
process_destroy ((struct process *) arg);
}
void tracer_destroy (struct tracer *tracer)
{
tdestroy (tracer->processes, _process_destroy);
free (tracer);
}