dynarr.inc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2000 by Florian Klaempfl
  4. member of the Free Pascal development team.
  5. This file implements the helper routines for dyn. Arrays in FPC
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************
  12. }
  13. type
  14. { don't add new fields, the size is used }
  15. { to calculate memory requirements }
  16. pdynarray = ^tdynarray;
  17. { removed packed here as
  18. 1) both fields have typically the same size (2, 4 or 8 bytes), if this is not the case, packed
  19. should be used only for this architecture
  20. 2) the memory blocks are sufficiently well aligned
  21. 3) in particular 64 bit CPUs which require natural alignment suffer from
  22. the packed as it causes each field access being split in 8 single loads and appropriate shift operations
  23. }
  24. tdynarray = { packed } record
  25. refcount : ptrint;
  26. high : tdynarrayindex;
  27. end;
  28. pdynarraytypedata = ^tdynarraytypedata;
  29. tdynarraytypedata =
  30. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  31. packed
  32. {$else}
  33. {$ifdef powerpc64}
  34. { 3.0.0 does not align elType field on a 8-byte boundary,
  35. thus use packed also in this case }
  36. {$ifdef VER3_0_0}
  37. packed
  38. {$endif VER3_0_0}
  39. {$endif powerpc64}
  40. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  41. record
  42. {$if declared(TRttiDataCommon)}
  43. common: TRttiDataCommon;
  44. {$endif declared TRttiDataCommon}
  45. case TTypeKind of
  46. tkArray: (
  47. elSize : SizeUInt;
  48. {$ifdef VER3_0}
  49. elType2 : Pointer;
  50. {$else}
  51. elType2 : PPointer;
  52. {$endif}
  53. varType : Longint;
  54. {$ifdef VER3_0}
  55. elType : Pointer;
  56. {$else}
  57. elType : PPointer;
  58. {$endif}
  59. );
  60. { include for proper alignment }
  61. tkInt64: (
  62. dummy : Int64
  63. );
  64. end;
  65. procedure fpc_dynarray_rangecheck(p : pointer;i : tdynarrayindex);[Public,Alias:'FPC_DYNARRAY_RANGECHECK']; compilerproc;
  66. begin
  67. if not(assigned(p)) or (i<0) or (i>pdynarray(p-sizeof(tdynarray))^.high) then
  68. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  69. end;
  70. function fpc_dynarray_length(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_LENGTH']; compilerproc;
  71. begin
  72. if assigned(p) then
  73. fpc_dynarray_length:=pdynarray(p-sizeof(tdynarray))^.high+1
  74. else
  75. fpc_dynarray_length:=0;
  76. end;
  77. function fpc_dynarray_high(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_HIGH']; compilerproc;
  78. begin
  79. if assigned(p) then
  80. fpc_dynarray_high:=pdynarray(p-sizeof(tdynarray))^.high
  81. else
  82. fpc_dynarray_high:=-1;
  83. end;
  84. procedure fpc_dynarray_clear(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_CLEAR']; compilerproc;
  85. var
  86. realp : pdynarray;
  87. begin
  88. if (P=Nil) then
  89. exit;
  90. realp:=pdynarray(p-sizeof(tdynarray));
  91. if realp^.refcount=0 then
  92. HandleErrorAddrFrameInd(204,get_pc_addr,get_frame);
  93. if (realp^.refcount>0) and declocked(realp^.refcount) then
  94. begin
  95. {$ifdef VER3_0}
  96. ti:=aligntoptr(ti+2+PByte(ti)[1]);
  97. {$else VER3_0}
  98. ti:=aligntoqword(ti+2+PByte(ti)[1]);
  99. {$endif VER3_0}
  100. if assigned(pdynarraytypedata(ti)^.elType) then
  101. int_finalizearray(p,pdynarraytypedata(ti)^.elType{$ifndef VER3_0}^{$endif},realp^.high+1);
  102. freemem(realp);
  103. end;
  104. p:=nil;
  105. end;
  106. { alias for internal use }
  107. Procedure fpc_dynarray_clear (var p : pointer;ti : pointer);[external name 'FPC_DYNARRAY_CLEAR'];
  108. procedure fpc_dynarray_incr_ref(p : pointer);[Public,Alias:'FPC_DYNARRAY_INCR_REF']; compilerproc;
  109. var
  110. realp : pdynarray;
  111. begin
  112. if p=nil then
  113. exit;
  114. realp:=pdynarray(p-sizeof(tdynarray));
  115. if realp^.refcount=0 then
  116. HandleErrorAddrFrameInd(204,get_pc_addr,get_frame)
  117. else if realp^.refcount>0 then
  118. inclocked(realp^.refcount);
  119. end;
  120. { provide local access to dynarr_decr_ref for dynarr_setlength }
  121. procedure fpc_dynarray_incr_ref(p : pointer); [external name 'FPC_DYNARRAY_INCR_REF'];
  122. procedure fpc_dynarray_assign(var dest: Pointer; src: Pointer; ti: pointer);[public,alias:'FPC_DYNARRAY_ASSIGN']; compilerproc;
  123. begin
  124. fpc_dynarray_incr_ref(src);
  125. fpc_dynarray_clear(dest,ti);
  126. Dest:=Src;
  127. end;
  128. procedure fpc_dynarray_assign(var dest: Pointer; src: Pointer; ti: pointer);[external name 'FPC_DYNARRAY_ASSIGN'];
  129. { provide local access to dynarr_setlength }
  130. procedure int_dynarray_setlength(var p : pointer;pti : pointer;
  131. dimcount : sizeint;dims : pdynarrayindex);[external name 'FPC_DYNARR_SETLENGTH'];
  132. procedure fpc_dynarray_setlength(var p : pointer;pti : pointer;
  133. dimcount : sizeint;dims : pdynarrayindex);[Public,Alias:'FPC_DYNARR_SETLENGTH']; compilerproc;
  134. var
  135. i : tdynarrayindex;
  136. movelen,
  137. size : sizeint;
  138. { contains the "fixed" pointers where the refcount }
  139. { and high are at positive offsets }
  140. realp,newp : pdynarray;
  141. ti : pointer;
  142. elesize : sizeint;
  143. eletype,eletypemngd : pointer;
  144. movsize : sizeint;
  145. begin
  146. { negative or zero length? }
  147. if dims[0]<=0 then
  148. { negative length is not allowed }
  149. if dims[0]<0 then
  150. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame)
  151. else
  152. begin
  153. { if the new dimension is 0, we've to release all data }
  154. fpc_dynarray_clear(p,pti);
  155. exit;
  156. end;
  157. { skip kind and name }
  158. {$ifdef VER3_0}
  159. ti:=aligntoptr(Pointer(pti)+2+PByte(pti)[1]);
  160. {$else VER3_0}
  161. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  162. {$endif VER3_0}
  163. elesize:=pdynarraytypedata(ti)^.elSize;
  164. {$ifdef VER3_0}
  165. eletype:=pdynarraytypedata(ti)^.elType2;
  166. {$else}
  167. eletype:=pdynarraytypedata(ti)^.elType2^;
  168. {$endif}
  169. { only set if type needs finalization }
  170. {$ifdef VER3_0}
  171. eletypemngd:=pdynarraytypedata(ti)^.elType;
  172. {$else}
  173. if assigned(pdynarraytypedata(ti)^.elType) then
  174. eletypemngd:=pdynarraytypedata(ti)^.elType^
  175. else
  176. eletypemngd:=nil;
  177. {$endif}
  178. { determine new memory size, throw a runtime error on overflow }
  179. {$push} {$q+,r+}
  180. size:=elesize*dims[0]+sizeof(tdynarray);
  181. {$pop}
  182. { not assigned yet? }
  183. if not(assigned(p)) then
  184. begin
  185. newp:=AllocMem(size);
  186. {$ifndef VER3_0}
  187. { call int_InitializeArray for management operators }
  188. if assigned(eletypemngd) and (PTypeKind(eletype)^ in [tkRecord, tkObject]) then
  189. int_InitializeArray(pointer(newp)+sizeof(tdynarray), eletype, dims[0]);
  190. {$endif VER3_0}
  191. end
  192. else
  193. begin
  194. realp:=pdynarray(p-sizeof(tdynarray));
  195. newp := realp;
  196. if realp^.refcount<>1 then
  197. begin
  198. { make an unique copy }
  199. newp:=getmem(size);
  200. fillchar(newp^,sizeof(tdynarray),0);
  201. if realp^.high < dims[0] then
  202. movelen := realp^.high+1
  203. else
  204. movelen := dims[0];
  205. movsize := elesize*movelen;
  206. move(p^,(pointer(newp)+sizeof(tdynarray))^, movsize);
  207. if size-sizeof(tdynarray)>movsize then
  208. begin
  209. fillchar((pointer(newp)+sizeof(tdynarray)+movsize)^,size-sizeof(tdynarray)-movsize,0);
  210. {$ifndef VER3_0}
  211. if assigned(eletypemngd) and (PTypeKind(eletype)^ in [tkRecord, tkObject]) then
  212. int_InitializeArray(pointer(newp)+sizeof(tdynarray)+movsize, eletype, dims[0]-movelen);
  213. {$endif VER3_0}
  214. end;
  215. { increment ref. count of managed members }
  216. if assigned(eletypemngd) then
  217. int_AddRefArray(pointer(newp)+sizeof(tdynarray),eletypemngd,movelen);
  218. { a declock(ref. count) isn't enough here }
  219. { it could be that the in MT environments }
  220. { in the mean time the refcount was }
  221. { decremented }
  222. { it is, because it doesn't really matter }
  223. { if the array is now removed }
  224. fpc_dynarray_clear(p,pti);
  225. end
  226. else if dims[0]<newp^.high+1 then
  227. begin
  228. { shrink the array }
  229. if assigned(eletypemngd) then
  230. int_finalizearray(pointer(newp)+sizeof(tdynarray)+
  231. elesize*dims[0],
  232. eletypemngd,newp^.high-dims[0]+1);
  233. reallocmem(realp,size);
  234. newp := realp;
  235. end
  236. else if dims[0]>newp^.high+1 then
  237. begin
  238. { grow the array }
  239. reallocmem(realp,size);
  240. newp := realp;
  241. fillchar((pointer(newp)+sizeof(tdynarray)+elesize*(newp^.high+1))^,
  242. (dims[0]-newp^.high-1)*elesize,0);
  243. {$ifndef VER3_0}
  244. { call int_InitializeArray for management operators }
  245. if assigned(eletypemngd) and (PTypeKind(eletype)^ in [tkRecord, tkObject]) then
  246. int_InitializeArray(pointer(newp)+sizeof(tdynarray)+elesize*(newp^.high+1),
  247. eletype, dims[0]-newp^.high-1);
  248. {$endif VER3_0}
  249. end;
  250. end;
  251. p:=pointer(newp)+sizeof(tdynarray);
  252. newp^.refcount:=1;
  253. newp^.high:=dims[0]-1;
  254. { handle nested arrays }
  255. if dimcount>1 then
  256. begin
  257. for i:=0 to dims[0]-1 do
  258. int_dynarray_setlength(pointer((pointer(newp)+sizeof(tdynarray)+i*elesize)^),
  259. eletype,dimcount-1,@dims[1]);
  260. end;
  261. end;
  262. { provide local access to array_to_dynarray_copy }
  263. function int_array_to_dynarray_copy(psrc : pointer;ti : pointer;
  264. lowidx,count,maxcount:tdynarrayindex;
  265. elesize : sizeint;
  266. eletype : pointer
  267. ) : fpc_stub_dynarray;[external name 'FPC_ARR_TO_DYNARR_COPY'];
  268. {$if defined(VER3_0) or defined(VER3_2)}
  269. function fpc_dynarray_copy(psrc : pointer;ti : pointer;
  270. lowidx,count:tdynarrayindex) : fpc_stub_dynarray;[Public,Alias:'FPC_DYNARR_COPY'];compilerproc;
  271. var
  272. realpsrc : pdynarray;
  273. eletype,tti : pointer;
  274. elesize : sizeint;
  275. begin
  276. fpc_dynarray_clear(pointer(result),ti);
  277. if psrc=nil then
  278. exit;
  279. realpsrc:=pdynarray(psrc-sizeof(tdynarray));
  280. {$ifdef VER3_0}
  281. tti:=aligntoptr(ti+2+PByte(ti)[1]);
  282. {$else VER3_0}
  283. tti:=aligntoqword(ti+2+PByte(ti)[1]);
  284. {$endif VER3_0}
  285. elesize:=pdynarraytypedata(tti)^.elSize;
  286. {$ifdef VER3_0}
  287. eletype:=pdynarraytypedata(tti)^.elType;
  288. {$else VER3_0}
  289. { only set if type needs finalization }
  290. if assigned(pdynarraytypedata(tti)^.elType) then
  291. eletype:=pdynarraytypedata(tti)^.elType^
  292. else
  293. eletype:=nil;
  294. {$endif VER3_0}
  295. fpc_array_to_dynarray_copy(psrc,ti,lowidx,count,realpsrc^.high+1,elesize,eletype);
  296. end;
  297. {$endif VER3_0 or VER3_2}
  298. { copy a custom array (open/dynamic/static) to dynamic array }
  299. function fpc_array_to_dynarray_copy(psrc : pointer;ti : pointer;
  300. lowidx,count,maxcount:tdynarrayindex;
  301. elesize : sizeint;
  302. eletype : pointer
  303. ) : fpc_stub_dynarray;[Public,Alias:'FPC_ARR_TO_DYNARR_COPY'];compilerproc;
  304. var
  305. size : sizeint;
  306. begin
  307. fpc_dynarray_clear(pointer(result),ti);
  308. if psrc=nil then
  309. exit;
  310. {$ifndef FPC_DYNARRAYCOPY_FIXED}
  311. if (lowidx=-1) and (count=-1) then
  312. begin
  313. lowidx:=0;
  314. count:=high(tdynarrayindex);
  315. end;
  316. {$endif FPC_DYNARRAYCOPY_FIXED}
  317. if (lowidx<0) then
  318. begin
  319. { Decrease count if index is negative, this is different from how copy()
  320. works on strings. Checked against D7. }
  321. if count<=0 then
  322. exit; { may overflow when adding lowidx }
  323. count:=count+lowidx;
  324. lowidx:=0;
  325. end;
  326. if (count>maxcount-lowidx) then
  327. count:=maxcount-lowidx;
  328. if count<=0 then
  329. exit;
  330. { create new array }
  331. size:=elesize*count;
  332. getmem(pointer(result),size+sizeof(tdynarray));
  333. pdynarray(result)^.refcount:=1;
  334. pdynarray(result)^.high:=count-1;
  335. inc(pointer(result),sizeof(tdynarray));
  336. { copy data }
  337. move(pointer(psrc+elesize*lowidx)^,pointer(result)^,size);
  338. { increment ref. count of members? }
  339. if assigned(eletype) then
  340. int_AddRefArray(pointer(result),eletype,count);
  341. end;
  342. {$ifndef VER3_0}
  343. procedure fpc_dynarray_delete(var p : pointer;source,count : SizeInt;pti : pointer);
  344. var
  345. newlen : tdynarrayindex;
  346. elesize : sizeint;
  347. { oldp is the same as p, actual header is accessed as oldp[-1].
  348. newp fairly points to the new header, array data starts at newp[1].
  349. realp takes the hit of being a var-parameter to ReallocMem not eligible for living in a register. }
  350. oldp,newp,realp : pdynarray;
  351. ti,eletypemngd : pointer;
  352. begin
  353. oldp:=p;
  354. if not assigned(oldp) or (count<=0) then
  355. exit;
  356. newlen:=oldp[-1].high+1;
  357. { Checks source < 0 or source >= len, using the fact that len is never negative. }
  358. if SizeUint(source)>=SizeUint(newlen) then
  359. exit;
  360. { cap count, and maybe delete whole array }
  361. if count>=newlen-source then
  362. begin
  363. if source=0 then
  364. begin
  365. fpc_dynarray_clear(p,pti);
  366. exit;
  367. end;
  368. count:=newlen-source;
  369. end;
  370. { skip kind and name }
  371. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  372. elesize:=pdynarraytypedata(ti)^.elSize;
  373. { only set if type needs finalization }
  374. eletypemngd:=pdynarraytypedata(ti)^.elType;
  375. if assigned(eletypemngd) then
  376. eletypemngd:=PPointer(eletypemngd)^;
  377. newlen:=newlen-count;
  378. if oldp[-1].refcount<>1 then
  379. begin
  380. { make an unique copy }
  381. newp:=getmem(elesize*newlen+sizeof(tdynarray));
  382. newp^.refcount:=1;
  383. { copy the elements that we still need }
  384. move(oldp^,pointer(newp+1)^,source*elesize);
  385. move((pointer(oldp)+(source+count)*elesize)^,(pointer(newp+1)+source*elesize)^,(newlen-source)*elesize);
  386. { increment ref. count of managed members }
  387. if assigned(eletypemngd) then
  388. int_AddRefArray(newp+1,eletypemngd,newlen);
  389. { a declock(ref. count) isn't enough here }
  390. { it could be that the in MT environments }
  391. { in the mean time the refcount was }
  392. { decremented }
  393. { it is, because it doesn't really matter }
  394. { if the array is now removed }
  395. fpc_dynarray_clear(p,pti);
  396. end
  397. else
  398. begin
  399. { finalize the elements that will be removed }
  400. if assigned(eletypemngd) then
  401. int_FinalizeArray(pointer(oldp)+source*elesize,eletypemngd,count);
  402. { close the gap by moving the trailing elements to the front }
  403. move((pointer(oldp)+(source+count)*elesize)^,(pointer(oldp)+source*elesize)^,(newlen-source)*elesize);
  404. { resize the array }
  405. realp:=oldp-1;
  406. newp:=reallocmem(realp,elesize*newlen+sizeof(tdynarray));
  407. end;
  408. newp^.high:=newlen-1;
  409. p:=newp+1;
  410. end;
  411. procedure fpc_dynarray_insert(var p : pointer;source : SizeInt;data : pointer;count : SizeInt;pti : pointer);compilerproc;
  412. var
  413. newlen : tdynarrayindex;
  414. elesize,dataofs : sizeint;
  415. oldp,newp,realp : pdynarray;
  416. ti,eletypemngd : pointer;
  417. begin
  418. if count=0 then
  419. exit;
  420. oldp:=p;
  421. if assigned(oldp) then
  422. begin
  423. dec(oldp);
  424. { cap insert index }
  425. newlen:=oldp^.high+1;
  426. if SizeUint(source)>SizeUint(newlen) then { Checks for not (0 <= source <= len), using the fact than 'newlen' is never negative. }
  427. if source<0 then
  428. source:=0
  429. else
  430. source:=newlen;
  431. newlen:=newlen+count;
  432. end
  433. else
  434. begin
  435. source:=0;
  436. newlen:=count;
  437. end;
  438. { skip kind and name }
  439. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  440. elesize:=pdynarraytypedata(ti)^.elSize;
  441. { only set if type needs initialization }
  442. eletypemngd:=pdynarraytypedata(ti)^.elType;
  443. if assigned(eletypemngd) then
  444. eletypemngd:=PPointer(eletypemngd)^;
  445. if not assigned(oldp) or (oldp^.refcount<>1) then
  446. begin
  447. newp:=getmem(elesize*newlen+sizeof(tdynarray));
  448. { copy leading elements. No-op when not Assigned(oldp) because in this case source = 0. }
  449. move(oldp[1],newp[1],source*elesize);
  450. { insert new elements }
  451. move(data^,(pointer(newp+1)+source*elesize)^,count*elesize);
  452. { copy trailing elements. This time must be careful with not Assigned(oldp). }
  453. if assigned(oldp) then
  454. move((pointer(oldp+1)+source*elesize)^,(pointer(newp+1)+(source+count)*elesize)^,(oldp^.high-source+1)*elesize);
  455. { increment ref. count of managed members }
  456. if assigned(eletypemngd) then
  457. int_AddRefArray(newp+1,eletypemngd,newlen);
  458. { a declock(ref. count) isn't enough here }
  459. { it could be that the in MT environments }
  460. { in the mean time the refcount was }
  461. { decremented }
  462. { it is, because it doesn't really matter }
  463. { if the array is now removed }
  464. fpc_dynarray_clear(p,pti);
  465. end
  466. else
  467. begin
  468. { dataofs >= 0 means that 'data' points into the source array with byte offset 'dataofs' from the header.
  469. dataofs < 0 means that 'data' does not point into the array. }
  470. dataofs:=-1;
  471. if (data>=oldp) and (data<=pointer(oldp+1)+oldp^.high*elesize) then
  472. dataofs:=data-pointer(oldp);
  473. { resize the array }
  474. realp:=oldp; { 'realp' as a 'var'-parameter avoids taking 'oldp' address. }
  475. newp:=reallocmem(realp,elesize*newlen+sizeof(tdynarray));
  476. { Fixup overlapping 'data'. }
  477. if dataofs>=0 then
  478. begin
  479. data:=pointer(newp)+dataofs;
  480. { If 'data' points into the trailing part, account for it being moved by 'count'. }
  481. if data>=pointer(newp+1)+source*elesize then
  482. data:=data+count*elesize;
  483. end;
  484. { move the trailing part after the inserted data }
  485. move((pointer(newp+1)+source*elesize)^,(pointer(newp+1)+(source+count)*elesize)^,(newp^.high-source+1)*elesize);
  486. { move the inserted data to the destination }
  487. move(data^,(pointer(newp+1)+source*elesize)^,count*elesize);
  488. { increase reference counts of inserted elements }
  489. if assigned(eletypemngd) then
  490. int_AddRefArray(pointer(newp+1)+source*elesize,eletypemngd,count);
  491. end;
  492. newp^.refcount:=1;
  493. newp^.high:=newlen-1;
  494. p:=newp+1;
  495. end;
  496. procedure fpc_dynarray_concat_multi(var dest : pointer; pti: pointer; const sarr:array of pointer); compilerproc;
  497. var
  498. i,firstnonempty,elesize,totallen,copybytes,skip : sizeint;
  499. newp,realp,copysrc,olddestp : pdynarray;
  500. ti,eletypemngd,copydest : pointer;
  501. begin
  502. totallen:=0;
  503. for i:=high(sarr) downto 0 do
  504. if assigned(sarr[i]) then
  505. begin
  506. inc(totallen,pdynarray(sarr[i])[-1].high+1);
  507. firstnonempty:=i; { 1) allows for append optimization to work even with some prepended []s, 2) required for the reuse optimization. }
  508. end;
  509. if totallen=0 then
  510. begin
  511. fpc_dynarray_clear(dest,pti);
  512. exit;
  513. end;
  514. { Reuse the only nonempty input? }
  515. if totallen=pdynarray(sarr[firstnonempty])[-1].high+1 then
  516. begin
  517. fpc_dynarray_assign(dest,sarr[firstnonempty],pti);
  518. exit;
  519. end;
  520. { skip kind and name }
  521. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  522. elesize:=pdynarraytypedata(ti)^.elSize;
  523. { only set if type needs initialization }
  524. eletypemngd:=pdynarraytypedata(ti)^.elType;
  525. if Assigned(eletypemngd) then
  526. eletypemngd:=PPointer(eletypemngd)^;
  527. { Can append? }
  528. olddestp:=dest;
  529. if (olddestp=sarr[firstnonempty]) and (olddestp[-1].refcount=1) then
  530. begin
  531. { Append, and be careful with 'dest' occuring among pieces. }
  532. realp:=olddestp-1;
  533. newp:=reallocmem(realp,totallen*elesize+sizeof(tdynarray));
  534. copydest:=pointer(newp+1)+(newp^.high+1)*elesize;
  535. inc(firstnonempty); { Start from the next element. }
  536. end
  537. else
  538. begin
  539. olddestp:=nil; { Append case is distinguished later as assigned(olddestp). }
  540. { allocate new array }
  541. newp:=getmem(totallen*elesize+sizeof(tdynarray));
  542. newp^.refcount:=1;
  543. copydest:=newp+1;
  544. end;
  545. while firstnonempty<=high(sarr) do
  546. begin
  547. copysrc:=sarr[firstnonempty];
  548. inc(firstnonempty);
  549. if not assigned(copysrc) then
  550. continue;
  551. if copysrc=olddestp then
  552. { Dest used as one of the pieces! Use new pointer instead. Array header still conveniently contains original 'high'.
  553. Can trigger only when appending, as otherwise olddestp = nil. }
  554. copysrc:=newp+1;
  555. copybytes:=(copysrc[-1].high+1)*elesize;
  556. move(copysrc^,copydest^,copybytes);
  557. inc(copydest,copybytes);
  558. end;
  559. if assigned(eletypemngd) then
  560. begin
  561. skip:=0;
  562. if assigned(olddestp) then
  563. skip:=newp^.high+1;
  564. int_AddRefArray(pointer(newp+1)+skip*elesize,eletypemngd,totallen-skip);
  565. end;
  566. if not assigned(olddestp) then
  567. { clear at the end, dest could be a reference to an array being used also as source }
  568. fpc_dynarray_clear(dest,pti);
  569. newp^.high:=totallen-1;
  570. dest:=newp+1;
  571. end;
  572. procedure fpc_dynarray_concat(var dest : pointer; pti: pointer; const src1,src2 : pointer); compilerproc;
  573. var
  574. totallen,elesize,ofs2 : sizeint;
  575. newp,realp,olddestp,copysrc : pdynarray;
  576. ti,eletypemngd : pointer;
  577. begin
  578. if not assigned(src1) or not assigned(src2) then
  579. begin
  580. fpc_dynarray_assign(dest, pointer(ptruint(src1) or ptruint(src2)), pti);
  581. exit; { From now on, both src1 and src2 are non-nil. }
  582. end;
  583. totallen:=pdynarray(src1)[-1].high+pdynarray(src2)[-1].high+2;
  584. { skip kind and name }
  585. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  586. elesize:=pdynarraytypedata(ti)^.elSize;
  587. { only set if type needs initialization }
  588. eletypemngd:=pdynarraytypedata(ti)^.elType;
  589. if assigned(eletypemngd) then
  590. eletypemngd:=PPointer(eletypemngd)^;
  591. olddestp:=dest;
  592. { Can append? }
  593. if (olddestp=src1) and (olddestp[-1].refcount=1) then
  594. begin
  595. { Append, and be careful with dest = src2. }
  596. realp:=olddestp-1;
  597. newp:=reallocmem(realp,totallen*elesize+sizeof(tdynarray));
  598. copysrc:=src2;
  599. if src2=olddestp then
  600. { Use new pointer instead. Array header still conveniently contains original 'high'. }
  601. copysrc:=newp+1;
  602. move(copysrc^,(pointer(newp+1)+(newp^.high+1)*elesize)^,(copysrc[-1].high+1)*elesize);
  603. if assigned(eletypemngd) then
  604. int_AddRefArray(pointer(newp+1)+(newp^.high+1)*elesize,eletypemngd,copysrc[-1].high+1);
  605. end
  606. else
  607. begin
  608. { allocate new array }
  609. newp:=getmem(totallen*elesize+sizeof(tdynarray));
  610. newp^.refcount:=1;
  611. ofs2:=(pdynarray(src1)[-1].high+1)*elesize;
  612. move(src1^,newp[1],ofs2);
  613. move(src2^,(pointer(newp+1)+ofs2)^,(pdynarray(src2)[-1].high+1)*elesize);
  614. { increase reference counts of all the elements }
  615. if assigned(eletypemngd) then
  616. int_AddRefArray(newp+1,eletypemngd,totallen);
  617. { clear at the end, dest could be a reference to an array being also source }
  618. fpc_dynarray_clear(dest,pti);
  619. end;
  620. newp^.high:=totallen-1;
  621. dest:=newp+1;
  622. end;
  623. {$endif VER3_0}
  624. procedure DynArraySetLength(var a: Pointer; typeInfo: Pointer; dimCnt: SizeInt; lengthVec: PSizeInt);
  625. external name 'FPC_DYNARR_SETLENGTH';
  626. function DynArraySize(a : pointer): tdynarrayindex;
  627. external name 'FPC_DYNARRAY_LENGTH';
  628. procedure DynArrayClear(var a: Pointer; typeInfo: Pointer);
  629. external name 'FPC_DYNARRAY_CLEAR';
  630. procedure DynArrayAssign(var dest: Pointer; src: Pointer; typeInfo: pointer);
  631. external name 'FPC_DYNARRAY_ASSIGN';
  632. function DynArrayDim(typeInfo: Pointer): Integer;
  633. begin
  634. result:=0;
  635. while (typeInfo <> nil) and (pdynarraytypeinfo(typeInfo)^.kind = tkDynArray) do
  636. begin
  637. { skip kind and name }
  638. {$ifdef VER3_0}
  639. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  640. {$else VER3_0}
  641. typeInfo:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  642. {$endif VER3_0}
  643. { element type info}
  644. {$ifdef VER3_0}
  645. typeInfo:=pdynarraytypedata(typeInfo)^.elType2;
  646. {$else VER3_0}
  647. typeInfo:=pdynarraytypedata(typeInfo)^.elType2^;
  648. {$endif VER3_0}
  649. Inc(result);
  650. end;
  651. end;
  652. function DynArrayBounds(a: Pointer; typeInfo: Pointer): TBoundArray;
  653. var
  654. i,dim: sizeint;
  655. begin
  656. dim:=DynArrayDim(typeInfo);
  657. SetLength(result, dim);
  658. for i:=0 to pred(dim) do
  659. if a = nil then
  660. exit
  661. else
  662. begin
  663. result[i]:=DynArraySize(a)-1;
  664. a:=PPointerArray(a)^[0];
  665. end;
  666. end;
  667. function IsDynArrayRectangular(a: Pointer; typeInfo: Pointer): Boolean;
  668. var
  669. i,j: sizeint;
  670. dim,count: sizeint;
  671. begin
  672. dim:=DynArrayDim(typeInfo);
  673. for i:=1 to pred(dim) do
  674. begin
  675. count:=DynArraySize(PPointerArray(a)^[0]);
  676. for j:=1 to Pred(DynArraySize(a)) do
  677. if count<>DynArraySize(PPointerArray(a)^[j]) then
  678. exit(false);
  679. a:=PPointerArray(a)^[0];
  680. end;
  681. result:=true;
  682. end;
  683. function DynArrayIndex(a: Pointer; const indices: array of SizeInt; typeInfo: Pointer): Pointer;
  684. var
  685. i,h: sizeint;
  686. begin
  687. h:=High(indices);
  688. for i:=0 to h do
  689. begin
  690. { skip kind and name }
  691. {$ifdef VER3_0}
  692. typeInfo:=aligntoptr(Pointer(typeInfo)+2+PByte(typeInfo)[1]);
  693. {$else VER3_0}
  694. typeInfo:=aligntoqword(Pointer(typeInfo)+2+PByte(typeInfo)[1]);
  695. {$endif VER3_0}
  696. if i=h then
  697. break;
  698. a := PPointerArray(a)^[indices[i]];
  699. { element type info}
  700. {$ifdef VER3_0}
  701. typeInfo:=pdynarraytypedata(typeInfo)^.elType2;
  702. {$else VER3_0}
  703. typeInfo:=pdynarraytypedata(typeInfo)^.elType2^;
  704. {$endif VER3_0}
  705. end;
  706. result:=a+SizeUint(indices[h])*pdynarraytypedata(typeInfo)^.elSize;
  707. end;
  708. { obsolete but needed for bootstrapping }
  709. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_DECR_REF']; compilerproc;
  710. begin
  711. fpc_dynarray_clear(p,ti);
  712. end;