summaryrefslogtreecommitdiff
path: root/riscv_new/rtl/inc/dynarr.inc
blob: cb64142ba1d49f66c2ae31e491772060f74a9a63 (plain)
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
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 2000 by Florian Klaempfl
    member of the Free Pascal development team.

    This file implements the helper routines for dyn. Arrays in FPC

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    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.

 **********************************************************************
}

type
   { don't add new fields, the size is used }
   { to calculate memory requirements       }
   pdynarray = ^tdynarray;
   tdynarray = packed record
      refcount : ptrint;
      high : tdynarrayindex;
   end;

   pdynarraytypedata = ^tdynarraytypedata;
   tdynarraytypedata =
{$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
   packed
{$else}
  {$ifdef powerpc64}
    { 3.0.0 does not align elType field on a 8-byte boundary,
      thus use packed also in this case }
    {$ifdef VER3_0_0}
      packed
    {$endif VER3_0_0}
  {$endif powerpc64}

{$endif FPC_REQUIRES_PROPER_ALIGNMENT}
   record
     elSize : SizeUInt;
     {$ifdef VER3_0}
     elType2 : Pointer;
     {$else}
     elType2 : PPointer;
     {$endif}
     varType : Longint;
     {$ifdef VER3_0}
     elType : Pointer;
     {$else}
     elType : PPointer;
     {$endif}
   end;

procedure fpc_dynarray_rangecheck(p : pointer;i : tdynarrayindex);[Public,Alias:'FPC_DYNARRAY_RANGECHECK']; compilerproc;
  begin
     if not(assigned(p)) or (i<0) or (i>pdynarray(p-sizeof(tdynarray))^.high) then
       HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  end;


function fpc_dynarray_length(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_LENGTH']; compilerproc;
  begin
     if assigned(p) then
       fpc_dynarray_length:=pdynarray(p-sizeof(tdynarray))^.high+1
     else
       fpc_dynarray_length:=0;
  end;


function fpc_dynarray_high(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_HIGH']; compilerproc;
  begin
     if assigned(p) then
       fpc_dynarray_high:=pdynarray(p-sizeof(tdynarray))^.high
     else
       fpc_dynarray_high:=-1;
  end;


procedure fpc_dynarray_clear(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_CLEAR']; compilerproc;
  var
     realp : pdynarray;
  begin
    if (P=Nil) then
      exit;
    realp:=pdynarray(p-sizeof(tdynarray));
    if realp^.refcount=0 then
      HandleErrorAddrFrameInd(204,get_pc_addr,get_frame);

    if (realp^.refcount>0) and declocked(realp^.refcount) then
      begin
{$ifdef VER3_0}
        ti:=aligntoptr(ti+2+PByte(ti)[1]);
{$else VER3_0}
        ti:=aligntoqword(ti+2+PByte(ti)[1]);
{$endif VER3_0}
        if assigned(pdynarraytypedata(ti)^.elType) then
          int_finalizearray(p,pdynarraytypedata(ti)^.elType{$ifndef VER3_0}^{$endif},realp^.high+1);
        freemem(realp);
      end;
    p:=nil;
  end;

{ alias for internal use }
Procedure fpc_dynarray_clear (var p : pointer;ti : pointer);[external name 'FPC_DYNARRAY_CLEAR'];


procedure fpc_dynarray_incr_ref(p : pointer);[Public,Alias:'FPC_DYNARRAY_INCR_REF']; compilerproc;
  var
     realp : pdynarray;
  begin
     if p=nil then
       exit;

     realp:=pdynarray(p-sizeof(tdynarray));
     if realp^.refcount=0 then
       HandleErrorAddrFrameInd(204,get_pc_addr,get_frame)
     else if realp^.refcount>0 then
       inclocked(realp^.refcount);
  end;

{ provide local access to dynarr_decr_ref for dynarr_setlength }
procedure fpc_dynarray_incr_ref(p : pointer); [external name 'FPC_DYNARRAY_INCR_REF'];


procedure fpc_dynarray_assign(var dest: Pointer; src: Pointer; ti: pointer);[public,alias:'FPC_DYNARRAY_ASSIGN']; compilerproc;
  begin
    fpc_dynarray_incr_ref(src);
    fpc_dynarray_clear(dest,ti);
    Dest:=Src;
  end;

procedure fpc_dynarray_assign(var dest: Pointer; src: Pointer; ti: pointer);[external name 'FPC_DYNARRAY_ASSIGN'];

{ provide local access to dynarr_setlength }
procedure int_dynarray_setlength(var p : pointer;pti : pointer;
  dimcount : sizeint;dims : pdynarrayindex);[external name 'FPC_DYNARR_SETLENGTH'];

procedure fpc_dynarray_setlength(var p : pointer;pti : pointer;
  dimcount : sizeint;dims : pdynarrayindex);[Public,Alias:'FPC_DYNARR_SETLENGTH']; compilerproc;

  var
     i : tdynarrayindex;
     movelen,
     size : sizeint;
     { contains the "fixed" pointers where the refcount }
     { and high are at positive offsets                 }
     realp,newp : pdynarray;
     ti : pointer;
     updatep: boolean;
     elesize : sizeint;
     eletype,eletypemngd : pointer;
     movsize : sizeint;

  begin
     { negative length is not allowed }
     if dims[0]<0 then
       HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);

     { skip kind and name }
{$ifdef VER3_0}
     ti:=aligntoptr(Pointer(pti)+2+PByte(pti)[1]);
{$else VER3_0}
     ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
{$endif VER3_0}

     elesize:=pdynarraytypedata(ti)^.elSize;
     {$ifdef VER3_0}
     eletype:=pdynarraytypedata(ti)^.elType2;
     {$else}
     eletype:=pdynarraytypedata(ti)^.elType2^;
     {$endif}
     { only set if type needs finalization }
     {$ifdef VER3_0}
     eletypemngd:=pdynarraytypedata(ti)^.elType;
     {$else}
     if assigned(pdynarraytypedata(ti)^.elType) then
       eletypemngd:=pdynarraytypedata(ti)^.elType^
     else
       eletypemngd:=nil;
     {$endif}

     { determine new memory size }
     size:=elesize*dims[0]+sizeof(tdynarray);
     updatep := false;

     { not assigned yet? }
     if not(assigned(p)) then
       begin
          { do we have to allocate memory? }
          if dims[0] = 0 then
            exit;
          getmem(newp,size);
          fillchar(newp^,size,0);
{$ifndef VER3_0}
          { call int_InitializeArray for management operators }
          if assigned(eletypemngd) and (PTypeKind(eletype)^ in [tkRecord, tkObject]) then
            int_InitializeArray(pointer(newp)+sizeof(tdynarray), eletype, dims[0]);
{$endif VER3_0}
          updatep := true;
       end
     else
       begin
          { if the new dimension is 0, we've to release all data }
          if dims[0]=0 then
            begin
               fpc_dynarray_clear(p,pti);
               exit;
            end;

          realp:=pdynarray(p-sizeof(tdynarray));
          newp := realp;

          if realp^.refcount<>1 then
            begin
               updatep := true;
               { make an unique copy }
               getmem(newp,size);
               fillchar(newp^,sizeof(tdynarray),0);
               if realp^.high < dims[0] then
                 movelen := realp^.high+1
               else
                 movelen := dims[0];
               movsize := elesize*movelen;
               move(p^,(pointer(newp)+sizeof(tdynarray))^, movsize);
               if size-sizeof(tdynarray)>movsize then
                 fillchar((pointer(newp)+sizeof(tdynarray)+movsize)^,size-sizeof(tdynarray)-movsize,0);

               { increment ref. count of managed members }
               if assigned(eletypemngd) then
                 for i:= 0 to movelen-1 do
                   int_addref(pointer(newp)+sizeof(tdynarray)+elesize*i,eletypemngd);

               { a declock(ref. count) isn't enough here }
               { it could be that the in MT environments  }
               { in the mean time the refcount was       }
               { decremented                             }

               { it is, because it doesn't really matter }
               { if the array is now removed             }
               fpc_dynarray_clear(p,pti);
            end
          else if dims[0]<>realp^.high+1 then
            begin
               { range checking is quite difficult ...  }
               { if size overflows then it is less than }
               { the values it was calculated from      }
               if (size<sizeof(tdynarray)) or
                 ((elesize>0) and (size<elesize)) then
                 HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);

               { resize? }
               { here, realp^.refcount has to be one, otherwise the previous }
               { if-statement would have been taken. Or is this also for MT  }
               { code? (JM)                                                  }
               if realp^.refcount=1 then
                 begin
                    { shrink the array? }
                    if dims[0]<realp^.high+1 then
                      begin
                         if assigned(eletypemngd) then
                           int_finalizearray(pointer(realp)+sizeof(tdynarray)+
                              elesize*dims[0],
                              eletypemngd,realp^.high-dims[0]+1);
                         reallocmem(realp,size);
                      end
                    else if dims[0]>realp^.high+1 then
                      begin
                         reallocmem(realp,size);
                         fillchar((pointer(realp)+sizeof(tdynarray)+elesize*(realp^.high+1))^,
                           (dims[0]-realp^.high-1)*elesize,0);
{$ifndef VER3_0}
                         { call int_InitializeArray for management operators }
                         if assigned(eletypemngd) and (PTypeKind(eletype)^ in [tkRecord, tkObject]) then
                           int_InitializeArray(pointer(realp)+sizeof(tdynarray)+elesize*(realp^.high+1),
                             eletype, dims[0]-realp^.high-1);
{$endif VER3_0}
                      end;
                    newp := realp;
                    updatep := true;
                 end;
            end;
       end;
    { handle nested arrays }
    if dimcount>1 then
      begin
         for i:=0 to dims[0]-1 do
           int_dynarray_setlength(pointer((pointer(newp)+sizeof(tdynarray)+i*elesize)^),
             eletype,dimcount-1,@dims[1]);
      end;
     if updatep then
       begin
         p:=pointer(newp)+sizeof(tdynarray);
         newp^.refcount:=1;
         newp^.high:=dims[0]-1;
       end;
  end;


{ provide local access to dynarr_copy }
function int_dynarray_copy(psrc : pointer;ti : pointer;
    lowidx,count:tdynarrayindex) : fpc_stub_dynarray;[external name 'FPC_DYNARR_COPY'];

function fpc_dynarray_copy(psrc : pointer;ti : pointer;
    lowidx,count:tdynarrayindex) : fpc_stub_dynarray;[Public,Alias:'FPC_DYNARR_COPY'];compilerproc;
  var
    realpsrc : pdynarray;
    i,size : sizeint;
    elesize : sizeint;
    eletype : pointer;
  begin
     fpc_dynarray_clear(pointer(result),ti);
     if psrc=nil then
       exit;
{$ifndef FPC_DYNARRAYCOPY_FIXED}
     if (lowidx=-1) and (count=-1) then
       begin
         lowidx:=0;
         count:=high(tdynarrayindex);
       end;
{$endif FPC_DYNARRAYCOPY_FIXED}
     realpsrc:=pdynarray(psrc-sizeof(tdynarray));
     if (lowidx<0) then
       begin
       { Decrease count if index is negative, this is different from how copy()
         works on strings. Checked against D7. }
         if count<=0 then
           exit;              { may overflow when adding lowidx }
         count:=count+lowidx;
         lowidx:=0;
       end;
     if (count>realpsrc^.high-lowidx+1) then
       count:=realpsrc^.high-lowidx+1;
     if count<=0 then
       exit;

     { skip kind and name }
{$ifdef VER3_0}
     ti:=aligntoptr(ti+2+PByte(ti)[1]);
{$else VER3_0}
     ti:=aligntoqword(ti+2+PByte(ti)[1]);
{$endif VER3_0}

     elesize:=pdynarraytypedata(ti)^.elSize;
     { only set if type needs finalization }
     {$ifdef VER3_0}
     eletype:=pdynarraytypedata(ti)^.elType;
     {$else}
     if assigned(pdynarraytypedata(ti)^.elType) then
       eletype:=pdynarraytypedata(ti)^.elType^
     else
       eletype:=nil;
     {$endif}

     { create new array }
     size:=elesize*count;
     getmem(pointer(result),size+sizeof(tdynarray));
     pdynarray(result)^.refcount:=1;
     pdynarray(result)^.high:=count-1;
     inc(pointer(result),sizeof(tdynarray));
     { copy data }
     move(pointer(psrc+elesize*lowidx)^,pointer(result)^,size);

     { increment ref. count of members? }
     if assigned(eletype) then
       for i:=0 to count-1 do
         int_addref(pointer(pointer(result)+elesize*i),eletype);
  end;


{$ifndef VER3_0}
procedure fpc_dynarray_delete(var p : pointer;source,count : SizeInt;pti : pointer);
   var
      newhigh,
      i : tdynarrayindex;
      size : sizeint;
      { contains the "fixed" pointers where the refcount }
      { and high are at positive offsets                 }
      realp,newp : pdynarray;
      ti : pointer;
      elesize : sizeint;
      eletype,eletypemngd : pointer;

   begin
     { if source > high then nothing to do }
     if not assigned(p) or
         (source>pdynarray(p-sizeof(tdynarray))^.high) or
         (count<=0) or
         (source<0) then
       exit;
     { cap count }
     if source+count-1>pdynarray(p-sizeof(tdynarray))^.high then
       count:=pdynarray(p-sizeof(tdynarray))^.high-source+1;

     { fast path: delete whole array }
     if (source=0) and (count=pdynarray(p-sizeof(tdynarray))^.high+1) then
       begin
         fpc_dynarray_clear(p,pti);
         exit;
       end;

     { skip kind and name }
     ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);

     elesize:=pdynarraytypedata(ti)^.elSize;
     eletype:=pdynarraytypedata(ti)^.elType2^;
     { only set if type needs finalization }
     if assigned(pdynarraytypedata(ti)^.elType) then
       eletypemngd:=pdynarraytypedata(ti)^.elType^
     else
       eletypemngd:=nil;

     realp:=pdynarray(p-sizeof(tdynarray));
     newp:=realp;

     { determine new memory size }
     newhigh:=realp^.high-count;
     size:=elesize*(newhigh+1)+sizeof(tdynarray);

     if realp^.refcount<>1 then
       begin
          { make an unique copy }
          getmem(newp,size);
          fillchar(newp^,sizeof(tdynarray),0);
          { copy the elements that we still need }
          if source>0 then
            move(p^,(pointer(newp)+sizeof(tdynarray))^,source*elesize);
          if source+count-1<realp^.high then
            move((p+(source+count)*elesize)^,(pointer(newp)+sizeof(tdynarray)+source*elesize)^,(realp^.high-(source+count)+1)*elesize);

          { increment ref. count of managed members }
          if assigned(eletypemngd) then
            for i:=0 to newhigh do
              int_addref(pointer(newp)+sizeof(tdynarray)+elesize*i,eletypemngd);

          { a declock(ref. count) isn't enough here }
          { it could be that the in MT environments  }
          { in the mean time the refcount was       }
          { decremented                             }

          { it is, because it doesn't really matter }
          { if the array is now removed             }
          fpc_dynarray_clear(p,pti);
        end
      else
        begin
          { finalize the elements that will be removed }
          if assigned(eletypemngd) then
            begin
              for i:=source to source+count-1 do
                int_finalize(p+i*elesize,eletype);
            end;

          { close the gap by moving the trailing elements to the front }
          move((p+(source+count)*elesize)^,(p+source*elesize)^,(realp^.high-(source+count)+1)*elesize);

          { resize the array }
          reallocmem(realp,size);
          newp:=realp;
        end;
    p:=pointer(newp)+sizeof(tdynarray);
    newp^.refcount:=1;
    newp^.high:=newhigh;
  end;


procedure fpc_dynarray_insert(var p : pointer;source : SizeInt;data : pointer;count : SizeInt;pti : pointer);compilerproc;
  var
    newhigh,
    i : tdynarrayindex;
    size : sizeint;
    realp,
    newp : pdynarray;
    ti : pointer;
    elesize : sizeint;
    eletype,eletypemngd : pointer;
  begin
    if not assigned(data) or
        (count=0) then
      exit;

    if assigned(p) then
      realp:=pdynarray(p-sizeof(tdynarray))
    else
      realp:=nil;
    newp:=realp;

    { cap insert index }
    if assigned(p) then
      begin
        if source<0 then
          source:=0
        else if source>realp^.high+1 then
          source:=realp^.high+1;
      end
    else
      source:=0;

    { skip kind and name }
    ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);

    elesize:=pdynarraytypedata(ti)^.elSize;
    eletype:=pdynarraytypedata(ti)^.elType2^;
    { only set if type needs initialization }
    if assigned(pdynarraytypedata(ti)^.elType) then
      eletypemngd:=pdynarraytypedata(ti)^.elType^
    else
      eletypemngd:=nil;

    { determine new memory size }
    if assigned(p) then
      newhigh:=realp^.high+count
    else
      newhigh:=count-1;
    size:=elesize*(newhigh+1)+sizeof(tdynarray);

    if assigned(p) then
      begin
        if realp^.refcount<>1 then
          begin
            { make an unique copy }
            getmem(newp,size);
            fillchar(newp^,sizeof(tdynarray),0);

            { copy leading elements }
            if source>0 then
              move(p^,(pointer(newp)+sizeof(tdynarray))^,source*elesize);
            { insert new elements }
            move(data^,(pointer(newp)+sizeof(tdynarray)+source*elesize)^,count*elesize);
            { copy trailing elements }
            if realp^.high-source+1>0 then
              move((p+source*elesize)^,(pointer(newp)+sizeof(tdynarray)+(source+count)*elesize)^,(realp^.high-source+1)*elesize);

            { increment ref. count of managed members }
            if assigned(eletypemngd) then
              for i:=0 to newhigh do
                int_addref(pointer(newp)+sizeof(tdynarray)+elesize*i,eletypemngd);

            { a declock(ref. count) isn't enough here }
            { it could be that the in MT environments  }
            { in the mean time the refcount was       }
            { decremented                             }

            { it is, because it doesn't really matter }
            { if the array is now removed             }
            fpc_dynarray_clear(p,pti);
          end
        else
          begin
            { resize the array }
            reallocmem(realp,size);

            { p might no longer be correct }
            p:=pointer(realp)+sizeof(tdynarray);

            { move the trailing part after the inserted data }
            if source<=realp^.high then
              move((p+source*elesize)^,(p+(source+count)*elesize)^,(realp^.high-source+1)*elesize);

            { move the inserted data to the destination }
            move(data^,(p+source*elesize)^,count*elesize);

            { increase reference counts of inserted elements }
            if assigned(eletypemngd) then
              begin
                for i:=source to source+count-1 do
                  int_addref(p+i*elesize,eletypemngd);
              end;

            newp:=realp;
          end;
      end
    else
      begin
        { allocate new array }
        getmem(newp,size);
        fillchar(newp^,sizeof(tdynarray),0);

        { insert data }
        move(data^,(pointer(newp)+sizeof(tdynarray))^,count*elesize);

        { increase reference counts of inserted elements }
        if assigned(eletypemngd) then
          begin
            for i:=0 to count-1 do
              int_addref(pointer(newp)+sizeof(tdynarray)+i*elesize,eletypemngd);
          end;
      end;

    p:=pointer(newp)+sizeof(tdynarray);
    newp^.refcount:=1;
    newp^.high:=newhigh;
  end;


procedure fpc_dynarray_concat_multi(var dest : pointer; pti: pointer; const sarr:array of pointer); compilerproc;
  var
    i,
    offset,
    totallen : sizeint;
    newp,
    realp,
    srealp : pdynarray;
    ti : pointer;
    elesize : sizeint;
    eletypemngd : pointer;
  begin
    { sanity check }
    if length(sarr)=0 then
      exit;

    totallen:=0;
    for i:=0 to high(sarr) do
      if assigned(sarr[i]) then
        inc(totallen,pdynarray(sarr[i]-sizeof(tdynarray))^.high+1);

    if totallen=0 then
      begin
        fpc_dynarray_clear(dest,pti);
        exit;
      end;

    { skip kind and name }
    ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);

    elesize:=pdynarraytypedata(ti)^.elSize;

    { only set if type needs initialization }
    if assigned(pdynarraytypedata(ti)^.elType) then
      eletypemngd:=pdynarraytypedata(ti)^.elType^
    else
      eletypemngd:=nil;

    { copy the elements of each source array }
    offset:=0;

    { the idea to reuse the first array, re-allocate it and append the other entries is not possible as the first entry
      might be finalized later on by the caller however in case of a re-allocate, the entry itself might be gone }
    { allocate new array }
    getmem(newp,totallen*elesize+sizeof(tdynarray));

    for i:=0 to high(sarr) do
      if assigned(sarr[i]) then
        begin
          srealp:=pdynarray(sarr[i]-sizeof(tdynarray));
          if srealp^.high>=0 then
            begin
              move(sarr[i]^,(pointer(newp)+sizeof(tdynarray)+offset*elesize)^,(srealp^.high+1)*elesize);
              inc(offset,srealp^.high+1);
            end;
        end;
    { increase reference counts of all the elements }
    if assigned(eletypemngd) then
      begin
        for i:=0 to totallen-1 do
          int_addref(pointer(newp)+sizeof(tdynarray)+i*elesize,eletypemngd);
      end;

    { clear at the end, dest could be a reference to an array being used also as source }
    fpc_dynarray_clear(dest,pti);
    dest:=pointer(newp)+sizeof(tdynarray);
    newp^.refcount:=1;
    newp^.high:=totallen-1;
  end;


procedure fpc_dynarray_concat(var dest : pointer; pti: pointer; const src1,src2 : pointer); compilerproc;
  var
    i,
    offset,
    totallen : sizeint;
    newp,
    realp,
    srealp : pdynarray;
    ti : pointer;
    elesize : sizeint;
    eletypemngd : pointer;
  begin
    totallen:=0;
    if assigned(src1) then
      inc(totallen,pdynarray(src1-sizeof(tdynarray))^.high+1);
    if assigned(src2) then
      inc(totallen,pdynarray(src2-sizeof(tdynarray))^.high+1);

    if totallen=0 then
      begin
        fpc_dynarray_clear(dest,pti);
        exit;
      end;

    { skip kind and name }
    ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);

    elesize:=pdynarraytypedata(ti)^.elSize;

    { only set if type needs initialization }
    if assigned(pdynarraytypedata(ti)^.elType) then
      eletypemngd:=pdynarraytypedata(ti)^.elType^
    else
      eletypemngd:=nil;

    { the idea to reuse the first array, re-allocate it and append the other entries is not possible as the first entry
      might be finalized later on by the caller however in case of a re-allocate, the entry itself might be gone }
    { allocate new array }
    getmem(newp,totallen*elesize+sizeof(tdynarray));

    { copy the elements of each source array }
    offset:=0;
    if assigned(src1) then
      begin
        srealp:=pdynarray(src1-sizeof(tdynarray));
        if srealp^.high>=0 then
          begin
            move(src1^,(pointer(newp)+sizeof(tdynarray)+offset*elesize)^,(srealp^.high+1)*elesize);
            inc(offset,srealp^.high+1);
          end;
      end;

    if assigned(src2) then
      begin
        srealp:=pdynarray(src2-sizeof(tdynarray));
        if srealp^.high>=0 then
          move(src2^,(pointer(newp)+sizeof(tdynarray)+offset*elesize)^,(srealp^.high+1)*elesize);
      end;

    { increase reference counts of all the elements }
    if assigned(eletypemngd) then
      begin
        for i:=0 to totallen-1 do
          int_addref(pointer(newp)+sizeof(tdynarray)+i*elesize,eletypemngd);
      end;

    { clear at the end, dest could be a reference to an array being also source }
    fpc_dynarray_clear(dest,pti);
    dest:=pointer(newp)+sizeof(tdynarray);
    newp^.refcount:=1;
    newp^.high:=totallen-1;
  end;
{$endif VER3_0}


procedure DynArraySetLength(var a: Pointer; typeInfo: Pointer; dimCnt: SizeInt; lengthVec: PSizeInt);
  external name 'FPC_DYNARR_SETLENGTH';

function DynArraySize(a : pointer): tdynarrayindex;
  external name 'FPC_DYNARRAY_LENGTH';

procedure DynArrayClear(var a: Pointer; typeInfo: Pointer);
  external name 'FPC_DYNARRAY_CLEAR';

function DynArrayDim(typeInfo: Pointer): Integer;
  begin
    result:=0;
    while (typeInfo <> nil) and (pdynarraytypeinfo(typeInfo)^.kind = tkDynArray) do
    begin
      { skip kind and name }
{$ifdef VER3_0}
      typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
{$else VER3_0}
      typeInfo:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
{$endif VER3_0}

      { element type info}
      {$ifdef VER3_0}
      typeInfo:=pdynarraytypedata(typeInfo)^.elType2;
      {$else VER3_0}
      typeInfo:=pdynarraytypedata(typeInfo)^.elType2^;
      {$endif VER3_0}

      Inc(result);
    end;
  end;

function DynArrayBounds(a: Pointer; typeInfo: Pointer): TBoundArray;
  var
    i,dim: sizeint;
  begin
    dim:=DynArrayDim(typeInfo);
    SetLength(result, dim);

    for i:=0 to pred(dim) do
      if a = nil then
        exit
      else
      begin
        result[i]:=DynArraySize(a)-1;
        a:=PPointerArray(a)^[0];
      end;
  end;

function IsDynArrayRectangular(a: Pointer; typeInfo: Pointer): Boolean;
  var
    i,j: sizeint;
    dim,count: sizeint;
  begin
    dim:=DynArrayDim(typeInfo);
    for i:=1 to pred(dim) do
    begin
      count:=DynArraySize(PPointerArray(a)^[0]);

      for j:=1 to Pred(DynArraySize(a)) do
        if count<>DynArraySize(PPointerArray(a)^[j]) then
          exit(false);

      a:=PPointerArray(a)^[0];
    end;
    result:=true;
  end;

function DynArrayIndex(a: Pointer; const indices: array of SizeInt; typeInfo: Pointer): Pointer;
  var
    i,h: sizeint;
    elsize: sizeuint;
  begin
    h:=High(indices);
    for i:=0 to h do
    begin
      if i<h then
        a := PPointerArray(a)^[indices[i]];

      { skip kind and name }
{$ifdef VER3_0}
      typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
{$else VER3_0}
      typeInfo:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
{$endif VER3_0}

      { store the last element size for the index calculation }
      elsize:=pdynarraytypedata(typeInfo)^.elSize;
      { element type info}
      {$ifdef VER3_0}
      typeInfo:=pdynarraytypedata(typeInfo)^.elType2;
      {$else VER3_0}
      typeInfo:=pdynarraytypedata(typeInfo)^.elType2^;
      {$endif VER3_0}

      if typeInfo=nil then
        exit(nil);
    end;

    result:=@(PByte(a)[indices[h]*elsize]);
  end;

{ obsolete but needed for bootstrapping }
procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_DECR_REF']; compilerproc;
  begin
    fpc_dynarray_clear(p,ti);
  end;