-
Notifications
You must be signed in to change notification settings - Fork 5
/
comelang.c
174 lines (133 loc) · 3.21 KB
/
comelang.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using comelang;
using unsafe;
void come_gc_init()
{
}
void come_gc_final()
{
}
void ncfree(void* mem)
{
if(mem) {
free(mem);
}
}
void* igc_calloc(size_t count, size_t size)
{
char* mem = calloc(1, sizeof(int)+sizeof(long)+count*size);
int* ref_count = (int*)mem;
(*ref_count)++;
long* size2 = (long*)(mem + sizeof(int));
*size2 = size*count + sizeof(long) + sizeof(int);
return mem + sizeof(int) + sizeof(long);
}
void igc_increment_ref_count(void* mem)
{
if(mem == NULL) {
return;
}
int* ref_count = (int*)((char*)mem - sizeof(int) - sizeof(long));
(*ref_count)++;
}
void print_ref_count(void* mem)
{
if(mem == NULL) {
return;
}
int* ref_count = (int*)((char*)mem - sizeof(int) - sizeof(long));
printf("ref count %d\n", *ref_count);
}
void igc_decrement_ref_count(void* mem)
{
if(mem == NULL) {
return;
}
int* ref_count = (int*)((char*)mem - sizeof(int) - sizeof(long));
(*ref_count)--;
int count = *ref_count;
if(count == 0) {
ncfree(ref_count);
}
}
void ncfree_object(void* mem)
{
if(mem == NULL) {
return;
}
int* ref_count = (int*)((char*)mem - sizeof(int) - sizeof(long));
ncfree(ref_count);
}
void call_finalizer(void* fun, void* mem, int call_finalizer_only)
{
if(mem == NULL) {
return;
}
if(call_finalizer_only) {
if(fun) {
void (*finalizer)(void*) = fun;
finalizer(mem);
}
}
else {
int* ref_count = (int*)((char*)mem - sizeof(int) - sizeof(long));
(*ref_count)--;
int count = *ref_count;
if(count == 0) {
if(mem) {
if(fun) {
void (*finalizer)(void*) = fun;
finalizer(mem);
}
ncfree_object(mem);
}
}
}
}
void* nccalloc(size_t nmemb, size_t size)
{
void* result = calloc(nmemb, size);
return result;
}
void*%? ncmemdup(void*% block)
{
managed block;
if(!block) {
return dummy_heap null;
}
char* mem = (char*)block - sizeof(int) - sizeof(long);
long* size_p = (long*)(mem + sizeof(int));
size_t size = *size_p;
void* ret = calloc(1, size);
int* ref_count = ret;
if (ret) {
char* p = ret;
char* p2 = mem;
while(p - (char*)ret < size) {
*p = *p2;
p++;
p2++;
}
}
(*ref_count) = 1;
long* size_p2 = (long*)((char*)ret + sizeof(int));
*size_p2 = size;
return dummy_heap (char*)ret + sizeof(int) + sizeof(long);
}
void* call_cloner(void* fun, void* mem)
{
if(fun && mem) {
void* (*cloner)(void*) = fun;
return cloner(mem);
}
return null;
}
void unwrap_exception(char* sname, int sline, char* caller_sname, int caller_sline, char* mem)
{
if(mem == null) {
fprintf(stderr, "%s %d: unwrap exception. The value is null\n", sname!, sline);
fprintf(stderr, "%s %d: caller is this point\n", caller_sname!, caller_sline);
}
}