-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.c
More file actions
executable file
·68 lines (63 loc) · 1.48 KB
/
Copy pathmemory.c
File metadata and controls
executable file
·68 lines (63 loc) · 1.48 KB
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
#include "common.h"
//the memory
#define MEMORY 0x100000
//#define MEMORY 0x800
struct cons_cell *the_cons_cells;
struct cons_cell *free_cons_cells;
cellpoint free_point = 1;
//memory init
void memory_init(void)
{
the_cons_cells = (struct cons_cell *)malloc(sizeof(struct cons_cell) * MEMORY);
if (the_cons_cells == NULL){
printf("Error: malloc error. -- INITIAL.\n");
exit(-1);
}
free_cons_cells = (struct cons_cell *)malloc(sizeof(struct cons_cell) * MEMORY);
if (free_cons_cells == NULL){
printf("Error: malloc error. -- INITIAL.\n");
exit(-1);
}
}
boolean memory_enough(int k)
{
return (free_point + k) < MEMORY;
}
//basic operation: cons
cellpoint cons(uint addr, uint decre)
{
if(!memory_enough(1)){
//store the args
car_set(allocons, addr);
cdr_set(allocons, decre);
//call garbage collection
garbage_collection();
//restore the args
addr = car(allocons);
decre = cdr(allocons);
}
the_cons_cells[free_point].car = addr;
the_cons_cells[free_point].cdr = decre;
// printf("%d\n", free_point);
return free_point++;
}
//basic operation: car
uint car(cellpoint cp)
{
return the_cons_cells[cp].car;
}
//basic operation: cdr
uint cdr(cellpoint cp)
{
return the_cons_cells[cp].cdr;
}
//basic operation: car_set!
void car_set(cellpoint cp, uint ele)
{
the_cons_cells[cp].car = ele;
}
//basic operation: cdr_set!
void cdr_set(cellpoint cp, uint ele)
{
the_cons_cells[cp].cdr = ele;
}