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