dynarr.inc 27 KB

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