forked from xurxodiz/questcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mygcollector.h
executable file
·47 lines (38 loc) · 944 Bytes
/
mygcollector.h
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
// Due to the peculiarities of the hashed symbol table
// it's required to have a garbage collector keeping track
// of all malloc addresses to prevent memory leaks.
void msg_error_theres_no_garbage();
void msg_error_too_much_garbage();
void gc_makeempty(gcollector *my) {
my->start = 0;
my->end = -1;
my->size = 0;
}
int gc_isempty(gcollector *my) {
return (my->size == 0);
}
int gc_isfull(gcollector *my) {
return (my->size == MAXQUEUE);
}
void gc_push(gcollector *my, void* sy) {
if (gc_isfull(my)) {
msg_error_too_much_garbage();
exit(EXIT_FAILURE);
}
my->end = (my->end+1) % MAXQUEUE;
my->size++;
my->queue[my->end] = sy;
}
void* gc_peek(gcollector *my) {
if (gc_isempty(my)) {
msg_error_theres_no_garbage();
exit(EXIT_FAILURE);
}
return my->queue[my->start];
}
void* gc_pop(gcollector *my) {
void* sy = gc_peek(my);
my->size--;
my->start = (my->start+1) % MAXQUEUE;
return sy;
}