-
Notifications
You must be signed in to change notification settings - Fork 1
/
rc.egl
191 lines (160 loc) · 3.35 KB
/
rc.egl
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
export '*'
extern func memset(any*, int, long) : any*
extern func free(any*)
extern func realloc(any*, long) : any*
extern func memmove(any*, any*, long) : any*
extern func strcmp(byte*, byte*) : int
struct __egl_ptr
{
long memcount
short wrefct
short wrefal
any*** wrefs
[any*, int :]* teardown
}
struct __egl_ptr_ptr
{
__egl_ptr main
__egl_ptr* to
}
struct __egl_class_indir
{
int count
byte** names
long* offsets
any** functions
}
func __egl_print_count(__egl_ptr* ptr)
{
puts ptr->memcount
}
func __egl_prepare(__egl_ptr* ptr)
{
ptr->memcount = 0
ptr->wrefct = 0
ptr->wrefal = 0
ptr->wrefs = nil
ptr->teardown = nil
}
func __egl_decr_no_free(__egl_ptr* ptr)
{
ptr->memcount -= 1;
}
func __egl_incr_ptr(__egl_ptr* ptr)
{
if ptr && ptr->memcount > 0-5
ptr->memcount = ptr->memcount + 1
}
func __egl_set_nil(any*** vals, short ct)
{
for int i = 0; i < ct; i += 1
{
vals[i]! = nil
}
}
func __egl_decr_ptr(__egl_ptr* ptr)
{
if !ptr || ptr->memcount < 0
return
ptr->memcount = ptr->memcount - 1
if ptr->memcount == 0
{
if ptr->wrefs
{
__egl_set_nil(ptr->wrefs, ptr->wrefct)
free(ptr->wrefs)
}
if ptr->teardown
{
ptr->memcount = 0-20
ptr->teardown(ptr, 1)
}
free(ptr)
}
}
func __egl_counted_destructor(__egl_ptr_ptr* ptr, int i)
{
i -- Silence unused variable warning
__egl_decr_ptr(ptr->to)
}
func __egl_check_ptr(__egl_ptr* ptr)
{
if !ptr || ptr->memcount < 0
return
if ptr->memcount == 0
{
if ptr->wrefs
{
__egl_set_nil(ptr->wrefs, ptr->wrefct)
free(ptr->wrefs)
}
if(ptr->teardown)
ptr->teardown(ptr, 1)
free(ptr)
}
}
func __egl_add_weak(__egl_ptr* ptr, any** pos)
{
if !ptr || ptr->memcount < 0
return
if ptr->wrefal == ptr->wrefct
{
ptr->wrefs = realloc(ptr->wrefs, (ptr->wrefct + 10) * sizeof(any*))
ptr->wrefal = ptr->wrefal + 10
}
ptr->wrefs[ptr->wrefct] = pos
ptr->wrefct = ptr->wrefct + 1
}
func __egl_remove_weak(__egl_ptr** pos)
{
if !pos || !pos!
return
__egl_ptr* ptr = pos!
int idx = 0-1
for int i = 0; i < ptr->wrefct; i += 1
{
if ptr->wrefs[i] == pos
{
idx = i
break
}
}
if idx < 0
return
if idx == ptr->wrefct - 1
{
ptr->wrefct = ptr->wrefct - 1
ptr->wrefs[idx] = nil
return
}
memmove(ptr->wrefs + idx, ptr->wrefs + idx + 1, (ptr->wrefct - idx - 1) * sizeof(any*))
ptr->wrefct = ptr->wrefct - 1
ptr->wrefs[ptr->wrefct] = nil
}
func __egl_array_fill_nil(any* arr, long ct)
{
memset(arr, 0, ct * sizeof(any*))
}
func __egl_array_decr_ptrs(any** arr, long ct)
{
for long i = 0; i < ct; i += 1
{
__egl_decr_ptr(arr[i])
}
}
func __egl_lookup_method(__egl_class_indir** obj, byte* name, int index) : any*
{
__egl_class_indir* cls = obj!
long offset = 0-1
for long i = 0; i < cls->count; i += 1
{
if strcmp(name, cls->names[i]) == 0
{
offset = cls->offsets[i]
break
}
}
if offset < 0
return nil
return cls->functions[index + offset]
}