-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmem.c
92 lines (67 loc) · 1.68 KB
/
mem.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
85
86
87
88
89
90
91
92
#include "mem.h"
void free_memory(void) {
if (DEBUG) printf("freeing memory...\n");
free_lists();
free_envs();
}
/* lists */
/* list of lists allocated */
List_list* lists_head;
List_list* lists_tail;
void free_lists(void) {
if (DEBUG) printf("freeing lists...\n");
if (lists_head == NULL) return;
List_list* temp = lists_head;
lists_head = lists_head->next;
free_list(&(temp->list));
free_lists();
}
void free_list(List** list) {
if (*list == NULL) return;
List* temp = *list;
*list = (*list)->cdr;
free(temp);
temp = NULL;
free_list(list);
}
void append_to_lists(List* list) {
if (lists_tail) lists_tail = lists_tail->next;
lists_tail = malloc(sizeof(List_list));
lists_tail->list = list;
lists_tail->next = NULL;
if (!lists_head) lists_head = lists_tail;
}
/* envs */
/* list of envs established */
Env_list* envs_head;
Env_list* envs_tail;
void free_envs(void) {
if (DEBUG) printf("freeing envs...\n");
if (envs_head == NULL) return;
Env_list* temp = envs_head;
envs_head = envs_head->next;
free_env(&(temp->env));
free_envs();
}
void free_env(Env** env) {
if (*env == NULL) return;
Frame* temp = (*env)->frame;
free(*env);
*env = NULL;
free_frame(&temp);
}
void free_frame(Frame** frame) {
if (*frame == NULL) return;
Frame* temp = (*frame)->next;
free(*frame);
*frame = NULL;
free_frame(&temp);
}
void append_to_envs(Env* env) {
if (envs_tail) envs_tail = envs_tail->next;
envs_tail = malloc(sizeof(Env_list));
envs_tail->env = env;
envs_tail->next = NULL;
if (!envs_head) envs_head = envs_tail;
}
/* tokens freed in parse.c */