dynarr.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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,skip : 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. { Can append? }
  480. olddestp:=dest;
  481. if (olddestp=sarr[firstnonempty]) and (olddestp[-1].refcount=1) then
  482. begin
  483. { Append, and be careful with 'dest' occuring among pieces. }
  484. realp:=olddestp-1;
  485. newp:=reallocmem(realp,totallen*elesize+sizeof(tdynarray));
  486. copydest:=pointer(newp+1)+(newp^.high+1)*elesize;
  487. inc(firstnonempty); { Start from the next element. }
  488. end
  489. else
  490. begin
  491. olddestp:=nil; { Append case is distinguished later as assigned(olddestp). }
  492. { allocate new array }
  493. newp:=getmem(totallen*elesize+sizeof(tdynarray));
  494. newp^.refcount:=1;
  495. copydest:=newp+1;
  496. end;
  497. while firstnonempty<=high(sarr) do
  498. begin
  499. copysrc:=sarr[firstnonempty];
  500. inc(firstnonempty);
  501. if not assigned(copysrc) then
  502. continue;
  503. if copysrc=olddestp then
  504. { Dest used as one of the pieces! Use new pointer instead. Array header still conveniently contains original 'high'.
  505. Can trigger only when appending, as otherwise olddestp = nil. }
  506. copysrc:=newp+1;
  507. copybytes:=(copysrc[-1].high+1)*elesize;
  508. move(copysrc^,copydest^,copybytes);
  509. inc(copydest,copybytes);
  510. end;
  511. if assigned(eletypemngd) then
  512. begin
  513. skip:=0;
  514. if assigned(olddestp) then
  515. skip:=newp^.high+1;
  516. int_AddRefArray(pointer(newp+1)+skip*elesize,eletypemngd,totallen-skip);
  517. end;
  518. if not assigned(olddestp) then
  519. { clear at the end, dest could be a reference to an array being used also as source }
  520. fpc_dynarray_clear(dest,pti);
  521. newp^.high:=totallen-1;
  522. dest:=newp+1;
  523. end;
  524. procedure fpc_dynarray_concat(var dest : pointer; pti: pointer; const src1,src2 : pointer); compilerproc;
  525. var
  526. totallen,elesize,ofs2 : sizeint;
  527. newp,realp,olddestp,copysrc : pdynarray;
  528. ti,eletypemngd : pointer;
  529. begin
  530. if not assigned(src1) or not assigned(src2) then
  531. begin
  532. fpc_dynarray_assign(dest, pointer(ptruint(src1) or ptruint(src2)), pti);
  533. exit; { From now on, both src1 and src2 are non-nil. }
  534. end;
  535. totallen:=pdynarray(src1)[-1].high+pdynarray(src2)[-1].high+2;
  536. { skip kind and name }
  537. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  538. elesize:=pdynarraytypedata(ti)^.elSize;
  539. { only set if type needs initialization }
  540. eletypemngd:=pdynarraytypedata(ti)^.elType;
  541. if assigned(eletypemngd) then
  542. eletypemngd:=PPointer(eletypemngd)^;
  543. olddestp:=dest;
  544. { Can append? }
  545. if (olddestp=src1) and (olddestp[-1].refcount=1) then
  546. begin
  547. { Append, and be careful with dest = src2. }
  548. realp:=olddestp-1;
  549. newp:=reallocmem(realp,totallen*elesize+sizeof(tdynarray));
  550. copysrc:=src2;
  551. if src2=olddestp then
  552. { Use new pointer instead. Array header still conveniently contains original 'high'. }
  553. copysrc:=newp+1;
  554. move(copysrc^,(pointer(newp+1)+(newp^.high+1)*elesize)^,(copysrc[-1].high+1)*elesize);
  555. if assigned(eletypemngd) then
  556. int_AddRefArray(pointer(newp+1)+(newp^.high+1)*elesize,eletypemngd,copysrc[-1].high+1);
  557. end
  558. else
  559. begin
  560. { allocate new array }
  561. newp:=getmem(totallen*elesize+sizeof(tdynarray));
  562. newp^.refcount:=1;
  563. ofs2:=(pdynarray(src1)[-1].high+1)*elesize;
  564. move(src1^,newp[1],ofs2);
  565. move(src2^,(pointer(newp+1)+ofs2)^,(pdynarray(src2)[-1].high+1)*elesize);
  566. { increase reference counts of all the elements }
  567. if assigned(eletypemngd) then
  568. int_AddRefArray(newp+1,eletypemngd,totallen);
  569. { clear at the end, dest could be a reference to an array being also source }
  570. fpc_dynarray_clear(dest,pti);
  571. end;
  572. newp^.high:=totallen-1;
  573. dest:=newp+1;
  574. end;
  575. procedure DynArraySetLength(var a: Pointer; typeInfo: Pointer; dimCnt: SizeInt; lengthVec: PSizeInt);
  576. external name 'FPC_DYNARR_SETLENGTH';
  577. function DynArraySize(a : pointer): tdynarrayindex;
  578. external name 'FPC_DYNARRAY_LENGTH';
  579. procedure DynArrayClear(var a: Pointer; typeInfo: Pointer);
  580. external name 'FPC_DYNARRAY_CLEAR';
  581. procedure DynArrayAssign(var dest: Pointer; src: Pointer; typeInfo: pointer);
  582. external name 'FPC_DYNARRAY_ASSIGN';
  583. function DynArrayDim(typeInfo: Pointer): Integer;
  584. begin
  585. result:=0;
  586. while (typeInfo <> nil) and (pdynarraytypeinfo(typeInfo)^.kind = tkDynArray) do
  587. begin
  588. { skip kind and name }
  589. typeInfo:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  590. { element type info}
  591. typeInfo:=pdynarraytypedata(typeInfo)^.elType2^;
  592. Inc(result);
  593. end;
  594. end;
  595. function DynArrayBounds(a: Pointer; typeInfo: Pointer): TBoundArray;
  596. var
  597. i,dim: sizeint;
  598. begin
  599. dim:=DynArrayDim(typeInfo);
  600. SetLength(result, dim);
  601. for i:=0 to pred(dim) do
  602. if a = nil then
  603. exit
  604. else
  605. begin
  606. result[i]:=DynArraySize(a)-1;
  607. a:=PPointerArray(a)^[0];
  608. end;
  609. end;
  610. function IsDynArrayRectangular(a: Pointer; typeInfo: Pointer): Boolean;
  611. var
  612. i,j: sizeint;
  613. dim,count: sizeint;
  614. begin
  615. dim:=DynArrayDim(typeInfo);
  616. for i:=1 to pred(dim) do
  617. begin
  618. count:=DynArraySize(PPointerArray(a)^[0]);
  619. for j:=1 to Pred(DynArraySize(a)) do
  620. if count<>DynArraySize(PPointerArray(a)^[j]) then
  621. exit(false);
  622. a:=PPointerArray(a)^[0];
  623. end;
  624. result:=true;
  625. end;
  626. function DynArrayIndex(a: Pointer; const indices: array of SizeInt; typeInfo: Pointer): Pointer;
  627. var
  628. i,h: sizeint;
  629. begin
  630. h:=High(indices);
  631. for i:=0 to h do
  632. begin
  633. { skip kind and name }
  634. typeInfo:=aligntoqword(Pointer(typeInfo)+2+PByte(typeInfo)[1]);
  635. if i=h then
  636. break;
  637. a := PPointerArray(a)^[indices[i]];
  638. { element type info}
  639. typeInfo:=pdynarraytypedata(typeInfo)^.elType2^;
  640. end;
  641. result:=a+SizeUint(indices[h])*pdynarraytypedata(typeInfo)^.elSize;
  642. end;
  643. { obsolete but needed for bootstrapping }
  644. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_DECR_REF']; compilerproc;
  645. begin
  646. fpc_dynarray_clear(p,ti);
  647. end;