-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmalloc.c
151 lines (134 loc) · 3.73 KB
/
malloc.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "malloc.h"
#include <assert.h>
static struct bool bpool[2] = {{BOOL, 0}, {BOOL, 1}};
struct bool *alloc_bool(int value) {
if (value)
return bpool + 1;
else
return bpool + 0;
}
struct symbol *alloc_err_msg(const char *fmt, ...) {
struct symbol *rtn = alloc_symbol("");
va_list args;
va_start(args, fmt);
vsnprintf((char *)rtn->sym, SYMBOL_LEN, fmt, args);
va_end(args);
return rtn;
}
struct environ *alloc_environ(struct environ *parent) {
struct environ *rtn = (struct environ *)GC_MALLOC(sizeof(struct environ));
assert(rtn);
rtn->parent = parent;
rtn->repo = NULL;
return rtn;
}
struct tree_node *alloc_tree_node(struct tree_node *parent, struct tree_node **pparent,
struct symbol *sym, struct exp *e) {
struct tree_node *rtn = (struct tree_node *)GC_MALLOC(sizeof(struct tree_node));
assert(rtn != NULL);
rtn->color = RED; /* makes insert easier */
rtn->parent = parent;
rtn->pparent = pparent;
rtn->left = NULL;
rtn->right = NULL;
rtn->sym = sym;
rtn->value = e;
return rtn;
}
struct symbol *alloc_symbol(const char *str) {
struct symbol *rtn = (struct symbol *)GC_MALLOC_ATOMIC(sizeof(struct symbol));
assert(rtn);
rtn->tag = SYMBOL;
strcpy((char *)rtn->sym, str);
return rtn;
}
struct number *alloc_long(long value) {
struct number *rtn = (struct number *)GC_MALLOC_ATOMIC(sizeof(struct number));
assert(rtn);
rtn->tag = NUMBER;
rtn->type = LONG;
rtn->l_value = value;
return rtn;
}
struct number *alloc_double(double value) {
struct number *rtn = (struct number *)GC_MALLOC_ATOMIC(sizeof(struct number));
assert(rtn);
rtn->tag = NUMBER;
rtn->type = DOUBLE;
rtn->d_value = value;
return rtn;
}
struct pair *alloc_pair(struct exp *car, struct exp *cdr) {
struct pair *rtn = (struct pair *)GC_MALLOC(sizeof(struct pair));
assert(rtn);
rtn->tag = PAIR;
rtn->car = car;
rtn->cdr = cdr;
return rtn;
}
struct stack_frame *alloc_stack_frame(struct stack_frame *prev) {
struct stack_frame *rtn = (struct stack_frame *)GC_MALLOC(sizeof(struct stack_frame));
assert(rtn);
rtn->prev = prev;
rtn->head = NULL;
rtn->tail = &rtn->head;
rtn->dot_list = 0;
return rtn;
}
struct stack *alloc_stack(struct stack *prev) {
struct stack *rtn = (struct stack *)GC_MALLOC(sizeof(struct stack));
assert(rtn);
rtn->prev = prev;
rtn->env = NULL;
return rtn;
}
struct quote_stack *alloc_quote_stack(struct quote_stack *prev) {
struct quote_stack *rtn = (struct quote_stack *)GC_MALLOC(sizeof(struct quote_stack));
assert(rtn);
rtn->prev = prev;
rtn->nest = 0;
return rtn;
}
struct dot_stack *alloc_dot_stack(struct dot_stack *prev) {
struct dot_stack *rtn = (struct dot_stack *)GC_MALLOC(sizeof(struct dot_stack));
assert(rtn);
rtn->prev = prev;
rtn->dot_nest = 0;
return rtn;
}
struct callable *alloc_builtin_pro(builtin_pro_f fun) {
struct callable *rtn = GC_MALLOC_ATOMIC(sizeof(struct callable));
assert(rtn);
rtn->tag = CALLABLE;
rtn->type = BUILTIN_PRO;
rtn->bp_value = fun;
return rtn;
}
struct callable *alloc_builtin_syntax(builtin_syntax_f fun) {
struct callable *rtn = GC_MALLOC_ATOMIC(sizeof(struct callable));
assert(rtn);
rtn->tag = CALLABLE;
rtn->type = BUILTIN_SYNTAX;
rtn->bs_value = fun;
return rtn;
}
struct callable *alloc_lambda(struct pair *pars, struct pair *body, struct environ *bind) {
struct callable *rtn = GC_MALLOC(sizeof(struct callable));
assert(rtn);
rtn->tag = CALLABLE;
rtn->type = LAMBDA;
rtn->u_value.pars = pars;
rtn->u_value.body = body;
rtn->u_value.bind = bind;
return rtn;
}
struct callable *alloc_macro(struct pair *pars, struct pair *body, struct environ *bind) {
struct callable *rtn = GC_MALLOC(sizeof(struct callable));
assert(rtn);
rtn->tag = CALLABLE;
rtn->type = MACRO;
rtn->u_value.pars = pars;
rtn->u_value.body = body;
rtn->u_value.bind = bind;
return rtn;
}