dynarr.inc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. newhigh : tdynarrayindex;
  346. size : sizeint;
  347. { contains the "fixed" pointers where the refcount }
  348. { and high are at positive offsets }
  349. realp,newp : pdynarray;
  350. ti : pointer;
  351. elesize : sizeint;
  352. eletype,eletypemngd : pointer;
  353. begin
  354. { if source > high then nothing to do }
  355. if not assigned(p) or
  356. (source>pdynarray(p-sizeof(tdynarray))^.high) or
  357. (count<=0) or
  358. (source<0) then
  359. exit;
  360. { cap count }
  361. if source+count-1>pdynarray(p-sizeof(tdynarray))^.high then
  362. count:=pdynarray(p-sizeof(tdynarray))^.high-source+1;
  363. { fast path: delete whole array }
  364. if (source=0) and (count=pdynarray(p-sizeof(tdynarray))^.high+1) then
  365. begin
  366. fpc_dynarray_clear(p,pti);
  367. exit;
  368. end;
  369. { skip kind and name }
  370. {$ifdef VER3_0}
  371. ti:=aligntoptr(Pointer(pti)+2+PByte(pti)[1]);
  372. {$else VER3_0}
  373. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  374. {$endif VER3_0}
  375. elesize:=pdynarraytypedata(ti)^.elSize;
  376. eletype:=pdynarraytypedata(ti)^.elType2^;
  377. { only set if type needs finalization }
  378. if assigned(pdynarraytypedata(ti)^.elType) then
  379. eletypemngd:=pdynarraytypedata(ti)^.elType^
  380. else
  381. eletypemngd:=nil;
  382. realp:=pdynarray(p-sizeof(tdynarray));
  383. newp:=realp;
  384. { determine new memory size }
  385. newhigh:=realp^.high-count;
  386. size:=elesize*(newhigh+1)+sizeof(tdynarray);
  387. if realp^.refcount<>1 then
  388. begin
  389. { make an unique copy }
  390. getmem(newp,size);
  391. fillchar(newp^,sizeof(tdynarray),0);
  392. { copy the elements that we still need }
  393. if source>0 then
  394. move(p^,(pointer(newp)+sizeof(tdynarray))^,source*elesize);
  395. if source+count-1<realp^.high then
  396. move((p+(source+count)*elesize)^,(pointer(newp)+sizeof(tdynarray)+source*elesize)^,(realp^.high-(source+count)+1)*elesize);
  397. { increment ref. count of managed members }
  398. if assigned(eletypemngd) then
  399. int_AddRefArray(pointer(newp)+sizeof(tdynarray),eletypemngd,newhigh+1);
  400. { a declock(ref. count) isn't enough here }
  401. { it could be that the in MT environments }
  402. { in the mean time the refcount was }
  403. { decremented }
  404. { it is, because it doesn't really matter }
  405. { if the array is now removed }
  406. fpc_dynarray_clear(p,pti);
  407. end
  408. else
  409. begin
  410. { finalize the elements that will be removed }
  411. if assigned(eletypemngd) then
  412. int_FinalizeArray(p+source*elesize,eletype,count);
  413. { close the gap by moving the trailing elements to the front }
  414. move((p+(source+count)*elesize)^,(p+source*elesize)^,(realp^.high-(source+count)+1)*elesize);
  415. { resize the array }
  416. reallocmem(realp,size);
  417. newp:=realp;
  418. end;
  419. p:=pointer(newp)+sizeof(tdynarray);
  420. newp^.refcount:=1;
  421. newp^.high:=newhigh;
  422. end;
  423. procedure fpc_dynarray_insert(var p : pointer;source : SizeInt;data : pointer;count : SizeInt;pti : pointer);compilerproc;
  424. var
  425. newhigh : tdynarrayindex;
  426. size : sizeint;
  427. realp,
  428. newp : pdynarray;
  429. ti : pointer;
  430. elesize : sizeint;
  431. eletype,eletypemngd : pointer;
  432. begin
  433. if not assigned(data) or
  434. (count=0) then
  435. exit;
  436. if assigned(p) then
  437. realp:=pdynarray(p-sizeof(tdynarray))
  438. else
  439. realp:=nil;
  440. newp:=realp;
  441. { cap insert index }
  442. if assigned(p) then
  443. begin
  444. if source<0 then
  445. source:=0
  446. else if source>realp^.high+1 then
  447. source:=realp^.high+1;
  448. end
  449. else
  450. source:=0;
  451. { skip kind and name }
  452. {$ifdef VER3_0}
  453. ti:=aligntoptr(Pointer(pti)+2+PByte(pti)[1]);
  454. {$else VER3_0}
  455. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  456. {$endif VER3_0}
  457. elesize:=pdynarraytypedata(ti)^.elSize;
  458. eletype:=pdynarraytypedata(ti)^.elType2^;
  459. { only set if type needs initialization }
  460. if assigned(pdynarraytypedata(ti)^.elType) then
  461. eletypemngd:=pdynarraytypedata(ti)^.elType^
  462. else
  463. eletypemngd:=nil;
  464. { determine new memory size }
  465. if assigned(p) then
  466. newhigh:=realp^.high+count
  467. else
  468. newhigh:=count-1;
  469. size:=elesize*(newhigh+1)+sizeof(tdynarray);
  470. if assigned(p) then
  471. begin
  472. if realp^.refcount<>1 then
  473. begin
  474. { make an unique copy }
  475. getmem(newp,size);
  476. fillchar(newp^,sizeof(tdynarray),0);
  477. { copy leading elements }
  478. if source>0 then
  479. move(p^,(pointer(newp)+sizeof(tdynarray))^,source*elesize);
  480. { insert new elements }
  481. move(data^,(pointer(newp)+sizeof(tdynarray)+source*elesize)^,count*elesize);
  482. { copy trailing elements }
  483. if realp^.high-source+1>0 then
  484. move((p+source*elesize)^,(pointer(newp)+sizeof(tdynarray)+(source+count)*elesize)^,(realp^.high-source+1)*elesize);
  485. { increment ref. count of managed members }
  486. if assigned(eletypemngd) then
  487. int_AddRefArray(pointer(newp)+sizeof(tdynarray),eletypemngd,newhigh+1);
  488. { a declock(ref. count) isn't enough here }
  489. { it could be that the in MT environments }
  490. { in the mean time the refcount was }
  491. { decremented }
  492. { it is, because it doesn't really matter }
  493. { if the array is now removed }
  494. fpc_dynarray_clear(p,pti);
  495. end
  496. else
  497. begin
  498. { resize the array }
  499. reallocmem(realp,size);
  500. { p might no longer be correct }
  501. p:=pointer(realp)+sizeof(tdynarray);
  502. { move the trailing part after the inserted data }
  503. if source<=realp^.high then
  504. move((p+source*elesize)^,(p+(source+count)*elesize)^,(realp^.high-source+1)*elesize);
  505. { move the inserted data to the destination }
  506. move(data^,(p+source*elesize)^,count*elesize);
  507. { increase reference counts of inserted elements }
  508. if assigned(eletypemngd) then
  509. int_AddRefArray(p+source*elesize,eletypemngd,count);
  510. newp:=realp;
  511. end;
  512. end
  513. else
  514. begin
  515. { allocate new array }
  516. getmem(newp,size);
  517. fillchar(newp^,sizeof(tdynarray),0);
  518. { insert data }
  519. move(data^,(pointer(newp)+sizeof(tdynarray))^,count*elesize);
  520. { increase reference counts of inserted elements }
  521. if assigned(eletypemngd) then
  522. int_AddRefArray(pointer(newp)+sizeof(tdynarray),eletypemngd,count);
  523. end;
  524. p:=pointer(newp)+sizeof(tdynarray);
  525. newp^.refcount:=1;
  526. newp^.high:=newhigh;
  527. end;
  528. procedure fpc_dynarray_concat_multi(var dest : pointer; pti: pointer; const sarr:array of pointer); compilerproc;
  529. var
  530. i,
  531. offset,
  532. totallen : sizeint;
  533. newp,
  534. realp,
  535. srealp : pdynarray;
  536. ti : pointer;
  537. elesize : sizeint;
  538. eletypemngd : pointer;
  539. begin
  540. { sanity check }
  541. if length(sarr)=0 then
  542. exit;
  543. totallen:=0;
  544. for i:=0 to high(sarr) do
  545. if assigned(sarr[i]) then
  546. inc(totallen,pdynarray(sarr[i]-sizeof(tdynarray))^.high+1);
  547. if totallen=0 then
  548. begin
  549. fpc_dynarray_clear(dest,pti);
  550. exit;
  551. end;
  552. { skip kind and name }
  553. {$ifdef VER3_0}
  554. ti:=aligntoptr(Pointer(pti)+2+PByte(pti)[1]);
  555. {$else VER3_0}
  556. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  557. {$endif VER3_0}
  558. elesize:=pdynarraytypedata(ti)^.elSize;
  559. { only set if type needs initialization }
  560. if assigned(pdynarraytypedata(ti)^.elType) then
  561. eletypemngd:=pdynarraytypedata(ti)^.elType^
  562. else
  563. eletypemngd:=nil;
  564. { copy the elements of each source array }
  565. offset:=0;
  566. { the idea to reuse the first array, re-allocate it and append the other entries is not possible as the first entry
  567. might be finalized later on by the caller however in case of a re-allocate, the entry itself might be gone }
  568. { allocate new array }
  569. getmem(newp,totallen*elesize+sizeof(tdynarray));
  570. for i:=0 to high(sarr) do
  571. if assigned(sarr[i]) then
  572. begin
  573. srealp:=pdynarray(sarr[i]-sizeof(tdynarray));
  574. if srealp^.high>=0 then
  575. begin
  576. move(sarr[i]^,(pointer(newp)+sizeof(tdynarray)+offset*elesize)^,(srealp^.high+1)*elesize);
  577. inc(offset,srealp^.high+1);
  578. end;
  579. end;
  580. { increase reference counts of all the elements }
  581. if assigned(eletypemngd) then
  582. int_AddRefArray(pointer(newp)+sizeof(tdynarray),eletypemngd,totallen);
  583. { clear at the end, dest could be a reference to an array being used also as source }
  584. fpc_dynarray_clear(dest,pti);
  585. dest:=pointer(newp)+sizeof(tdynarray);
  586. newp^.refcount:=1;
  587. newp^.high:=totallen-1;
  588. end;
  589. procedure fpc_dynarray_concat(var dest : pointer; pti: pointer; const src1,src2 : pointer); compilerproc;
  590. var
  591. offset,
  592. totallen : sizeint;
  593. newp,
  594. realp,
  595. srealp : pdynarray;
  596. ti : pointer;
  597. elesize : sizeint;
  598. eletypemngd : pointer;
  599. begin
  600. totallen:=0;
  601. if assigned(src1) then
  602. inc(totallen,pdynarray(src1-sizeof(tdynarray))^.high+1);
  603. if assigned(src2) then
  604. inc(totallen,pdynarray(src2-sizeof(tdynarray))^.high+1);
  605. if totallen=0 then
  606. begin
  607. fpc_dynarray_clear(dest,pti);
  608. exit;
  609. end;
  610. { skip kind and name }
  611. {$ifdef VER3_0}
  612. ti:=aligntoptr(Pointer(pti)+2+PByte(pti)[1]);
  613. {$else VER3_0}
  614. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  615. {$endif VER3_0}
  616. elesize:=pdynarraytypedata(ti)^.elSize;
  617. { only set if type needs initialization }
  618. if assigned(pdynarraytypedata(ti)^.elType) then
  619. eletypemngd:=pdynarraytypedata(ti)^.elType^
  620. else
  621. eletypemngd:=nil;
  622. { the idea to reuse the first array, re-allocate it and append the other entries is not possible as the first entry
  623. might be finalized later on by the caller however in case of a re-allocate, the entry itself might be gone }
  624. { allocate new array }
  625. getmem(newp,totallen*elesize+sizeof(tdynarray));
  626. { copy the elements of each source array }
  627. offset:=0;
  628. if assigned(src1) then
  629. begin
  630. srealp:=pdynarray(src1-sizeof(tdynarray));
  631. if srealp^.high>=0 then
  632. begin
  633. move(src1^,(pointer(newp)+sizeof(tdynarray)+offset*elesize)^,(srealp^.high+1)*elesize);
  634. inc(offset,srealp^.high+1);
  635. end;
  636. end;
  637. if assigned(src2) then
  638. begin
  639. srealp:=pdynarray(src2-sizeof(tdynarray));
  640. if srealp^.high>=0 then
  641. move(src2^,(pointer(newp)+sizeof(tdynarray)+offset*elesize)^,(srealp^.high+1)*elesize);
  642. end;
  643. { increase reference counts of all the elements }
  644. if assigned(eletypemngd) then
  645. int_AddRefArray(pointer(newp)+sizeof(tdynarray),eletypemngd,totallen);
  646. { clear at the end, dest could be a reference to an array being also source }
  647. fpc_dynarray_clear(dest,pti);
  648. dest:=pointer(newp)+sizeof(tdynarray);
  649. newp^.refcount:=1;
  650. newp^.high:=totallen-1;
  651. end;
  652. {$endif VER3_0}
  653. procedure DynArraySetLength(var a: Pointer; typeInfo: Pointer; dimCnt: SizeInt; lengthVec: PSizeInt);
  654. external name 'FPC_DYNARR_SETLENGTH';
  655. function DynArraySize(a : pointer): tdynarrayindex;
  656. external name 'FPC_DYNARRAY_LENGTH';
  657. procedure DynArrayClear(var a: Pointer; typeInfo: Pointer);
  658. external name 'FPC_DYNARRAY_CLEAR';
  659. procedure DynArrayAssign(var dest: Pointer; src: Pointer; typeInfo: pointer);
  660. external name 'FPC_DYNARRAY_ASSIGN';
  661. function DynArrayDim(typeInfo: Pointer): Integer;
  662. begin
  663. result:=0;
  664. while (typeInfo <> nil) and (pdynarraytypeinfo(typeInfo)^.kind = tkDynArray) do
  665. begin
  666. { skip kind and name }
  667. {$ifdef VER3_0}
  668. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  669. {$else VER3_0}
  670. typeInfo:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  671. {$endif VER3_0}
  672. { element type info}
  673. {$ifdef VER3_0}
  674. typeInfo:=pdynarraytypedata(typeInfo)^.elType2;
  675. {$else VER3_0}
  676. typeInfo:=pdynarraytypedata(typeInfo)^.elType2^;
  677. {$endif VER3_0}
  678. Inc(result);
  679. end;
  680. end;
  681. function DynArrayBounds(a: Pointer; typeInfo: Pointer): TBoundArray;
  682. var
  683. i,dim: sizeint;
  684. begin
  685. dim:=DynArrayDim(typeInfo);
  686. SetLength(result, dim);
  687. for i:=0 to pred(dim) do
  688. if a = nil then
  689. exit
  690. else
  691. begin
  692. result[i]:=DynArraySize(a)-1;
  693. a:=PPointerArray(a)^[0];
  694. end;
  695. end;
  696. function IsDynArrayRectangular(a: Pointer; typeInfo: Pointer): Boolean;
  697. var
  698. i,j: sizeint;
  699. dim,count: sizeint;
  700. begin
  701. dim:=DynArrayDim(typeInfo);
  702. for i:=1 to pred(dim) do
  703. begin
  704. count:=DynArraySize(PPointerArray(a)^[0]);
  705. for j:=1 to Pred(DynArraySize(a)) do
  706. if count<>DynArraySize(PPointerArray(a)^[j]) then
  707. exit(false);
  708. a:=PPointerArray(a)^[0];
  709. end;
  710. result:=true;
  711. end;
  712. function DynArrayIndex(a: Pointer; const indices: array of SizeInt; typeInfo: Pointer): Pointer;
  713. var
  714. i,h: sizeint;
  715. begin
  716. h:=High(indices);
  717. for i:=0 to h do
  718. begin
  719. { skip kind and name }
  720. {$ifdef VER3_0}
  721. typeInfo:=aligntoptr(Pointer(typeInfo)+2+PByte(typeInfo)[1]);
  722. {$else VER3_0}
  723. typeInfo:=aligntoqword(Pointer(typeInfo)+2+PByte(typeInfo)[1]);
  724. {$endif VER3_0}
  725. if i=h then
  726. break;
  727. a := PPointerArray(a)^[indices[i]];
  728. { element type info}
  729. {$ifdef VER3_0}
  730. typeInfo:=pdynarraytypedata(typeInfo)^.elType2;
  731. {$else VER3_0}
  732. typeInfo:=pdynarraytypedata(typeInfo)^.elType2^;
  733. {$endif VER3_0}
  734. end;
  735. result:=a+SizeUint(indices[h])*pdynarraytypedata(typeInfo)^.elSize;
  736. end;
  737. { obsolete but needed for bootstrapping }
  738. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_DECR_REF']; compilerproc;
  739. begin
  740. fpc_dynarray_clear(p,ti);
  741. end;