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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
|
#include "v1.h"
/*** allocate.h
***/
# define u3a_v1_botox u3a_v2_botox
# define u3a_v1_box u3a_v2_box
# define u3a_v1_cell u3a_v2_cell
# define u3a_v1_fbox u3a_v2_fbox
# define u3a_v1_fbox_no u3a_v2_fbox_no
# define u3a_v1_into u3a_v2_into
# define u3a_v1_is_cat u3a_v2_is_cat
# define u3a_v1_is_north u3a_v2_is_north
# define u3a_v1_is_pom u3a_v2_is_pom
# define u3a_v1_minimum u3a_v2_minimum
# define u3a_v1_outa u3a_v2_outa
# define u3v1to u3v2to
# define u3v1of u3v2of
/*** hashtable.h
***/
# define u3h_v1_buck u3h_v2_buck
# define u3h_v1_node u3h_v2_node
# define u3h_v1_root u3h_v2_root
# define u3h_v1_slot_is_node u3h_v2_slot_is_node
# define u3h_v1_slot_is_noun u3h_v2_slot_is_noun
# define u3h_v1_slot_to_noun u3h_v2_slot_to_noun
/* u3h_v1_free(): free hashtable.
*/
void
u3h_v1_free_nodes(u3p(u3h_v1_root) har_p);
/*** jets.h
***/
# define u3j_v1_fink u3j_v2_fink
# define u3j_v1_fist u3j_v2_fist
# define u3j_v1_hank u3j_v2_hank
# define u3j_v1_rite u3j_v2_rite
# define u3j_v1_site u3j_v2_site
/*** nock.h
***/
# define u3n_v1_memo u3n_v2_memo
# define u3n_v1_prog u3n_v2_prog
/*** vortex.h
***/
# define u3A_v1 u3A_v2
/*** allocate.c
***/
/* _box_v1_slot(): select the right free list to search for a block.
*/
static c3_w
_box_v1_slot(c3_w siz_w)
{
if ( siz_w < u3a_v1_minimum ) {
return 0;
}
else {
c3_w i_w = 1;
while ( 1 ) {
if ( i_w == u3a_v1_fbox_no ) {
return (i_w - 1);
}
if ( siz_w < 16 ) {
return i_w;
}
siz_w = (siz_w + 1) >> 1;
i_w += 1;
}
}
}
/* _box_v1_make(): construct a box.
*/
static u3a_v1_box*
_box_v1_make(void* box_v, c3_w siz_w, c3_w use_w)
{
u3a_v1_box* box_u = box_v;
c3_w* box_w = box_v;
u3_assert(siz_w >= u3a_v1_minimum);
box_w[0] = siz_w;
box_w[siz_w - 1] = siz_w;
box_u->use_w = use_w;
return box_u;
}
/* _box_v1_attach(): attach a box to the free list.
*/
static void
_box_v1_attach(u3a_v1_box* box_u)
{
u3_assert(box_u->siz_w >= (1 + c3_wiseof(u3a_v1_fbox)));
u3_assert(0 != u3v1of(u3a_v1_fbox, box_u));
{
c3_w sel_w = _box_v1_slot(box_u->siz_w);
u3p(u3a_v1_fbox) fre_p = u3v1of(u3a_v1_fbox, box_u);
u3p(u3a_v1_fbox)* pfr_p = &u3R_v1->all.fre_p[sel_w];
u3p(u3a_v1_fbox) nex_p = *pfr_p;
u3v1to(u3a_v1_fbox, fre_p)->pre_p = 0;
u3v1to(u3a_v1_fbox, fre_p)->nex_p = nex_p;
if ( u3v1to(u3a_v1_fbox, fre_p)->nex_p ) {
u3v1to(u3a_v1_fbox, u3v1to(u3a_v1_fbox, fre_p)->nex_p)->pre_p = fre_p;
}
(*pfr_p) = fre_p;
}
}
/* _box_v1_detach(): detach a box from the free list.
*/
static void
_box_v1_detach(u3a_v1_box* box_u)
{
u3p(u3a_v1_fbox) fre_p = u3v1of(u3a_v1_fbox, box_u);
u3p(u3a_v1_fbox) pre_p = u3v1to(u3a_v1_fbox, fre_p)->pre_p;
u3p(u3a_v1_fbox) nex_p = u3v1to(u3a_v1_fbox, fre_p)->nex_p;
if ( nex_p ) {
if ( u3v1to(u3a_v1_fbox, nex_p)->pre_p != fre_p ) {
u3_assert(!"loom: corrupt");
}
u3v1to(u3a_v1_fbox, nex_p)->pre_p = pre_p;
}
if ( pre_p ) {
if( u3v1to(u3a_v1_fbox, pre_p)->nex_p != fre_p ) {
u3_assert(!"loom: corrupt");
}
u3v1to(u3a_v1_fbox, pre_p)->nex_p = nex_p;
}
else {
c3_w sel_w = _box_v1_slot(box_u->siz_w);
if ( fre_p != u3R_v1->all.fre_p[sel_w] ) {
u3_assert(!"loom: corrupt");
}
u3R_v1->all.fre_p[sel_w] = nex_p;
}
}
/* _box_v1_free(): free and coalesce.
*/
static void
_box_v1_free(u3a_v1_box* box_u)
{
c3_w* box_w = (c3_w *)(void *)box_u;
u3_assert(box_u->use_w != 0);
box_u->use_w -= 1;
if ( 0 != box_u->use_w ) {
return;
}
// we're always migrating a north road, so no need to check for it
{
/* Try to coalesce with the block below.
*/
if ( box_w != u3a_v1_into(u3R_v1->rut_p) ) {
c3_w laz_w = *(box_w - 1);
u3a_v1_box* pox_u = (u3a_v1_box*)(void *)(box_w - laz_w);
if ( 0 == pox_u->use_w ) {
_box_v1_detach(pox_u);
_box_v1_make(pox_u, (laz_w + box_u->siz_w), 0);
box_u = pox_u;
box_w = (c3_w*)(void *)pox_u;
}
}
/* Try to coalesce with the block above, or the wilderness.
*/
if ( (box_w + box_u->siz_w) == u3a_v1_into(u3R_v1->hat_p) ) {
u3R_v1->hat_p = u3a_v1_outa(box_w);
}
else {
u3a_v1_box* nox_u = (u3a_v1_box*)(void *)(box_w + box_u->siz_w);
if ( 0 == nox_u->use_w ) {
_box_v1_detach(nox_u);
_box_v1_make(box_u, (box_u->siz_w + nox_u->siz_w), 0);
}
_box_v1_attach(box_u);
}
}
}
/* u3a_v1_wfree(): free storage.
*/
void
u3a_v1_wfree(void* tox_v)
{
_box_v1_free(u3a_v1_botox(tox_v));
}
/* u3a_v1_free(): free for aligned malloc.
*/
void
u3a_v1_free(void* tox_v)
{
if (NULL == tox_v)
return;
c3_w* tox_w = tox_v;
c3_w pad_w = tox_w[-1];
c3_w* org_w = tox_w - (pad_w + 1);
u3a_v1_wfree(org_w);
}
/* u3a_v1_reclaim(): clear ad-hoc persistent caches to reclaim memory.
*/
void
u3a_v1_reclaim(void)
{
// clear the memoization cache
//
u3h_v1_free_nodes(u3R_v1->cax.har_p);
}
/* _me_v1_lose_north(): lose on a north road.
*/
static void
_me_v1_lose_north(u3_noun dog)
{
top:
{
c3_w* dog_w = u3a_v1_to_ptr(dog);
u3a_v1_box* box_u = u3a_v1_botox(dog_w);
if ( box_u->use_w > 1 ) {
box_u->use_w -= 1;
}
else {
if ( 0 == box_u->use_w ) {
fprintf(stderr, "bail: foul\r\n");
abort();
}
else {
if ( _(u3a_v1_is_pom(dog)) ) {
u3a_v1_cell* dog_u = (void *)dog_w;
u3_noun h_dog = dog_u->hed;
u3_noun t_dog = dog_u->tel;
if ( !_(u3a_v1_is_cat(h_dog)) ) {
_me_v1_lose_north(h_dog);
}
u3a_v1_wfree(dog_w);
if ( !_(u3a_v1_is_cat(t_dog)) ) {
dog = t_dog;
goto top;
}
}
else {
u3a_v1_wfree(dog_w);
}
}
}
}
}
/* u3a_v1_lose(): lose a reference count.
*/
void
u3a_v1_lose(u3_noun som)
{
if ( !_(u3a_v1_is_cat(som)) ) {
_me_v1_lose_north(som);
}
}
/*** hashtable.c
***/
/* _ch_v1_free_buck(): free bucket
*/
static void
_ch_v1_free_buck(u3h_v1_buck* hab_u)
{
c3_w i_w;
for ( i_w = 0; i_w < hab_u->len_w; i_w++ ) {
u3a_v1_lose(u3h_v1_slot_to_noun(hab_u->sot_w[i_w]));
}
u3a_v1_wfree(hab_u);
}
/* _ch_v1_free_node(): free node.
*/
static void
_ch_v1_free_node(u3h_v1_node* han_u, c3_w lef_w)
{
c3_w len_w = c3_pc_w(han_u->map_w);
c3_w i_w;
lef_w -= 5;
for ( i_w = 0; i_w < len_w; i_w++ ) {
c3_w sot_w = han_u->sot_w[i_w];
if ( _(u3h_v1_slot_is_noun(sot_w)) ) {
u3a_v1_lose(u3h_v1_slot_to_noun(sot_w));
}
else {
void* hav_v = u3h_v1_slot_to_node(sot_w);
if ( 0 == lef_w ) {
_ch_v1_free_buck(hav_v);
} else {
_ch_v1_free_node(hav_v, lef_w);
}
}
}
u3a_v1_wfree(han_u);
}
/* u3h_v1_free_nodes(): free hashtable nodes.
*/
void
u3h_v1_free_nodes(u3p(u3h_v1_root) har_p)
{
u3h_v1_root* har_u = u3v1to(u3h_v1_root, har_p);
c3_w i_w;
for ( i_w = 0; i_w < 64; i_w++ ) {
c3_w sot_w = har_u->sot_w[i_w];
if ( _(u3h_v1_slot_is_noun(sot_w)) ) {
u3a_v1_lose(u3h_v1_slot_to_noun(sot_w));
}
else if ( _(u3h_v1_slot_is_node(sot_w)) ) {
u3h_v1_node* han_u = (u3h_v1_node*) u3h_v1_slot_to_node(sot_w);
_ch_v1_free_node(han_u, 25);
}
har_u->sot_w[i_w] = 0;
}
har_u->use_w = 0;
har_u->arm_u.mug_w = 0;
har_u->arm_u.inx_w = 0;
}
/* _ch_v1_walk_buck(): walk bucket for gc.
*/
static void
_ch_v1_walk_buck(u3h_v1_buck* hab_u, void (*fun_f)(u3_noun, void*), void* wit)
{
c3_w i_w;
for ( i_w = 0; i_w < hab_u->len_w; i_w++ ) {
fun_f(u3h_v1_slot_to_noun(hab_u->sot_w[i_w]), wit);
}
}
/* _ch_v1_walk_node(): walk node for gc.
*/
static void
_ch_v1_walk_node(u3h_v1_node* han_u, c3_w lef_w, void (*fun_f)(u3_noun, void*), void* wit)
{
c3_w len_w = c3_pc_w(han_u->map_w);
c3_w i_w;
lef_w -= 5;
for ( i_w = 0; i_w < len_w; i_w++ ) {
c3_w sot_w = han_u->sot_w[i_w];
if ( _(u3h_v1_slot_is_noun(sot_w)) ) {
u3_noun kev = u3h_v1_slot_to_noun(sot_w);
fun_f(kev, wit);
}
else {
void* hav_v = u3h_v1_slot_to_node(sot_w);
if ( 0 == lef_w ) {
_ch_v1_walk_buck(hav_v, fun_f, wit);
} else {
_ch_v1_walk_node(hav_v, lef_w, fun_f, wit);
}
}
}
}
/* u3h_v1_walk_with(): traverse hashtable with key, value fn and data
* argument; RETAINS.
*/
void
u3h_v1_walk_with(u3p(u3h_v1_root) har_p,
void (*fun_f)(u3_noun, void*),
void* wit)
{
u3h_v1_root* har_u = u3v1to(u3h_v1_root, har_p);
c3_w i_w;
for ( i_w = 0; i_w < 64; i_w++ ) {
c3_w sot_w = har_u->sot_w[i_w];
if ( _(u3h_v1_slot_is_noun(sot_w)) ) {
u3_noun kev = u3h_v1_slot_to_noun(sot_w);
fun_f(kev, wit);
}
else if ( _(u3h_v1_slot_is_node(sot_w)) ) {
u3h_v1_node* han_u = (u3h_v1_node*) u3h_v1_slot_to_node(sot_w);
_ch_v1_walk_node(han_u, 25, fun_f, wit);
}
}
}
/* _ch_v1_walk_plain(): use plain u3_noun fun_f for each node
*/
static void
_ch_v1_walk_plain(u3_noun kev, void* wit)
{
void (*fun_f)(u3_noun) = (void (*)(u3_noun))wit;
fun_f(kev);
}
/* u3h_v1_walk(): u3h_v1_walk_with, but with no data argument
*/
void
u3h_v1_walk(u3p(u3h_v1_root) har_p, void (*fun_f)(u3_noun))
{
u3h_v1_walk_with(har_p, _ch_v1_walk_plain, (void *)fun_f);
}
/*** jets.c
***/
/* _cj_fink_free(): lose and free everything in a u3j_v1_fink.
*/
static void
_cj_v1_fink_free(u3p(u3j_v1_fink) fin_p)
{
c3_w i_w;
u3j_v1_fink* fin_u = u3v1to(u3j_v1_fink, fin_p);
u3a_v1_lose(fin_u->sat);
for ( i_w = 0; i_w < fin_u->len_w; ++i_w ) {
u3j_v1_fist* fis_u = &(fin_u->fis_u[i_w]);
u3a_v1_lose(fis_u->bat);
u3a_v1_lose(fis_u->pax);
}
u3a_v1_wfree(fin_u);
}
/* u3j_v1_rite_lose(): lose references of u3j_v1_rite (but do not free).
*/
void
u3j_v1_rite_lose(u3j_v1_rite* rit_u)
{
if ( (c3y == rit_u->own_o) && u3_none != rit_u->clu ) {
u3a_v1_lose(rit_u->clu);
_cj_v1_fink_free(rit_u->fin_p);
}
}
/* u3j_v1_site_lose(): lose references of u3j_v1_site (but do not free).
*/
void
u3j_v1_site_lose(u3j_v1_site* sit_u)
{
u3a_v1_lose(sit_u->axe);
if ( u3_none != sit_u->bat ) {
u3a_v1_lose(sit_u->bat);
}
if ( u3_none != sit_u->bas ) {
u3a_v1_lose(sit_u->bas);
}
if ( u3_none != sit_u->loc ) {
u3a_v1_lose(sit_u->loc);
u3a_v1_lose(sit_u->lab);
if ( c3y == sit_u->fon_o ) {
if ( sit_u->fin_p ) {
_cj_v1_fink_free(sit_u->fin_p);
}
}
}
}
/* _cj_v1_free_hank(): free an entry from the hank cache.
*/
static void
_cj_v1_free_hank(u3_noun kev)
{
u3a_v1_cell* cel_u = (u3a_v1_cell*) u3a_v1_to_ptr(kev);
u3j_v1_hank* han_u = u3v1to(u3j_v1_hank, cel_u->tel);
if ( u3_none != han_u->hax ) {
u3a_v1_lose(han_u->hax);
u3j_v1_site_lose(&(han_u->sit_u));
}
u3a_v1_wfree(han_u);
}
/* u3j_v1_reclaim(): clear ad-hoc persistent caches to reclaim memory.
*/
void
u3j_v1_reclaim(void)
{
// clear the jet hank cache
//
u3h_v1_walk(u3R_v1->jed.han_p, _cj_v1_free_hank);
u3h_v1_free_nodes(u3R_v1->jed.han_p);
}
/*** nock.c
***/
/* _cn_v1_prog_free(): free memory retained by program pog_u
*/
static void
_cn_v1_prog_free(u3n_v1_prog* pog_u)
{
// fix up pointers for loom portability
pog_u->byc_u.ops_y = (c3_y*) ((void*) pog_u) + sizeof(u3n_v1_prog);
pog_u->lit_u.non = (u3_noun*) (pog_u->byc_u.ops_y + pog_u->byc_u.len_w);
pog_u->mem_u.sot_u = (u3n_v1_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w);
pog_u->cal_u.sit_u = (u3j_v1_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w);
pog_u->reg_u.rit_u = (u3j_v1_rite*) (pog_u->cal_u.sit_u + pog_u->cal_u.len_w);
c3_w dex_w;
for (dex_w = 0; dex_w < pog_u->lit_u.len_w; ++dex_w) {
u3a_v1_lose(pog_u->lit_u.non[dex_w]);
}
for (dex_w = 0; dex_w < pog_u->mem_u.len_w; ++dex_w) {
u3a_v1_lose(pog_u->mem_u.sot_u[dex_w].key);
}
for (dex_w = 0; dex_w < pog_u->cal_u.len_w; ++dex_w) {
u3j_v1_site_lose(&(pog_u->cal_u.sit_u[dex_w]));
}
for (dex_w = 0; dex_w < pog_u->reg_u.len_w; ++dex_w) {
u3j_v1_rite_lose(&(pog_u->reg_u.rit_u[dex_w]));
}
u3a_v1_free(pog_u);
}
/* _n_v1_feb(): u3h_v1_walk helper for u3n_v1_free
*/
static void
_n_v1_feb(u3_noun kev)
{
u3a_v1_cell *cel_u = (u3a_v1_cell*) u3a_v1_to_ptr(kev);
_cn_v1_prog_free(u3v1to(u3n_v1_prog, cel_u->tel));
}
/* u3n_v1_free(): free bytecode cache
*/
void
u3n_v1_free(void)
{
u3p(u3h_v1_root) har_p = u3R_v1->byc.har_p;
u3h_v1_walk(har_p, _n_v1_feb);
u3h_v1_free_nodes(har_p);
}
/* u3n_v1_reclaim(): clear ad-hoc persistent caches to reclaim memory.
*/
void
u3n_v1_reclaim(void)
{
// clear the bytecode cache
//
// We can't just u3h_v1_free() -- the value is a post to a u3n_v1_prog.
// Note that the hank cache *must* also be freed (in u3j_v1_reclaim())
//
u3n_v1_free();
}
/*** vortex.c
***/
/* u3v_v1_reclaim(): clear ad-hoc persistent caches to reclaim memory.
*/
void
u3v_v1_reclaim(void)
{
// clear the u3v_wish cache
//
u3a_v1_lose(u3A_v1->yot);
u3A_v1->yot = u3_nul;
}
/*** init
***/
void
u3_v1_load(c3_z wor_i)
{
c3_w len_w = wor_i - 1;
c3_w ver_w = *(u3_Loom_v1 + len_w);
u3_assert( U3V_VER1 == ver_w );
c3_w* mem_w = u3_Loom_v1 + 1;
c3_w siz_w = c3_wiseof(u3v_v1_home);
c3_w* mat_w = (mem_w + len_w) - siz_w;
u3H_v1 = (void *)mat_w;
u3R_v1 = &u3H_v1->rod_u;
u3R_v1->cap_p = u3R_v1->mat_p = u3a_v1_outa(u3H_v1);
}
/*** manage.c
***/
void
u3m_v1_reclaim(void)
{
u3v_v1_reclaim();
u3j_v1_reclaim();
u3n_v1_reclaim();
u3a_v1_reclaim();
}
|