File: alloc.c

package info (click to toggle)
aribas 1.64-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 1,928 kB
  • sloc: ansic: 26,078; pascal: 384; asm: 201; lisp: 133; makefile: 45; sh: 1
file content (1044 lines) | stat: -rw-r--r-- 26,140 bytes parent folder | download | duplicates (2)
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
863
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
/****************************************************************/
/* file alloc.c

ARIBAS interpreter for Arithmetic
Copyright (C) 1996-2002 O.Forster

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Address of the author

    Otto Forster
    Math. Institut der LMU
    Theresienstr. 39
    D-80333 Muenchen, Germany

Email   [email protected]
*/
/****************************************************************/

/*
** alloc.c
** memory allocation and garbage collection functions
**
** date of last change
** 1995-03-29
** 1996-10-04   changed iniconfig
** 1997-04-18   various SIZE defines, changed mvsymtab, tempfree
** 1997-09-06   memory allocation #ifdef M_LARGE
** 1997-12-26   small changes in iniconfig
** 1998-01-06   fixed small bug in moveobj
** 2002-04-05   changed some configuration constants
** 2010-01-30   ArrayStack
*/

#include "common.h"

/*-------------------------------------------------------------*/
/* configuration constants */

#define SMALL_BLOCK0SIZE     4000   /* unit is sizeof(truc) = 4 */
#define MED_BLOCK0SIZE       8000
#define BIG_BLOCK0SIZE      16000
#define SMALL_BLOCKSIZE     12240   /* multiple of 255 */
#define MED_BLOCKSIZE       16320   /* multiple of 255, < 2**14 */
#define BIG_BLOCKSIZE       65280   /* multiple of 255, < 2**16 */

#ifdef M_SMALL

#define HASHTABSIZE  509    /* size of hash table (prime) */
#define BLOCKMAX      16
#define ARIBUFSIZE  5000    /* size of bignum buffer (word2's) */
#define BLOCK0SIZE  SMALL_BLOCK0SIZE
#define BLOCKSIZE   SMALL_BLOCKSIZE
#define RESERVE     6000    /* soviel Bytes sollen freibleiben */
#define ARRAYSTKSIZE SMALL_BLOCKSIZE
#define WORKSTKSIZE 6000    /* size of evaluation work stack (word4's) */
#define ARGSTKSIZE  7000    /* size of argument save stack (word4's) */

#endif

#ifdef M_LARGE
#include <assert.h>

#define HASHTABSIZE     1009    /* size of hash table (prime) */
#define BLOCKMAX         200    /* must be < 255 */
#define BLOCK0SIZE     BIG_BLOCK0SIZE
#define BLOCKSIZE      BIG_BLOCKSIZE
#define RESERVE        BIG_BLOCK0SIZE
#define WORKSTKSIZE    BIG_BLOCK0SIZE
#define ARRAYSTKSIZE   BIG_BLOCKSIZE
#define ARGSTKSIZE     BIG_BLOCK0SIZE

#ifdef MEM
#if (MEM >= 1) && (MEM <= 32)
#define MEM_DEFAULT    (MEM*1024)
#endif
#endif /* MEM */
#ifdef INTSIZE
#if (INTSIZE >= 20) && (INTSIZE <= 300)
#define ARIBUFSIZE      (INTSIZE * 209)
#endif
#else
#define ARIBUFSIZE      10000   /* size of bignum buffer (word2's) */
#endif /* INTSIZE */
#endif /* M_LARGE */


#ifndef MEM_DEFAULT

#ifdef ATARIST
#define MEM_DEFAULT 512
#endif
#ifdef MsDOS
#define MEM_DEFAULT 300
#endif
#ifdef DjGPP
#define MEM_DEFAULT 2048
#endif
#ifdef MsWIN32
#define MEM_DEFAULT 2048
#endif
#ifdef genUNiX
#define MEM_DEFAULT 2048
#endif

#endif /* MEM_DEFAULT */

/*-------------------------------------------------------------*/

PUBLIC truc *Symbol;
PUBLIC truc *Memory[BLOCKMAX 1];
PUBLIC trucptr *Symtab;     /* symbol table */
PUBLIC size_t hashtabSize;
PUBLIC truc *WorkStack;     /* evaluation stack (also work stack) */
PUBLIC truc *ArrayStack, *ArrayStkCeil;
PUBLIC truc *evalStkPtr, *workStkPtr, *arrayStkPtr;
PUBLIC truc *ArgStack;      /* argument stack (also save stack) */
PUBLIC truc *argStkPtr, *saveStkPtr;
PUBLIC truc *basePtr;

PUBLIC word2 *AriBuf, *PrimTab;
PUBLIC word2 *AriScratch, *AuxBuf;
PUBLIC size_t aribufSize, auxbufSize, scrbufSize;
        /* unit is sizeof(word2) */

PUBLIC void inialloc    _((void));
PUBLIC int memalloc _((int mem));
PUBLIC void dealloc _((void));
PUBLIC void resetarr    _((void));
PUBLIC int initend  _((void));
PUBLIC int tempfree _((int flg));
PUBLIC int inpack   _((truc obj, truc pack));
PUBLIC char *stringalloc  _((unsigned int size));
PUBLIC unsigned getblocksize  _((void));
PUBLIC size_t new0  _((unsigned int size));
PUBLIC truc newobj  _((int flg, unsigned int size, trucptr *ptraddr));
PUBLIC truc new0obj _((int flg, unsigned int size, trucptr *ptraddr));
PUBLIC unsigned obj4size  _((int type, truc *ptr));
PUBLIC void cpy4arr _((truc *ptr1, unsigned len, truc *ptr2));

/*--------------------------------------------------------*/

typedef struct {
    byte    flag;
    byte    flg2;
    word2   curbot;
    word2   blkceil;
    word2   blkbot;
} blkdesc;

PRIVATE char *Stringpool;
PRIVATE char *Stringsys;

PRIVATE size_t symBot, userBot;
PRIVATE size_t memBot, memCeil;
PRIVATE size_t argstkSize, workstkSize, arrstkSize, blockSize, block0Size;
PRIVATE int curblock, noofblocks, auxindex0, maxblocks;
PRIVATE blkdesc blockinfo[BLOCKMAX 1];

PRIVATE word4 gccount = 0;

PRIVATE truc gcsym, memavsym;

PRIVATE void iniconfig  _((int mem));
PRIVATE void inisymtab  _((void));
PRIVATE void iniblock   _((void));
PRIVATE void memstatistics  _((long slot[4]));
PRIVATE void displaymem     _((long s[]));
PRIVATE void gcstatistics   _((void));
PRIVATE int memshrink   _((int nnew, int nold));
PRIVATE truc Fmemavail  _((int argn));
PRIVATE void nextblock  _((unsigned int size));
PRIVATE void clearbufs  _((void));
PRIVATE truc Fgc    _((int argn));
PRIVATE int garbcollect  _((int mode));
PRIVATE void prepgc _((void));
PRIVATE void endgc  _((void));
PRIVATE void mvsymtab   _((void));
PRIVATE void mvargstk   _((void));
PRIVATE void mvarrstk   _((void));
PRIVATE void mvevalstk  _((void));
PRIVATE void moveobj    _((truc *x));
PRIVATE int toupdate    _((truc *x));
PRIVATE int datupdate   _((int flg));

#define FREE        0
#define HALFFULL    1
#define FULL        2
#define RESERVED    4
#define NOAGERL     8

/*------------------------------------------------------------*/
PUBLIC void inialloc()
{
    gcsym   = newsymsig("gc",   sFBINARY, (wtruc)Fgc, s_01);
    memavsym= newsymsig("memavail", sFBINARY, (wtruc)Fmemavail, s_01);
}
/*--------------------------------------------------------------*/
PRIVATE void iniconfig(mem)
int mem;
{
    int k;
    long memmax;

#ifdef M_LARGE
    assert(sizeof(word4) == 4);
    assert(sizeof(word2) == 2);
#endif
    argstkSize = ARGSTKSIZE;
    workstkSize = WORKSTKSIZE;
    arrstkSize = ARRAYSTKSIZE;
    hashtabSize = HASHTABSIZE;
    aribufSize = ARIBUFSIZE;
    blockSize = BLOCKSIZE;
    block0Size = BLOCK0SIZE;

    if(mem <= 0) {
        mem = MEM_DEFAULT;
    }
#ifdef M_LARGE
    else if(mem < 1000) {
        block0Size = MED_BLOCK0SIZE;
        if(mem < 512)
            mem = 512;
    }
#else
    else if(mem < 64)
        mem = 64;
#endif
#ifdef ATARIST
    else if(mem >= 1000) {
        blockSize = BIG_BLOCKSIZE;
        block0Size = MED_BLOCK0SIZE;
    }
#endif
#ifdef DOSorTOS
    else if(mem >= 200)
        blockSize = MED_BLOCKSIZE;
#endif
    memmax = blockSize;
    memmax *= BLOCKMAX;
    memmax /= 255;
    if(mem > memmax)
        mem = memmax;
    if(mem < 2000) {
        if(mem < 96) {
            maxblocks = 2;
        }
        else {
            for(k=3; k<BLOCKMAX; k  )
                if(mem/k <= blockSize/255)
                    break;
            maxblocks = k;
        }
        blockSize = mem/maxblocks;
        blockSize *= 255;
        blockSize &= 0xFFFE;    /* make even */
    }
    else {  /* mem >= 2000 */
        maxblocks = (mem   blockSize/510)/(blockSize/255);
        if(maxblocks > BLOCKMAX)
            maxblocks = BLOCKMAX;
    }
}
/*--------------------------------------------------------------*/
/*
** allocate memory for Symtab,
** ArgStack, WorkStack, EvalStack,
** Symbol and Memory
** returns total amount of allocated memory (in kilobytes)
*/
PUBLIC int memalloc(mem)
int mem;
{
    int k;
    unsigned long memallsize;
    size_t size;
    void *ptr;

    stacklimit();

    iniconfig(mem);
    size = sizeof(trucptr) * hashtabSize;
    memallsize = size;
    ptr = malloc(size);
    if(ptr) {
        Symtab = (trucptr *)ptr;
        size = sizeof(truc) * argstkSize;
        memallsize  = size;
        ptr = malloc(size);
    }
    if(ptr) {
        ArgStack = (truc *)ptr;
        size = sizeof(truc) * workstkSize;
        memallsize  = size;
        ptr = malloc(size);
    }
    if(ptr) {
        WorkStack = (truc *)ptr;
        size = sizeof(truc) * arrstkSize;
        memallsize  = size;
        ptr = malloc(size);
    }
    if(ptr) {
        ArrayStack = (truc *)ptr;
        ArrayStkCeil = ArrayStack   arrstkSize;
        size = sizeof(word2)*(aribufSize   PRIMTABSIZE   16);
        memallsize  = size;
        ptr = malloc(size);
    }
    if(ptr) {
        AriBuf = (word2 *)ptr;
        PrimTab = AriBuf   aribufSize   16;
        inisymtab();
        resetarr();
    }
    else
        faterr(err_memory);

    size = sizeof(truc) * block0Size;
    ptr = malloc(size);
#ifdef M_LARGE
    noofblocks = maxblocks;
    if(ptr) {
        memallsize  = size;
        Memory[0] = (truc *)ptr;
    }
    else {
        goto errmem;
    }
    size = sizeof(truc)*blockSize*noofblocks;
    ptr = malloc(size);
    if(ptr) {
        memallsize  = size;
        Memory[1] = (truc *)ptr;
        for(k=2; k<=noofblocks; k  )
            Memory[k] = Memory[k-1]   blockSize;
    }
    else {
        goto errmem;
    }
#else /* !M_LARGE */
    k = 0;
    while(ptr != NULL) {
        memallsize  = size;
        Memory[k] = (truc *)ptr;
        if(  k > maxblocks)
            break;
        size = sizeof(truc) * blockSize;
        ptr = malloc(size);
    }
    noofblocks = k-1;
#endif /* ?M_LARGE */
    ptr = malloc(RESERVE);  /* test free memory */
    if(ptr != NULL)
        free(ptr);
    else {
        free(Memory[noofblocks]);
        noofblocks--;
        memallsize -= size;
    }
    if(noofblocks < 2)
        goto errmem;
    Symbol = Memory[0];
    symBot = 0;
    Stringpool = (char *)(Symbol   block0Size);
    iniblock();
    return((int)(memallsize >> 10));
  errmem:
    faterr(err_memory);
    return(0);
}
/*-------------------------------------------------------------*/
PUBLIC void dealloc()
{

#ifdef M_LARGE
    free(Memory[1]);
#else
    int i;
    for(i=noofblocks; i>=1; i--)
        if(blockinfo[i].blkbot == 0)
            free(Memory[i]);
#endif
    free(Symbol);
    free(AriBuf);
    free(WorkStack);
    free(ArrayStack);
    free(ArgStack);
    free(Symtab);
}
/*-------------------------------------------------------------*/
PUBLIC void resetarr()
{
    workStkPtr = WorkStack - 1;
    evalStkPtr = WorkStack   workstkSize;
    arrayStkPtr= ArrayStack - 1;
    argStkPtr  = ArgStack - 1;
    saveStkPtr = ArgStack   argstkSize;
    basePtr    = ArgStack;
}
/* ------------------------------------------------------- */
PRIVATE void inisymtab()
{
    trucptr *sympt;
    int i;

    sympt = Symtab;
    i = hashtabSize;
    while(--i >= 0)
        *sympt   = NULL;
}
/*---------------------------------------------------------*/
PRIVATE void iniblock()
{
    int split, m;
    int i;

    scrbufSize =
        (blockSize / sizeof(word2)) * sizeof(truc);
#ifdef M_LARGE
    auxbufSize =
    (noofblocks*blockSize/2)/sizeof(word2)*sizeof(truc) - scrbufSize;
#else /* !M_LARGE */
    auxbufSize = scrbufSize;
    if(noofblocks == 3)
        auxbufSize /= 2;
    else if(noofblocks < 3)
        auxbufSize = 0;
#endif /* ?M_LARGE */

    m = (noofblocks 1)/2   1;
    split = (noofblocks & 1) && (noofblocks < BLOCKMAX);
    if(split) {
        noofblocks  ;
        for(i=noofblocks; i>=m; i--)
            Memory[i] = Memory[i-1];
    }
    blockinfo[0].flag = noofblocks;
    for(i=1; i<=noofblocks; i  ) {
        blockinfo[i].flag = (i<m ? FREE : RESERVED);
        blockinfo[i].blkbot = 0;
        blockinfo[i].curbot = 0;
        blockinfo[i].blkceil = blockSize;
    }
    if(split) {
        blockinfo[m-1].blkceil = blockSize/2;
        blockinfo[m].blkbot = blockSize/2;
        blockinfo[m].curbot = blockSize/2;
    }
    curblock = 1;
    memBot = blockinfo[curblock].blkbot;
    memCeil = blockinfo[curblock].blkceil;
    AriScratch = (word2 *)Memory[noofblocks];

#ifdef M_LARGE
    auxindex0 = noofblocks/2 1;
#else /* !M_LARGE */
    auxindex0 = noofblocks-1;
#endif /* ?M_LARGE */
    AuxBuf = (word2 *)Memory[auxindex0];
}
/*---------------------------------------------------------*/
/*
** must be called at the end of initializations
** sets global variables userBot and Stringsys
** returns number of bytes used by system for symbols and symbol names
*/
PUBLIC int initend()
{
    size_t n;

    userBot = symBot;
    Stringsys = Stringpool;

    n = (char *)(Symbol   block0Size) - Stringsys;
    n  = sizeof(truc)*userBot;
    return(n);
}
/*---------------------------------------------------------*/
/*
** Mit Argument flg > 0: Gibt die zweite Haelfte der Memory-Bloecke frei
** Mit Argument flg == 0: Allokiert von neuem die freigegebenen
** Memorybloecke
** Rueckgabewert: 1 bei Erfolg, 0 bei Fehler
*/
PUBLIC int tempfree(flg)
int flg;
{
#ifdef M_SMALL
    int i, m, res;
    size_t size;
    void *ptr;

    m = (noofblocks/2)   1;
    if(blockinfo[m].blkbot > 0)
        m  ;
    if(flg > 0) {
        garbcollect(1);
        if(blockinfo[1].flag == RESERVED)
            garbcollect(1);
        /* nun ist zweite Haelfte frei */
        for(i=noofblocks; i>=m; i--)
            free(Memory[i]);
    }
    else {
        size = blockSize * sizeof(truc);
        for(i=m; i<=noofblocks; i  ) {
            ptr = malloc(size);
            if(ptr == NULL) {
                res = memshrink(i-1,noofblocks);
                if(res == 0) {
                noofblocks = i-1;
                return(0);
                }
                else
                break;
            }
            Memory[i] = (truc *)ptr;
        }
        AriScratch = (word2 *)Memory[noofblocks];
        AuxBuf = (word2 *)Memory[noofblocks-1];
    }
#endif /* M_SMALL */
    return(1);
}
/*---------------------------------------------------------*/
/*
** Reduziert die Anzahl der Memory-Bloecke von nold auf nnew
** Es wird vorausgesetzt, dass die derzeit aktiven Bloecke
** zur ersten Haelfte gehoeren und dass nnew < nold
** Rueckgabewert: 1 bei Erfolg, 0 bei Misserfolg
*/
PRIVATE int memshrink(nnew,nold)
int nnew, nold;
{
#ifdef M_SMALL
    int i,m,m1,split;

    m = (nold/2)   1;
    split = (blockinfo[m].blkbot > 0);
    if(split)
        nnew--;
    if(nnew < 2)
        return(0);
    else if(nnew == 2)
        auxbufSize = 0;
    else if(nnew == 3)
        auxbufSize /= 2;
    m1 = nnew/2;
    for(i=m1; i<m; i  ) {
        if(blockinfo[m].blkbot < blockinfo[m].curbot)
            return(0);
    }
    if(split) {
        for(i=m; i<=nnew; i  )
            Memory[i] = Memory[i 1];
        blockinfo[m-1].blkceil = blockinfo[m].blkceil;
        blockinfo[m].blkbot = 0;
    }
    m = (nnew 1)/2;
    if(nnew & 1) {
        nnew  ;
        for(i=nnew; i>m; i--)
            Memory[i] = Memory[i-1];
        blockinfo[m].blkceil /= 2;
        blockinfo[m 1].blkbot = blockinfo[m].blkceil;
    }
    for(i=nnew; i>m; i--)
        blockinfo[i].flag = RESERVED;
    noofblocks = nnew;
#endif  /* M_SMALL */
    return(1);
}
/*---------------------------------------------------------*/
PUBLIC int inpack(obj,pack)
truc obj, pack;
{
    variant v;
    int sys;

    v.xx = obj;
    sys = (v.pp.ww < userBot);
    if(pack == arisym)
        return(sys);
    else if(pack == usersym)
        return(!sys);
    else
        return(0);
}
/*---------------------------------------------------------*/
PRIVATE void memstatistics(slot)
long slot[4];
{
    int i, flg;
    unsigned b,c;
    unsigned long nres = 0, nact = 0, nfree = 0, nsymb;
    unsigned s = sizeof(truc);

    for(i=1; i<=noofblocks; i  ) {
        b = blockinfo[i].blkbot;
        c = blockinfo[i].blkceil;
        if((flg = blockinfo[i].flag) == RESERVED) {
            nres  = c - b;
        }
        else {
            nact  = c - b;
            if(flg < FULL) {
                b = (i == curblock ? memBot : blockinfo[i].curbot);
                nfree  = c - b;
            }
        }
    }
    slot[0] = s * nres;
    slot[1] = s * nact;
    slot[2] = s * nfree;
    nsymb = Stringpool - (char *)(Symbol   symBot);
    slot[3] = nsymb;
}
/*---------------------------------------------------------*/
PRIVATE void displaymem(s)
long s[];
{
    int n;
    long diff;

    diff = s[1] - s[2];
    n = s2form(OutBuf,"~8D Bytes reserved; ~D Bytes active ",
        intcast(s[0]),intcast(s[1]));
    s2form(OutBuf n,"(~D used, ~D free)",intcast(diff),intcast(s[2]));
    fprintline(tstdout,OutBuf);
    s1form(OutBuf,
      "~8D Bytes free for user defined symbols and symbol names", 
      intcast(s[3]));
    fprintline(tstdout,OutBuf);
}
/*---------------------------------------------------------*/
PRIVATE void gcstatistics()
{
    fnewline(tstdout);
    s1form(OutBuf,"total number of garbage collections: ~D", 
      intcast(gccount));
    fprintline(tstdout,OutBuf);
}
/*---------------------------------------------------------*/
PRIVATE truc Fmemavail(argn)
int argn;
{
    long s[4];
    unsigned f;
    int verbose;

    verbose = (argn == 0 || *argStkPtr != zero);

    memstatistics(s);
    if(verbose) {
        gcstatistics();
        displaymem(s);
    }
    f = s[2] >> 10;     /* free kilobytes */
    return(mkfixnum(f));
}
/*---------------------------------------------------------*/
PUBLIC char *stringalloc(size)
unsigned int size;  /* unit for size is sizeof(char) */
{
    if(Stringpool - size <= (char *)(Symbol   symBot))
        faterr(err_memory);
    Stringpool -= size;
    return(Stringpool);
}
/*---------------------------------------------------------*/
PUBLIC unsigned getblocksize()
{
    return(blockSize);
}
/*---------------------------------------------------------*/
PUBLIC size_t new0(size)
unsigned int size;  /* unit for size is sizeof(truc) */
{
    size_t loc;

    loc = symBot;
    symBot  = size;
#ifdef ALIGN8
    if (symBot & 0x1)
        symBot  ;
#endif
    if(Stringpool <= (char *)(Symbol   symBot))
        faterr(err_memory);
    return(loc);
}
/*---------------------------------------------------------*/
PUBLIC truc newobj(flg,size,ptraddr)
int flg;
unsigned int size;
trucptr *ptraddr;
{
    variant v;

    if(size > memCeil - memBot) {
        nextblock(size);
    }
    v.pp.b0 = flg;
    v.pp.b1 = curblock;
    v.pp.ww = memBot;
    *ptraddr = Memory[curblock]   memBot;

    memBot  = size;
    return(v.xx);
}
/*---------------------------------------------------------*/
/*
** allocation from memory block 0
** (for symbols, not moved during garbage collection)
*/
PUBLIC truc new0obj(flg,size,ptraddr)
int flg;
unsigned int size;  /* unit for size is sizeof(truc) */
trucptr *ptraddr;
{
    variant v;
    size_t loc = new0(size);

    v.pp.b0 = flg;
    v.pp.b1 = 0;
    v.pp.ww = loc;
    *ptraddr = Symbol   loc;

    return(v.xx);
}
/*---------------------------------------------------------*/
PRIVATE void nextblock(size)
unsigned int size;
{
    int i,k;
    int collected = 0;

    blockinfo[curblock].curbot = memBot;
    blockinfo[curblock].flag =
        (memCeil - memBot >= NOAGERL ? HALFFULL : FULL);

    if(size > blockSize) {
        reset(err_2large);
    }
  nochmal:
    k = curblock;
    for(i=1; i<=noofblocks; i  ) {
        if(  k > noofblocks)
            k = 1;
        if((blockinfo[k].flag <= HALFFULL) &&
           (size <= blockinfo[k].blkceil - blockinfo[k].curbot)) {
            memBot = blockinfo[k].curbot;
            memCeil = blockinfo[k].blkceil;
            curblock = k;
            return;
        }
    }
    if(!collected && garbcollect(1)) {
        collected = 1;
        goto nochmal;
    }
    clearbufs();
    if(garbcollect(0))
        reset(err_memev);
    else
        faterr(err_garb);
}
/*------------------------------------------------------------*/
PRIVATE void clearbufs()
{
    *res3Ptr = zero;
    *res2Ptr = zero;
    *res1Ptr = zero;
    *brkbindPtr = zero;
}
/*------------------------------------------------------------*/
PRIVATE truc Fgc(argn)
int argn;
{
    garbcollect(1);
    return Fmemavail(argn);
}
/*------------------------------------------------------------*/
PRIVATE int garbcollect(mode)
int mode;   /* mode = 0: emergency collection */
{
    static int merk = 0;

    gccount  ;
    if(merk  ) {
        merk = 0;
        return(0);
    }
    prepgc();
    mvsymtab();
    if(mode > 0) {
        mvargstk();
        mvevalstk();
        mvarrstk();
    }
    endgc();

    --merk;
    return(1);
}
/*------------------------------------------------------------*/
PRIVATE void prepgc()
{
    int i, first = 1;
    for(i=1; i<=noofblocks; i  ) {
        blockinfo[i].curbot = blockinfo[i].blkbot;
        if(blockinfo[i].flag == RESERVED) {
            blockinfo[i].flag = FREE;
            if(first) {
                curblock = i;
                memBot = blockinfo[i].curbot;
                memCeil = blockinfo[i].blkceil;
                first = 0;
            }
        }
        else
            blockinfo[i].flag = RESERVED;
    }
}
/*------------------------------------------------------------*/
PRIVATE void endgc()
{
    int scratchind, auxind;

    blockinfo[curblock].curbot = memBot;

    if(blockinfo[1].flag == RESERVED) {
        scratchind = 1;
        auxind = 2;
    }
    else {
        scratchind = noofblocks;
        auxind = auxindex0;
    }
    AriScratch = (word2 *)Memory[scratchind];
    AuxBuf = (word2 *)Memory[auxind];
}
/*------------------------------------------------------------*/
PRIVATE void mvsymtab()
{
    int n, flg;
    truc *x;

    *res3Ptr = zero;
    n = 0;
    while((x = nextsymptr(n  )) != NULL) {
        flg = *FLAGPTR(x);
        if(flg & sGCMOVEBIND)
            moveobj(SYMBINDPTR(x));
    }
}
/*------------------------------------------------------------*/
PRIVATE void mvargstk()
{
    truc *ptr;

    ptr = ArgStack - 1;
    while(  ptr <= argStkPtr)
        moveobj(ptr);
}
/*------------------------------------------------------------*/
PRIVATE void mvarrstk()
{
    truc *ptr;

    ptr = ArrayStack - 1;
    while(  ptr <= arrayStkPtr)
        moveobj(ptr);
}
/*------------------------------------------------------------*/
PRIVATE void mvevalstk()
{
    truc *ptr;

    ptr = WorkStack - 1;
    while(  ptr <= workStkPtr)
        moveobj(ptr);

    ptr = WorkStack   workstkSize;
    while(--ptr >= evalStkPtr)
        moveobj(ptr);
}
/*------------------------------------------------------------*/
PRIVATE void moveobj(x)
truc *x;
{
    int flg;
    unsigned int len;
    truc *ptr, *ptr2;

  nochmal:
    flg = toupdate(x);
    if(!flg)
        return;

    ptr = TAddress(x);
    if(*FLAGPTR(ptr) == GCMARK) {   /* update *x */
        *x = *ptr;
        *FLAGPTR(x) = flg;
        return;
    }
    len = obj4size(flg,ptr);

    if(len == 0)    /* this case should not happen */
        return;

    *x = newobj(flg,len,&ptr2);
    cpy4arr(ptr,len,ptr2);
    *ptr = *x;          /* put forwarding address */
    *FLAGPTR(ptr) = GCMARK;

    if(datupdate(flg) && (len >= 2)) {
        while(--len > 1)    /* first element always fixed */
            moveobj(  ptr2);
        /*** tail recursion elimination *******/
        x = ptr2   1;
        goto nochmal;
    }
}
/*------------------------------------------------------------*/
/*
** returns 0, if *x needs no update; else returns flag of *x
*/
PRIVATE int toupdate(x)
truc *x;
{
    int flg, seg;

    flg = *FLAGPTR(x);
    if(flg & FIXMASK)
        return(0);
    seg = *SEGPTR(x);
    if(seg == 0 || blockinfo[seg].flag != RESERVED)
        return(0);
    else {
        return(flg);
    }
}
/*------------------------------------------------------------*/
/*
** returns 0, if data of object are fixed, else returns 1
*/
PRIVATE int datupdate(flg)
int flg;
{
    if(flg == fSTREAM)
        return(0);
    else if(flg <= fVECTOR)
        return(1);
    else
        return(0);
}
/*------------------------------------------------------------*/
/*
** return size of objects (unit is sizeof(truc)=4)
** which are not fixed during garbage collection
*/
PUBLIC unsigned obj4size(type,ptr)
int type;
truc *ptr;
{
    unsigned int len;

    switch(type) {
    case fBIGNUM:
    case fGF2NINT:
        len = ((struct bigcell *)ptr)->len;
        return(SIZEOFBIG(len));
    case fSTRING:
    case fBYTESTRING:
        len = ((struct strcell *)ptr)->len;
        return(SIZEOFSTRING(len));
    case fSTREAM:
        return(SIZEOFSTREAM);
    case fVECTOR:
        len = ((struct vector *)ptr)->len;
        return(SIZEOFVECTOR(len));
    case fPOINTER:
    case fRECORD:
        len = ((struct record *)ptr)->len;
        return(SIZEOFRECORD(len));
    case fSPECIAL1:
    case fBUILTIN1:
        return(SIZEOFOPNODE(1));
    case fSPECIAL2:
    case fBUILTIN2:
        return(SIZEOFOPNODE(2));
    case fTUPLE:
    case fWHILEXPR:
    case fIFEXPR:
    case fFOREXPR:
    case fCOMPEXPR:
        len = ((struct compnode *)ptr)->len;
        return(SIZEOFCOMP(len));
    case fSPECIALn:
    case fBUILTINn:
    case fFUNCALL:
        len = *ARGCPTR(ptr);
        return(SIZEOFFUNODE(len));
    case fFUNDEF:
        return(SIZEOFFUNDEF);
    case fSTACK:
        return(SIZEOFSTACK);
    default:
        if(type >= fFLTOBJ) {
            len = fltprec(type);
            return(SIZEOFFLOAT(len));
        }
        else {
            error(gcsym,err_case,mkfixnum(type));
            return(0);
        }
    }
}
/*------------------------------------------------------------*/
/*
** kopiert das word4-Array (ptr1,len) nach ptr2
*/
PUBLIC void cpy4arr(ptr1,len,ptr2)
truc *ptr1, *ptr2;
unsigned int len;
{
    while(len--)
        *ptr2   = *ptr1  ;
}
/************************************************************************/