forked from urbit/vere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
allocate.c
2904 lines (2533 loc) · 65.9 KB
/
allocate.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
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// @file
#include "allocate.h"
#include "hashtable.h"
#include "log.h"
#include "manage.h"
#include "options.h"
#include "retrieve.h"
#include "trace.h"
#include "vortex.h"
u3_road* u3a_Road;
#ifdef U3_MEMORY_DEBUG
c3_w u3_Code;
#endif
c3_w u3a_to_pug(c3_w off);
c3_w u3a_to_pom(c3_w off);
void
u3a_drop(const u3a_pile* pil_u);
void*
u3a_peek(const u3a_pile* pil_u);
void*
u3a_pop(const u3a_pile* pil_u);
void*
u3a_push(const u3a_pile* pil_u);
c3_o
u3a_pile_done(const u3a_pile* pil_u);
/* _box_count(): adjust memory count.
*/
#ifdef U3_CPU_DEBUG
static void
_box_count(c3_ws siz_ws)
{
u3R->all.fre_w += siz_ws;
{
c3_w end_w = u3a_heap(u3R);
c3_w all_w = (end_w - u3R->all.fre_w);
if ( all_w > u3R->all.max_w ) {
u3R->all.max_w = all_w;
}
}
}
#else
static void
_box_count(c3_ws siz_ws) { }
#endif
/* _box_vaal(): validate box alignment. no-op without C3DBG
TODO: I think validation code that might be compiled out like this,
_box_count, (others?) should have perhaps its own header and certainly its
own prefix. having to remind yourself that _box_count doesn't actually do
anything unless U3_CPU_DEBUG is defined is annoying. */
#define _box_vaal(box_u) \
do { \
c3_dessert(((uintptr_t)u3a_boxto(box_u) \
& u3a_balign-1) == 0); \
c3_dessert((((u3a_box*)(box_u))->siz_w \
& u3a_walign-1) == 0); \
} while(0)
/* _box_slot(): select the right free list to search for a block.
**
** siz_w == 6 words then [0]
** siz_w < 16 then [1]
** siz_w < 32 then [2]
** siz_w < 64 then [3]
** ...
** siz_w >= 2GB then [26]
*/
static c3_w
_box_slot(c3_w siz_w)
{
if ( u3a_minimum == siz_w ) {
return 0;
}
else if ( !(siz_w >> 4) ) {
c3_dessert( u3a_minimum < siz_w );
return 1;
}
else {
c3_w bit_w = c3_bits_word(siz_w) - 3;
c3_w max_w = u3a_fbox_no - 1;
return c3_min(bit_w, max_w);
}
}
/* _box_make(): construct a box.
box_v - start addr of box
siz_w - size of allocated space adjacent to block
use_w - box's refcount
*/
static u3a_box*
_box_make(void* box_v, c3_w siz_w, c3_w use_w)
{
u3a_box* box_u = box_v;
c3_w* box_w = box_v;
u3_assert(siz_w >= u3a_minimum);
box_u->siz_w = siz_w;
box_w[siz_w - 1] = siz_w; /* stor size at end of allocation as well */
box_u->use_w = use_w;
_box_vaal(box_u);
# ifdef U3_MEMORY_DEBUG
box_u->cod_w = u3_Code;
box_u->eus_w = 0;
# endif
return box_u;
}
/* _box_attach(): attach a box to the free list.
*/
static void
_box_attach(u3a_box* box_u)
{
u3_assert(box_u->siz_w >= (1 + c3_wiseof(u3a_fbox)));
u3_assert(0 != u3of(u3a_fbox, box_u));
#if 0
// For debugging, fill the box with beef.
{
c3_w* box_w = (void *)box_u;
c3_w i_w;
for ( i_w = c3_wiseof(u3a_box); (i_w + 1) < box_u->siz_w; i_w++ ) {
box_w[i_w] = 0xdeadbeef;
}
}
#endif
_box_count(box_u->siz_w);
{
c3_w sel_w = _box_slot(box_u->siz_w);
u3p(u3a_fbox) fre_p = u3of(u3a_fbox, box_u);
u3p(u3a_fbox)* pfr_p = &u3R->all.fre_p[sel_w];
u3p(u3a_fbox) nex_p = *pfr_p;
u3to(u3a_fbox, fre_p)->pre_p = 0;
u3to(u3a_fbox, fre_p)->nex_p = nex_p;
if ( u3to(u3a_fbox, fre_p)->nex_p ) {
u3to(u3a_fbox, u3to(u3a_fbox, fre_p)->nex_p)->pre_p = fre_p;
}
(*pfr_p) = fre_p;
}
}
/* _box_detach(): detach a box from the free list.
*/
static void
_box_detach(u3a_box* box_u)
{
u3p(u3a_fbox) fre_p = u3of(u3a_fbox, box_u);
u3p(u3a_fbox) pre_p = u3to(u3a_fbox, fre_p)->pre_p;
u3p(u3a_fbox) nex_p = u3to(u3a_fbox, fre_p)->nex_p;
_box_count(-(box_u->siz_w));
if ( nex_p ) {
if ( u3to(u3a_fbox, nex_p)->pre_p != fre_p ) {
u3_assert(!"loom: corrupt");
}
u3to(u3a_fbox, nex_p)->pre_p = pre_p;
}
if ( pre_p ) {
if( u3to(u3a_fbox, pre_p)->nex_p != fre_p ) {
u3_assert(!"loom: corrupt");
}
u3to(u3a_fbox, pre_p)->nex_p = nex_p;
}
else {
c3_w sel_w = _box_slot(box_u->siz_w);
if ( fre_p != u3R->all.fre_p[sel_w] ) {
u3_assert(!"loom: corrupt");
}
u3R->all.fre_p[sel_w] = nex_p;
}
}
/* _box_free(): free and coalesce.
*/
static void
_box_free(u3a_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;
}
_box_vaal(box_u);
#if 0
/* Clear the contents of the block, for debugging.
*/
{
c3_w i_w;
for ( i_w = c3_wiseof(u3a_box); (i_w + 1) < box_u->siz_w; i_w++ ) {
box_w[i_w] = 0xdeadbeef;
}
}
#endif
if ( c3y == u3a_is_north(u3R) ) { /* north */
/* Try to coalesce with the block below.
*/
if ( box_w != u3a_into(u3R->rut_p) ) {
c3_w laz_w = *(box_w - 1); /* the size of a box stored at the end of its allocation */
u3a_box* pox_u = (u3a_box*)(void *)(box_w - laz_w); /* the head of the adjacent box below */
if ( 0 == pox_u->use_w ) {
_box_detach(pox_u);
_box_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_into(u3R->hat_p) ) {
u3R->hat_p = u3a_outa(box_w);
}
else {
u3a_box* nox_u = (u3a_box*)(void *)(box_w + box_u->siz_w);
if ( 0 == nox_u->use_w ) {
_box_detach(nox_u);
_box_make(box_u, (box_u->siz_w + nox_u->siz_w), 0);
}
_box_attach(box_u);
}
} /* end north */
else { /* south */
/* Try to coalesce with the block above.
*/
if ( (box_w + box_u->siz_w) != u3a_into(u3R->rut_p) ) {
u3a_box* nox_u = (u3a_box*)(void *)(box_w + box_u->siz_w);
if ( 0 == nox_u->use_w ) {
_box_detach(nox_u);
_box_make(box_u, (box_u->siz_w + nox_u->siz_w), 0);
}
}
/* Try to coalesce with the block below, or with the wilderness.
*/
if ( box_w == u3a_into(u3R->hat_p) ) {
u3R->hat_p = u3a_outa(box_w + box_u->siz_w);
}
else {
c3_w laz_w = box_w[-1];
u3a_box* pox_u = (u3a_box*)(void *)(box_w - laz_w);
if ( 0 == pox_u->use_w ) {
_box_detach(pox_u);
_box_make(pox_u, (laz_w + box_u->siz_w), 0);
box_u = pox_u;
}
_box_attach(box_u);
}
} /* end south */
}
/* _ca_box_make_hat(): in u3R, allocate directly on the hat.
*/
static u3a_box*
_ca_box_make_hat(c3_w len_w, c3_w ald_w, c3_w off_w, c3_w use_w)
{
c3_w
pad_w, /* padding between returned pointer and box */
siz_w; /* total size of allocation */
u3_post
box_p, /* start of box */
all_p; /* start of returned pointer */
if ( c3y == u3a_is_north(u3R) ) {
box_p = all_p = u3R->hat_p;
all_p += c3_wiseof(u3a_box) + off_w;
pad_w = c3_align(all_p, ald_w, C3_ALGHI)
- all_p;
siz_w = c3_align(len_w + pad_w, u3a_walign, C3_ALGHI);
// hand-inlined: siz_w >= u3a_open(u3R)
//
if ( (siz_w >= (u3R->cap_p - u3R->hat_p)) ) {
return 0;
}
u3R->hat_p += siz_w;
}
else {
box_p = all_p = u3R->hat_p - len_w;
all_p += c3_wiseof(u3a_box) + off_w;
pad_w = all_p
- c3_align(all_p, ald_w, C3_ALGLO);
siz_w = c3_align(len_w + pad_w, u3a_walign, C3_ALGHI);
// hand-inlined: siz_w >= u3a_open(u3R)
//
if ( siz_w >= (u3R->hat_p - u3R->cap_p) ) {
return 0;
}
box_p = u3R->hat_p -= siz_w;
}
c3_dessert(!(ald_w <= 2 && off_w == 0) || (0 == pad_w));
c3_dessert(pad_w <= 4);
return _box_make(u3a_into(box_p), siz_w, use_w);
}
#if 0
/* _me_road_all_hat(): in u3R, allocate directly on the hat.
*/
static u3a_box*
_ca_box_make_hat(c3_w len_w, c3_w alm_w, c3_w use_w)
{
return _box_make(_me_road_all_hat(len_w), len_w, use_w);
}
#endif
#if 0 // not yet used
/* _me_road_all_cap(): in u3R, allocate directly on the cap.
*/
static c3_w*
_me_road_all_cap(c3_w len_w)
{
if ( len_w > u3a_open(u3R) ) {
u3m_bail(c3__meme); return 0;
}
if ( c3y == u3a_is_north(u3R) ) {
u3R->cap_p -= len_w;
return u3a_into(u3R->cap_p);
}
else {
u3_post all_p;
all_p = u3R->cap_p;
u3R->cap_p += len_w;
return u3a_into(all_p);
}
}
#endif
#if 0
/* u3a_sane(): check allocator sanity.
*/
void
u3a_sane(void)
{
c3_w i_w;
for ( i_w = 0; i_w < u3a_fbox_no; i_w++ ) {
u3a_fbox* fre_u = u3R->all.fre_u[i_w];
while ( fre_u ) {
if ( fre_u == u3R->all.fre_u[i_w] ) {
u3_assert(fre_u->pre_u == 0);
}
else {
u3_assert(fre_u->pre_u != 0);
u3_assert(fre_u->pre_u->nex_u == fre_u);
if ( fre_u->nex_u != 0 ) {
u3_assert(fre_u->nex_u->pre_u == fre_u);
}
}
fre_u = fre_u->nex_u;
}
}
}
#endif
/* u3a_reflux(): dump 1K cells from the cell list into regular memory.
*/
void
u3a_reflux(void)
{
c3_w i_w;
for ( i_w = 0; u3R->all.cel_p && (i_w < 1024); i_w++ ) {
u3_post cel_p = u3R->all.cel_p;
u3a_box* box_u = &(u3to(u3a_fbox, cel_p)->box_u);
u3R->all.cel_p = u3to(u3a_fbox, cel_p)->nex_p;
// otherwise _box_free() will double-count it
//
_box_count(-(u3a_minimum));
_box_free(box_u);
}
}
/* _ca_reclaim_half(): reclaim from memoization cache.
*/
static void
_ca_reclaim_half(void)
{
// XX u3l_log avoid here, as it can
// cause problems when handling errors
if ( (0 == u3R->cax.har_p) ||
(0 == u3to(u3h_root, u3R->cax.har_p)->use_w) )
{
fprintf(stderr, "allocate: reclaim: memo cache: empty\r\n");
u3m_bail(c3__meme);
}
#if 1
fprintf(stderr, "allocate: reclaim: half of %d entries\r\n",
u3to(u3h_root, u3R->cax.har_p)->use_w);
u3h_trim_to(u3R->cax.har_p, u3to(u3h_root, u3R->cax.har_p)->use_w / 2);
#else
/* brutal and guaranteed effective
*/
u3h_free(u3R->cax.har_p);
u3R->cax.har_p = u3h_new();
#endif
}
/* _ca_willoc(): u3a_walloc() internals.
*/
static void*
_ca_willoc(c3_w len_w, c3_w ald_w, c3_w off_w)
{
c3_w siz_w = c3_max(u3a_minimum, u3a_boxed(len_w));
c3_w sel_w = _box_slot(siz_w);
/* XX: this logic is totally bizarre, but preserve it.
**
** This means we use the next size bigger instead of the "correct"
** size. For example, a 20 word allocation will be freed into free
** list 2 but will be allocated from free list 3.
**
** This is important to preserve because the sequential search may be
** very slow. On a real-world task involving many compilations,
** removing this line made this function appear in ~80% of samples.
**
** For reference, this was added in cgyarvin/urbit ffed9e748d8f6c.
*/
if ( (sel_w != 0) && (sel_w != u3a_fbox_no - 1) ) {
sel_w += 1;
}
// u3l_log("walloc %d: *pfr_p %x", len_w, u3R->all.fre_p[sel_w]);
while ( 1 ) {
u3p(u3a_fbox) *pfr_p = &u3R->all.fre_p[sel_w];
while ( 1 ) {
/* increment until we get a non-null freelist */
if ( 0 == *pfr_p ) {
if ( sel_w < (u3a_fbox_no - 1) ) {
sel_w += 1;
break;
}
else {
// nothing in top free list; chip away at the hat
//
u3a_box* box_u;
// memory nearly empty; reclaim; should not be needed
//
// if ( (u3a_open(u3R) + u3R->all.fre_w) < 65536 ) { _ca_reclaim_half(); }
box_u = _ca_box_make_hat(siz_w, ald_w, off_w, 1);
/* Flush a bunch of cell cache, then try again.
*/
if ( 0 == box_u ) {
if ( u3R->all.cel_p ) {
u3a_reflux();
return _ca_willoc(len_w, ald_w, off_w);
}
else {
_ca_reclaim_half();
return _ca_willoc(len_w, ald_w, off_w);
}
}
else return u3a_boxto(box_u);
}
}
else { /* we got a non-null freelist */
u3_post box_p, all_p;
box_p = all_p = *pfr_p;
all_p += c3_wiseof(u3a_box) + off_w;
c3_w pad_w = c3_align(all_p, ald_w, C3_ALGHI) - all_p;
c3_w des_w = c3_align(siz_w + pad_w, u3a_walign, C3_ALGHI);
/* calls maximally requesting DWORD alignment of returned pointer
shouldn't require padding. */
c3_dessert(!(ald_w <= 2 && off_w == 0) || (0 == pad_w));
c3_dessert(pad_w <= 4);
if ( (des_w) > u3to(u3a_fbox, *pfr_p)->box_u.siz_w ) {
/* This free block is too small. Continue searching.
*/
pfr_p = &(u3to(u3a_fbox, *pfr_p)->nex_p);
continue;
}
else { /* free block fits desired alloc size */
u3a_box* box_u = &(u3to(u3a_fbox, *pfr_p)->box_u);
/* We have found a free block of adequate size. Remove it
** from the free list.
*/
_box_count(-(box_u->siz_w));
/* misc free list consistency checks.
TODO: in the future should probably only run for C3DBG builds */
{
if ( (0 != u3to(u3a_fbox, *pfr_p)->pre_p) &&
(u3to(u3a_fbox, u3to(u3a_fbox, *pfr_p)->pre_p)->nex_p
!= (*pfr_p)) )
{ /* this->pre->nex isn't this */
u3_assert(!"loom: corrupt");
}
if( (0 != u3to(u3a_fbox, *pfr_p)->nex_p) &&
(u3to(u3a_fbox, u3to(u3a_fbox, *pfr_p)->nex_p)->pre_p
!= (*pfr_p)) )
{ /* this->nex->pre isn't this */
u3_assert(!"loom: corrupt");
}
/* pop the block */
/* this->nex->pre = this->pre */
if ( 0 != u3to(u3a_fbox, *pfr_p)->nex_p ) {
u3to(u3a_fbox, u3to(u3a_fbox, *pfr_p)->nex_p)->pre_p =
u3to(u3a_fbox, *pfr_p)->pre_p;
}
/* this = this->nex */
*pfr_p = u3to(u3a_fbox, *pfr_p)->nex_p;
}
/* If we can chop off another block, do it.
*/
if ( (des_w + u3a_minimum) <= box_u->siz_w ) {
/* Split the block.
*/
/* XXX: Despite the fact that we're making a box here, we don't
actually have to ensure it's aligned, since des_w and all boxes
already on the loom /are/ aligned. A debug break here implies
that you broke those conditions, not that this needs to handle
alignment. abandon hope. */
c3_w* box_w = ((c3_w *)(void *)box_u);
c3_w* end_w = box_w + des_w;
c3_w lef_w = (box_u->siz_w - des_w);
_box_attach(_box_make(end_w, lef_w, 0));
return u3a_boxto(_box_make(box_w, des_w, 1));
}
else {
u3_assert(0 == box_u->use_w);
box_u->use_w = 1;
#ifdef U3_MEMORY_DEBUG
box_u->cod_w = u3_Code;
#endif
return u3a_boxto(box_u);
}
}
}
}
}
}
/* _ca_walloc(): u3a_walloc() internals.
- len_w: allocation length in words
- ald_w: desired alignment. N.B. the void * returned is not guaranteed to be
aligned on this value. But the allocation will be sized such that the
caller can independently align the value.
- off_w: alignment offset to use when sizing request.
void * returned guaranteed to be DWORD (8-byte) aligned.
*/
static void*
_ca_walloc(c3_w len_w, c3_w ald_w, c3_w off_w)
{
void* ptr_v;
for (;;) {
ptr_v = _ca_willoc(len_w, ald_w, off_w);
if ( 0 != ptr_v ) {
break;
}
_ca_reclaim_half();
}
_box_vaal(u3a_botox(ptr_v));
return ptr_v;
}
/* u3a_walloc(): allocate storage words on hat heap.
*/
void*
u3a_walloc(c3_w len_w)
{
void* ptr_v;
ptr_v = _ca_walloc(len_w, 1, 0);
#if 0
if ( (703 == u3_Code) &&
u3a_botox(ptr_v) == (u3a_box*)(void *)0x200dfe3e4 ) {
static int xuc_i;
u3l_log("xuc_i %d", xuc_i);
if ( 1 == xuc_i ) {
u3a_box* box_u = u3a_botox(ptr_v);
box_u->cod_w = 999;
}
xuc_i++;
}
#endif
_box_vaal(u3a_botox(ptr_v));
return ptr_v;
}
/* u3a_wealloc(): realloc in words.
*/
void*
u3a_wealloc(void* lag_v, c3_w len_w)
{
if ( !lag_v ) {
return u3a_walloc(len_w);
}
else {
u3a_box* box_u = u3a_botox(lag_v);
c3_w* old_w = lag_v;
c3_w tiz_w = c3_min(box_u->siz_w, len_w);
{
c3_w* new_w = u3a_walloc(len_w);
c3_w i_w;
for ( i_w = 0; i_w < tiz_w; i_w++ ) {
new_w[i_w] = old_w[i_w];
}
u3a_wfree(lag_v);
return new_w;
}
}
}
/* u3a_pile_prep(): initialize stack control.
*/
void
u3a_pile_prep(u3a_pile* pil_u, c3_w len_w)
{
// frame size, in words
//
c3_w wor_w = (len_w + 3) >> 2;
c3_o nor_o = u3a_is_north(u3R);
pil_u->mov_ws = (c3y == nor_o) ? -wor_w : wor_w;
pil_u->off_ws = (c3y == nor_o) ? 0 : -wor_w;
pil_u->top_p = u3R->cap_p;
#ifdef U3_MEMORY_DEBUG
pil_u->rod_u = u3R;
#endif
}
/* u3a_wfree(): free storage.
*/
void
u3a_wfree(void* tox_v)
{
_box_free(u3a_botox(tox_v));
}
/* u3a_wtrim(): trim storage.
old_w - old length
len_w - new length
*/
void
u3a_wtrim(void* tox_v, c3_w old_w, c3_w len_w)
{
c3_w* nov_w = tox_v;
if ( (old_w > len_w)
&& ((old_w - len_w) >= u3a_minimum) )
{
u3a_box* box_u = u3a_botox(nov_w);
c3_w* box_w = (void*)u3a_botox(nov_w);
c3_w* end_w = c3_align(nov_w + len_w + 1, /* +1 for trailing allocation size */
u3a_balign,
C3_ALGHI);
c3_w asz_w = (end_w - box_w); /* total size in words of new allocation */
if (box_u->siz_w <= asz_w) return;
c3_w bsz_w = box_u->siz_w - asz_w; /* size diff in words between old and new */
c3_dessert(asz_w && ((asz_w & u3a_walign-1) == 0)); /* new allocation size must be non-zero and DWORD multiple */
c3_dessert(end_w < (box_w + box_u->siz_w)); /* desired alloc end must not exceed existing boundaries */
c3_dessert(((uintptr_t)end_w & u3a_balign-1) == 0); /* address of box getting freed must be DWORD aligned */
c3_dessert((bsz_w & u3a_walign-1) == 0); /* size of box getting freed must be DWORD multiple */
_box_attach(_box_make(end_w, bsz_w, 0)); /* free the unneeded space */
box_u->siz_w = asz_w;
box_w[asz_w - 1] = asz_w;
}
}
/* u3a_calloc(): allocate and zero-initialize array
*/
void*
u3a_calloc(size_t num_i, size_t len_i)
{
size_t byt_i = num_i * len_i;
c3_w* out_w;
u3_assert(byt_i / len_i == num_i);
out_w = u3a_malloc(byt_i);
memset(out_w, 0, byt_i);
return out_w;
}
/* u3a_malloc(): aligned storage measured in bytes.
Internally pads allocations to 16-byte alignment independent of DWORD
alignment ensured for word sized allocations.
*/
void*
u3a_malloc(size_t len_i)
{
c3_w len_w = (c3_w)((len_i + 3) >> 2);
c3_w *ptr_w = _ca_walloc(len_w +1, 4, 1); /* +1 for word storing pad size */
c3_w *out_w = c3_align(ptr_w + 1, 16, C3_ALGHI);
c3_w pad_w = u3a_outa(out_w) - u3a_outa(ptr_w);
out_w[-1] = pad_w - 1; /* the size of the pad doesn't include the word storing the size (-1) */
c3_dessert(&out_w[len_w] /* alloced space after alignment is sufficient */
<= &((c3_w*)u3a_botox(ptr_w))[u3a_botox(ptr_w)->siz_w]);
c3_dessert(pad_w <= 4 && pad_w > 0);
c3_dessert(&out_w[-1] > ptr_w);
return out_w;
}
/* u3a_cellblock(): allocate a block of cells on the hat.
XXX beware when we stop boxing cells and QWORD align references. Alignment
not guaranteed to be preserved after a call.
*/
static c3_o
u3a_cellblock(c3_w num_w)
{
u3p(u3a_fbox) fre_p;
c3_w i_w;
if ( c3y == u3a_is_north(u3R) ) {
if ( u3R->cap_p <= (u3R->hat_p + (num_w * u3a_minimum) + (1 << u3a_page)) ) {
return c3n;
}
else {
u3_post cel_p = u3R->all.cel_p;
u3_post hat_p = u3R->hat_p;
u3R->hat_p += (num_w * u3a_minimum);
for ( i_w = 0; i_w < num_w; i_w++) {
u3_post all_p = hat_p;
void* box_v = u3a_into(all_p);
u3a_box* box_u = box_v;
c3_w* box_w = box_v;
// hand inline of _box_make(u3a_into(all_p), u3a_minimum, 1)
{
box_u->siz_w = u3a_minimum;
box_w[u3a_minimum - 1] = u3a_minimum;
box_u->use_w = 1;
#ifdef U3_MEMORY_DEBUG
box_u->cod_w = 0;
box_u->eus_w = 0;
#endif
}
hat_p += u3a_minimum;
fre_p = u3of(u3a_fbox, box_u);
u3to(u3a_fbox, fre_p)->nex_p = cel_p;
cel_p = fre_p;
}
u3R->all.cel_p = cel_p;
}
}
else {
if ( (u3R->cap_p + (num_w * u3a_minimum) + (1 << u3a_page)) >= u3R->hat_p ) {
return c3n;
}
else {
u3_post cel_p = u3R->all.cel_p;
u3_post hat_p = u3R->hat_p;
u3R->hat_p -= (num_w * u3a_minimum);
for ( i_w = 0; i_w < num_w; i_w++ ) {
u3_post all_p = (hat_p -= u3a_minimum);
void* box_v = u3a_into(all_p);
u3a_box* box_u = box_v;
c3_w* box_w = box_v;
// hand inline of _box_make(u3a_into(all_p), u3a_minimum, 1);
{
box_u->siz_w = u3a_minimum;
box_w[u3a_minimum - 1] = u3a_minimum;
box_u->use_w = 1;
# ifdef U3_MEMORY_DEBUG
box_u->cod_w = 0;
box_u->eus_w = 0;
# endif
}
fre_p = u3of(u3a_fbox, box_u);
u3to(u3a_fbox, fre_p)->nex_p = cel_p;
cel_p = fre_p;
}
u3R->all.cel_p = cel_p;
}
}
_box_count(num_w * u3a_minimum);
return c3y;
}
/* u3a_celloc(): allocate a cell.
XXX beware when we stop boxing cells and QWORD align references
*/
c3_w*
u3a_celloc(void)
{
#ifdef U3_CPU_DEBUG
u3R->pro.cel_d++;
#endif
#ifdef U3_MEMORY_DEBUG
if ( u3C.wag_w & u3o_debug_ram ) {
return u3a_walloc(c3_wiseof(u3a_cell));
}
#endif
u3p(u3a_fbox) cel_p;
if ( !(cel_p = u3R->all.cel_p) ) {
if ( u3R == &(u3H->rod_u) ) {
// no cell allocator on home road
//
return u3a_walloc(c3_wiseof(u3a_cell));
}
else {
if ( c3n == u3a_cellblock(4096) ) {
return u3a_walloc(c3_wiseof(u3a_cell));
}
cel_p = u3R->all.cel_p;
}
}
{
u3a_box* box_u = &(u3to(u3a_fbox, cel_p)->box_u);
box_u->use_w = 1;
u3R->all.cel_p = u3to(u3a_fbox, cel_p)->nex_p;
_box_count(-(u3a_minimum));
return u3a_boxto(box_u);
}
}
/* u3a_cfree(): free a cell.
*/
void
u3a_cfree(c3_w* cel_w)
{
#ifdef U3_MEMORY_DEBUG
if ( u3C.wag_w & u3o_debug_ram ) {
return u3a_wfree(cel_w);
}
#endif
if ( u3R == &(u3H->rod_u) ) {
return u3a_wfree(cel_w);
}
else {
u3a_box* box_u = u3a_botox(cel_w);
u3p(u3a_fbox) fre_p = u3of(u3a_fbox, box_u);
_box_count(u3a_minimum);
u3to(u3a_fbox, fre_p)->nex_p = u3R->all.cel_p;
u3R->all.cel_p = fre_p;
}
}
/* u3a_realloc(): aligned realloc in bytes.
*/
void*
u3a_realloc(void* lag_v, size_t len_i)
{
if ( !lag_v ) {
return u3a_malloc(len_i);
}
else {
c3_w len_w = (c3_w)((len_i + 3) >> 2);
c3_w* lag_w = lag_v;
c3_w pad_w = lag_w[-1];
c3_w* org_w = lag_w - (pad_w + 1);
u3a_box* box_u = u3a_botox((void *)org_w);
c3_w* old_w = lag_v;
c3_w tiz_w = c3_min(box_u->siz_w, len_w);
{
c3_w* new_w = u3a_malloc(len_i);
c3_w i_w;
for ( i_w = 0; i_w < tiz_w; i_w++ ) {
new_w[i_w] = old_w[i_w];
}
u3a_wfree(org_w);
return new_w;
}
}
}
/* u3a_free(): free for aligned malloc.
*/
void
u3a_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);
// u3l_log("free %p %p", org_w, tox_w);
u3a_wfree(org_w);
}
/* _me_wash_north(): clean up mug slots after copy.
*/
static void _me_wash_north(u3_noun dog);
static void
_me_wash_north_in(u3_noun som)
{
if ( _(u3a_is_cat(som)) ) return;
if ( !_(u3a_north_is_junior(u3R, som)) ) return;
_me_wash_north(som);
}
static void
_me_wash_north(u3_noun dog)
{
u3_assert(c3y == u3a_is_dog(dog));
// u3_assert(c3y == u3a_north_is_junior(u3R, dog));
{
u3a_noun* dog_u = u3a_to_ptr(dog);
if ( dog_u->mug_w == 0 ) return;
dog_u->mug_w = 0; // power wash
// if ( dog_u->mug_w >> 31 ) { dog_u->mug_w = 0; }
if ( _(u3a_is_pom(dog)) ) {
u3a_cell* god_u = (u3a_cell *)(void *)dog_u;
_me_wash_north_in(god_u->hed);
_me_wash_north_in(god_u->tel);
}
}
}
/* _me_wash_south(): clean up mug slots after copy.
*/
static void _me_wash_south(u3_noun dog);
static void
_me_wash_south_in(u3_noun som)
{