dynarr.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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. updatep: boolean;
  143. elesize : sizeint;
  144. eletype,eletypemngd : pointer;
  145. movsize : sizeint;
  146. begin
  147. { negative length is not allowed }
  148. if dims[0]<0 then
  149. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  150. { skip kind and name }
  151. {$ifdef VER3_0}
  152. ti:=aligntoptr(Pointer(pti)+2+PByte(pti)[1]);
  153. {$else VER3_0}
  154. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  155. {$endif VER3_0}
  156. elesize:=pdynarraytypedata(ti)^.elSize;
  157. {$ifdef VER3_0}
  158. eletype:=pdynarraytypedata(ti)^.elType2;
  159. {$else}
  160. eletype:=pdynarraytypedata(ti)^.elType2^;
  161. {$endif}
  162. { only set if type needs finalization }
  163. {$ifdef VER3_0}
  164. eletypemngd:=pdynarraytypedata(ti)^.elType;
  165. {$else}
  166. if assigned(pdynarraytypedata(ti)^.elType) then
  167. eletypemngd:=pdynarraytypedata(ti)^.elType^
  168. else
  169. eletypemngd:=nil;
  170. {$endif}
  171. { determine new memory size }
  172. size:=elesize*dims[0]+sizeof(tdynarray);
  173. updatep := false;
  174. { not assigned yet? }
  175. if not(assigned(p)) then
  176. begin
  177. { do we have to allocate memory? }
  178. if dims[0] = 0 then
  179. exit;
  180. newp:=AllocMem(size);
  181. {$ifndef VER3_0}
  182. { call int_InitializeArray for management operators }
  183. if assigned(eletypemngd) and (PTypeKind(eletype)^ in [tkRecord, tkObject]) then
  184. int_InitializeArray(pointer(newp)+sizeof(tdynarray), eletype, dims[0]);
  185. {$endif VER3_0}
  186. updatep := true;
  187. end
  188. else
  189. begin
  190. { if the new dimension is 0, we've to release all data }
  191. if dims[0]=0 then
  192. begin
  193. fpc_dynarray_clear(p,pti);
  194. exit;
  195. end;
  196. realp:=pdynarray(p-sizeof(tdynarray));
  197. newp := realp;
  198. if realp^.refcount<>1 then
  199. begin
  200. updatep := true;
  201. { make an unique copy }
  202. getmem(newp,size);
  203. fillchar(newp^,sizeof(tdynarray),0);
  204. if realp^.high < dims[0] then
  205. movelen := realp^.high+1
  206. else
  207. movelen := dims[0];
  208. movsize := elesize*movelen;
  209. move(p^,(pointer(newp)+sizeof(tdynarray))^, movsize);
  210. if size-sizeof(tdynarray)>movsize then
  211. fillchar((pointer(newp)+sizeof(tdynarray)+movsize)^,size-sizeof(tdynarray)-movsize,0);
  212. { increment ref. count of managed members }
  213. if assigned(eletypemngd) then
  214. int_AddRefArray(pointer(newp)+sizeof(tdynarray),eletypemngd,movelen);
  215. { a declock(ref. count) isn't enough here }
  216. { it could be that the in MT environments }
  217. { in the mean time the refcount was }
  218. { decremented }
  219. { it is, because it doesn't really matter }
  220. { if the array is now removed }
  221. fpc_dynarray_clear(p,pti);
  222. end
  223. else if dims[0]<>realp^.high+1 then
  224. begin
  225. { range checking is quite difficult ... }
  226. { if size overflows then it is less than }
  227. { the values it was calculated from }
  228. if (size<sizeof(tdynarray)) or
  229. ((elesize>0) and (size<elesize)) then
  230. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  231. { resize? }
  232. { here, realp^.refcount has to be one, otherwise the previous }
  233. { if-statement would have been taken. Or is this also for MT }
  234. { code? (JM) }
  235. if realp^.refcount=1 then
  236. begin
  237. { shrink the array? }
  238. if dims[0]<realp^.high+1 then
  239. begin
  240. if assigned(eletypemngd) then
  241. int_finalizearray(pointer(realp)+sizeof(tdynarray)+
  242. elesize*dims[0],
  243. eletypemngd,realp^.high-dims[0]+1);
  244. reallocmem(realp,size);
  245. end
  246. else if dims[0]>realp^.high+1 then
  247. begin
  248. reallocmem(realp,size);
  249. fillchar((pointer(realp)+sizeof(tdynarray)+elesize*(realp^.high+1))^,
  250. (dims[0]-realp^.high-1)*elesize,0);
  251. {$ifndef VER3_0}
  252. { call int_InitializeArray for management operators }
  253. if assigned(eletypemngd) and (PTypeKind(eletype)^ in [tkRecord, tkObject]) then
  254. int_InitializeArray(pointer(realp)+sizeof(tdynarray)+elesize*(realp^.high+1),
  255. eletype, dims[0]-realp^.high-1);
  256. {$endif VER3_0}
  257. end;
  258. newp := realp;
  259. updatep := true;
  260. end;
  261. end;
  262. end;
  263. { handle nested arrays }
  264. if dimcount>1 then
  265. begin
  266. for i:=0 to dims[0]-1 do
  267. int_dynarray_setlength(pointer((pointer(newp)+sizeof(tdynarray)+i*elesize)^),
  268. eletype,dimcount-1,@dims[1]);
  269. end;
  270. if updatep then
  271. begin
  272. p:=pointer(newp)+sizeof(tdynarray);
  273. newp^.refcount:=1;
  274. newp^.high:=dims[0]-1;
  275. end;
  276. end;
  277. { provide local access to array_to_dynarray_copy }
  278. function int_array_to_dynarray_copy(psrc : pointer;ti : pointer;
  279. lowidx,count,maxcount:tdynarrayindex;
  280. elesize : sizeint;
  281. eletype : pointer
  282. ) : fpc_stub_dynarray;[external name 'FPC_ARR_TO_DYNARR_COPY'];
  283. {$if defined(VER3_0) or defined(VER3_2)}
  284. function fpc_dynarray_copy(psrc : pointer;ti : pointer;
  285. lowidx,count:tdynarrayindex) : fpc_stub_dynarray;[Public,Alias:'FPC_DYNARR_COPY'];compilerproc;
  286. var
  287. realpsrc : pdynarray;
  288. eletype,tti : pointer;
  289. elesize : sizeint;
  290. begin
  291. fpc_dynarray_clear(pointer(result),ti);
  292. if psrc=nil then
  293. exit;
  294. realpsrc:=pdynarray(psrc-sizeof(tdynarray));
  295. {$ifdef VER3_0}
  296. tti:=aligntoptr(ti+2+PByte(ti)[1]);
  297. {$else VER3_0}
  298. tti:=aligntoqword(ti+2+PByte(ti)[1]);
  299. {$endif VER3_0}
  300. elesize:=pdynarraytypedata(tti)^.elSize;
  301. {$ifdef VER3_0}
  302. eletype:=pdynarraytypedata(tti)^.elType;
  303. {$else VER3_0}
  304. { only set if type needs finalization }
  305. if assigned(pdynarraytypedata(tti)^.elType) then
  306. eletype:=pdynarraytypedata(tti)^.elType^
  307. else
  308. eletype:=nil;
  309. {$endif VER3_0}
  310. fpc_array_to_dynarray_copy(psrc,ti,lowidx,count,realpsrc^.high+1,elesize,eletype);
  311. end;
  312. {$endif VER3_0 or VER3_2}
  313. { copy a custom array (open/dynamic/static) to dynamic array }
  314. function fpc_array_to_dynarray_copy(psrc : pointer;ti : pointer;
  315. lowidx,count,maxcount:tdynarrayindex;
  316. elesize : sizeint;
  317. eletype : pointer
  318. ) : fpc_stub_dynarray;[Public,Alias:'FPC_ARR_TO_DYNARR_COPY'];compilerproc;
  319. var
  320. size : sizeint;
  321. begin
  322. fpc_dynarray_clear(pointer(result),ti);
  323. if psrc=nil then
  324. exit;
  325. {$ifndef FPC_DYNARRAYCOPY_FIXED}
  326. if (lowidx=-1) and (count=-1) then
  327. begin
  328. lowidx:=0;
  329. count:=high(tdynarrayindex);
  330. end;
  331. {$endif FPC_DYNARRAYCOPY_FIXED}
  332. if (lowidx<0) then
  333. begin
  334. { Decrease count if index is negative, this is different from how copy()
  335. works on strings. Checked against D7. }
  336. if count<=0 then
  337. exit; { may overflow when adding lowidx }
  338. count:=count+lowidx;
  339. lowidx:=0;
  340. end;
  341. if (count>maxcount-lowidx) then
  342. count:=maxcount-lowidx;
  343. if count<=0 then
  344. exit;
  345. { create new array }
  346. size:=elesize*count;
  347. getmem(pointer(result),size+sizeof(tdynarray));
  348. pdynarray(result)^.refcount:=1;
  349. pdynarray(result)^.high:=count-1;
  350. inc(pointer(result),sizeof(tdynarray));
  351. { copy data }
  352. move(pointer(psrc+elesize*lowidx)^,pointer(result)^,size);
  353. { increment ref. count of members? }
  354. if assigned(eletype) then
  355. int_AddRefArray(pointer(result),eletype,count);
  356. end;
  357. {$ifndef VER3_0}
  358. procedure fpc_dynarray_delete(var p : pointer;source,count : SizeInt;pti : pointer);
  359. var
  360. newhigh : tdynarrayindex;
  361. size : sizeint;
  362. { contains the "fixed" pointers where the refcount }
  363. { and high are at positive offsets }
  364. realp,newp : pdynarray;
  365. ti : pointer;
  366. elesize : sizeint;
  367. eletype,eletypemngd : pointer;
  368. begin
  369. { if source > high then nothing to do }
  370. if not assigned(p) or
  371. (source>pdynarray(p-sizeof(tdynarray))^.high) or
  372. (count<=0) or
  373. (source<0) then
  374. exit;
  375. { cap count }
  376. if source+count-1>pdynarray(p-sizeof(tdynarray))^.high then
  377. count:=pdynarray(p-sizeof(tdynarray))^.high-source+1;
  378. { fast path: delete whole array }
  379. if (source=0) and (count=pdynarray(p-sizeof(tdynarray))^.high+1) then
  380. begin
  381. fpc_dynarray_clear(p,pti);
  382. exit;
  383. end;
  384. { skip kind and name }
  385. {$ifdef VER3_0}
  386. ti:=aligntoptr(Pointer(pti)+2+PByte(pti)[1]);
  387. {$else VER3_0}
  388. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  389. {$endif VER3_0}
  390. elesize:=pdynarraytypedata(ti)^.elSize;
  391. eletype:=pdynarraytypedata(ti)^.elType2^;
  392. { only set if type needs finalization }
  393. if assigned(pdynarraytypedata(ti)^.elType) then
  394. eletypemngd:=pdynarraytypedata(ti)^.elType^
  395. else
  396. eletypemngd:=nil;
  397. realp:=pdynarray(p-sizeof(tdynarray));
  398. newp:=realp;
  399. { determine new memory size }
  400. newhigh:=realp^.high-count;
  401. size:=elesize*(newhigh+1)+sizeof(tdynarray);
  402. if realp^.refcount<>1 then
  403. begin
  404. { make an unique copy }
  405. getmem(newp,size);
  406. fillchar(newp^,sizeof(tdynarray),0);
  407. { copy the elements that we still need }
  408. if source>0 then
  409. move(p^,(pointer(newp)+sizeof(tdynarray))^,source*elesize);
  410. if source+count-1<realp^.high then
  411. move((p+(source+count)*elesize)^,(pointer(newp)+sizeof(tdynarray)+source*elesize)^,(realp^.high-(source+count)+1)*elesize);
  412. { increment ref. count of managed members }
  413. if assigned(eletypemngd) then
  414. int_AddRefArray(pointer(newp)+sizeof(tdynarray),eletypemngd,newhigh+1);
  415. { a declock(ref. count) isn't enough here }
  416. { it could be that the in MT environments }
  417. { in the mean time the refcount was }
  418. { decremented }
  419. { it is, because it doesn't really matter }
  420. { if the array is now removed }
  421. fpc_dynarray_clear(p,pti);
  422. end
  423. else
  424. begin
  425. { finalize the elements that will be removed }
  426. if assigned(eletypemngd) then
  427. int_FinalizeArray(p+source*elesize,eletype,count);
  428. { close the gap by moving the trailing elements to the front }
  429. move((p+(source+count)*elesize)^,(p+source*elesize)^,(realp^.high-(source+count)+1)*elesize);
  430. { resize the array }
  431. reallocmem(realp,size);
  432. newp:=realp;
  433. end;
  434. p:=pointer(newp)+sizeof(tdynarray);
  435. newp^.refcount:=1;
  436. newp^.high:=newhigh;
  437. end;
  438. procedure fpc_dynarray_insert(var p : pointer;source : SizeInt;data : pointer;count : SizeInt;pti : pointer);compilerproc;
  439. var
  440. newhigh : tdynarrayindex;
  441. size : sizeint;
  442. realp,
  443. newp : pdynarray;
  444. ti : pointer;
  445. elesize : sizeint;
  446. eletype,eletypemngd : pointer;
  447. begin
  448. if not assigned(data) or
  449. (count=0) then
  450. exit;
  451. if assigned(p) then
  452. realp:=pdynarray(p-sizeof(tdynarray))
  453. else
  454. realp:=nil;
  455. newp:=realp;
  456. { cap insert index }
  457. if assigned(p) then
  458. begin
  459. if source<0 then
  460. source:=0
  461. else if source>realp^.high+1 then
  462. source:=realp^.high+1;
  463. end
  464. else
  465. source:=0;
  466. { skip kind and name }
  467. {$ifdef VER3_0}
  468. ti:=aligntoptr(Pointer(pti)+2+PByte(pti)[1]);
  469. {$else VER3_0}
  470. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  471. {$endif VER3_0}
  472. elesize:=pdynarraytypedata(ti)^.elSize;
  473. eletype:=pdynarraytypedata(ti)^.elType2^;
  474. { only set if type needs initialization }
  475. if assigned(pdynarraytypedata(ti)^.elType) then
  476. eletypemngd:=pdynarraytypedata(ti)^.elType^
  477. else
  478. eletypemngd:=nil;
  479. { determine new memory size }
  480. if assigned(p) then
  481. newhigh:=realp^.high+count
  482. else
  483. newhigh:=count-1;
  484. size:=elesize*(newhigh+1)+sizeof(tdynarray);
  485. if assigned(p) then
  486. begin
  487. if realp^.refcount<>1 then
  488. begin
  489. { make an unique copy }
  490. getmem(newp,size);
  491. fillchar(newp^,sizeof(tdynarray),0);
  492. { copy leading elements }
  493. if source>0 then
  494. move(p^,(pointer(newp)+sizeof(tdynarray))^,source*elesize);
  495. { insert new elements }
  496. move(data^,(pointer(newp)+sizeof(tdynarray)+source*elesize)^,count*elesize);
  497. { copy trailing elements }
  498. if realp^.high-source+1>0 then
  499. move((p+source*elesize)^,(pointer(newp)+sizeof(tdynarray)+(source+count)*elesize)^,(realp^.high-source+1)*elesize);
  500. { increment ref. count of managed members }
  501. if assigned(eletypemngd) then
  502. int_AddRefArray(pointer(newp)+sizeof(tdynarray),eletypemngd,newhigh+1);
  503. { a declock(ref. count) isn't enough here }
  504. { it could be that the in MT environments }
  505. { in the mean time the refcount was }
  506. { decremented }
  507. { it is, because it doesn't really matter }
  508. { if the array is now removed }
  509. fpc_dynarray_clear(p,pti);
  510. end
  511. else
  512. begin
  513. { resize the array }
  514. reallocmem(realp,size);
  515. { p might no longer be correct }
  516. p:=pointer(realp)+sizeof(tdynarray);
  517. { move the trailing part after the inserted data }
  518. if source<=realp^.high then
  519. move((p+source*elesize)^,(p+(source+count)*elesize)^,(realp^.high-source+1)*elesize);
  520. { move the inserted data to the destination }
  521. move(data^,(p+source*elesize)^,count*elesize);
  522. { increase reference counts of inserted elements }
  523. if assigned(eletypemngd) then
  524. int_AddRefArray(p+source*elesize,eletypemngd,count);
  525. newp:=realp;
  526. end;
  527. end
  528. else
  529. begin
  530. { allocate new array }
  531. getmem(newp,size);
  532. fillchar(newp^,sizeof(tdynarray),0);
  533. { insert data }
  534. move(data^,(pointer(newp)+sizeof(tdynarray))^,count*elesize);
  535. { increase reference counts of inserted elements }
  536. if assigned(eletypemngd) then
  537. int_AddRefArray(pointer(newp)+sizeof(tdynarray),eletypemngd,count);
  538. end;
  539. p:=pointer(newp)+sizeof(tdynarray);
  540. newp^.refcount:=1;
  541. newp^.high:=newhigh;
  542. end;
  543. procedure fpc_dynarray_concat_multi(var dest : pointer; pti: pointer; const sarr:array of pointer); compilerproc;
  544. var
  545. i,
  546. offset,
  547. totallen : sizeint;
  548. newp,
  549. realp,
  550. srealp : pdynarray;
  551. ti : pointer;
  552. elesize : sizeint;
  553. eletypemngd : pointer;
  554. begin
  555. { sanity check }
  556. if length(sarr)=0 then
  557. exit;
  558. totallen:=0;
  559. for i:=0 to high(sarr) do
  560. if assigned(sarr[i]) then
  561. inc(totallen,pdynarray(sarr[i]-sizeof(tdynarray))^.high+1);
  562. if totallen=0 then
  563. begin
  564. fpc_dynarray_clear(dest,pti);
  565. exit;
  566. end;
  567. { skip kind and name }
  568. {$ifdef VER3_0}
  569. ti:=aligntoptr(Pointer(pti)+2+PByte(pti)[1]);
  570. {$else VER3_0}
  571. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  572. {$endif VER3_0}
  573. elesize:=pdynarraytypedata(ti)^.elSize;
  574. { only set if type needs initialization }
  575. if assigned(pdynarraytypedata(ti)^.elType) then
  576. eletypemngd:=pdynarraytypedata(ti)^.elType^
  577. else
  578. eletypemngd:=nil;
  579. { copy the elements of each source array }
  580. offset:=0;
  581. { the idea to reuse the first array, re-allocate it and append the other entries is not possible as the first entry
  582. might be finalized later on by the caller however in case of a re-allocate, the entry itself might be gone }
  583. { allocate new array }
  584. getmem(newp,totallen*elesize+sizeof(tdynarray));
  585. for i:=0 to high(sarr) do
  586. if assigned(sarr[i]) then
  587. begin
  588. srealp:=pdynarray(sarr[i]-sizeof(tdynarray));
  589. if srealp^.high>=0 then
  590. begin
  591. move(sarr[i]^,(pointer(newp)+sizeof(tdynarray)+offset*elesize)^,(srealp^.high+1)*elesize);
  592. inc(offset,srealp^.high+1);
  593. end;
  594. end;
  595. { increase reference counts of all the elements }
  596. if assigned(eletypemngd) then
  597. int_AddRefArray(pointer(newp)+sizeof(tdynarray),eletypemngd,totallen);
  598. { clear at the end, dest could be a reference to an array being used also as source }
  599. fpc_dynarray_clear(dest,pti);
  600. dest:=pointer(newp)+sizeof(tdynarray);
  601. newp^.refcount:=1;
  602. newp^.high:=totallen-1;
  603. end;
  604. procedure fpc_dynarray_concat(var dest : pointer; pti: pointer; const src1,src2 : pointer); compilerproc;
  605. var
  606. offset,
  607. totallen : sizeint;
  608. newp,
  609. realp,
  610. srealp : pdynarray;
  611. ti : pointer;
  612. elesize : sizeint;
  613. eletypemngd : pointer;
  614. begin
  615. totallen:=0;
  616. if assigned(src1) then
  617. inc(totallen,pdynarray(src1-sizeof(tdynarray))^.high+1);
  618. if assigned(src2) then
  619. inc(totallen,pdynarray(src2-sizeof(tdynarray))^.high+1);
  620. if totallen=0 then
  621. begin
  622. fpc_dynarray_clear(dest,pti);
  623. exit;
  624. end;
  625. { skip kind and name }
  626. {$ifdef VER3_0}
  627. ti:=aligntoptr(Pointer(pti)+2+PByte(pti)[1]);
  628. {$else VER3_0}
  629. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  630. {$endif VER3_0}
  631. elesize:=pdynarraytypedata(ti)^.elSize;
  632. { only set if type needs initialization }
  633. if assigned(pdynarraytypedata(ti)^.elType) then
  634. eletypemngd:=pdynarraytypedata(ti)^.elType^
  635. else
  636. eletypemngd:=nil;
  637. { the idea to reuse the first array, re-allocate it and append the other entries is not possible as the first entry
  638. might be finalized later on by the caller however in case of a re-allocate, the entry itself might be gone }
  639. { allocate new array }
  640. getmem(newp,totallen*elesize+sizeof(tdynarray));
  641. { copy the elements of each source array }
  642. offset:=0;
  643. if assigned(src1) then
  644. begin
  645. srealp:=pdynarray(src1-sizeof(tdynarray));
  646. if srealp^.high>=0 then
  647. begin
  648. move(src1^,(pointer(newp)+sizeof(tdynarray)+offset*elesize)^,(srealp^.high+1)*elesize);
  649. inc(offset,srealp^.high+1);
  650. end;
  651. end;
  652. if assigned(src2) then
  653. begin
  654. srealp:=pdynarray(src2-sizeof(tdynarray));
  655. if srealp^.high>=0 then
  656. move(src2^,(pointer(newp)+sizeof(tdynarray)+offset*elesize)^,(srealp^.high+1)*elesize);
  657. end;
  658. { increase reference counts of all the elements }
  659. if assigned(eletypemngd) then
  660. int_AddRefArray(pointer(newp)+sizeof(tdynarray),eletypemngd,totallen);
  661. { clear at the end, dest could be a reference to an array being also source }
  662. fpc_dynarray_clear(dest,pti);
  663. dest:=pointer(newp)+sizeof(tdynarray);
  664. newp^.refcount:=1;
  665. newp^.high:=totallen-1;
  666. end;
  667. {$endif VER3_0}
  668. procedure DynArraySetLength(var a: Pointer; typeInfo: Pointer; dimCnt: SizeInt; lengthVec: PSizeInt);
  669. external name 'FPC_DYNARR_SETLENGTH';
  670. function DynArraySize(a : pointer): tdynarrayindex;
  671. external name 'FPC_DYNARRAY_LENGTH';
  672. procedure DynArrayClear(var a: Pointer; typeInfo: Pointer);
  673. external name 'FPC_DYNARRAY_CLEAR';
  674. procedure DynArrayAssign(var dest: Pointer; src: Pointer; typeInfo: pointer);
  675. external name 'FPC_DYNARRAY_ASSIGN';
  676. function DynArrayDim(typeInfo: Pointer): Integer;
  677. begin
  678. result:=0;
  679. while (typeInfo <> nil) and (pdynarraytypeinfo(typeInfo)^.kind = tkDynArray) do
  680. begin
  681. { skip kind and name }
  682. {$ifdef VER3_0}
  683. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  684. {$else VER3_0}
  685. typeInfo:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  686. {$endif VER3_0}
  687. { element type info}
  688. {$ifdef VER3_0}
  689. typeInfo:=pdynarraytypedata(typeInfo)^.elType2;
  690. {$else VER3_0}
  691. typeInfo:=pdynarraytypedata(typeInfo)^.elType2^;
  692. {$endif VER3_0}
  693. Inc(result);
  694. end;
  695. end;
  696. function DynArrayBounds(a: Pointer; typeInfo: Pointer): TBoundArray;
  697. var
  698. i,dim: sizeint;
  699. begin
  700. dim:=DynArrayDim(typeInfo);
  701. SetLength(result, dim);
  702. for i:=0 to pred(dim) do
  703. if a = nil then
  704. exit
  705. else
  706. begin
  707. result[i]:=DynArraySize(a)-1;
  708. a:=PPointerArray(a)^[0];
  709. end;
  710. end;
  711. function IsDynArrayRectangular(a: Pointer; typeInfo: Pointer): Boolean;
  712. var
  713. i,j: sizeint;
  714. dim,count: sizeint;
  715. begin
  716. dim:=DynArrayDim(typeInfo);
  717. for i:=1 to pred(dim) do
  718. begin
  719. count:=DynArraySize(PPointerArray(a)^[0]);
  720. for j:=1 to Pred(DynArraySize(a)) do
  721. if count<>DynArraySize(PPointerArray(a)^[j]) then
  722. exit(false);
  723. a:=PPointerArray(a)^[0];
  724. end;
  725. result:=true;
  726. end;
  727. function DynArrayIndex(a: Pointer; const indices: array of SizeInt; typeInfo: Pointer): Pointer;
  728. var
  729. i,h: sizeint;
  730. elsize: sizeuint;
  731. begin
  732. h:=High(indices);
  733. for i:=0 to h do
  734. begin
  735. if i<h then
  736. a := PPointerArray(a)^[indices[i]];
  737. { skip kind and name }
  738. {$ifdef VER3_0}
  739. typeInfo:=aligntoptr(Pointer(typeInfo)+2+PByte(typeInfo)[1]);
  740. {$else VER3_0}
  741. typeInfo:=aligntoqword(Pointer(typeInfo)+2+PByte(typeInfo)[1]);
  742. {$endif VER3_0}
  743. { store the last element size for the index calculation }
  744. elsize:=pdynarraytypedata(typeInfo)^.elSize;
  745. { element type info}
  746. {$ifdef VER3_0}
  747. typeInfo:=pdynarraytypedata(typeInfo)^.elType2;
  748. {$else VER3_0}
  749. typeInfo:=pdynarraytypedata(typeInfo)^.elType2^;
  750. {$endif VER3_0}
  751. if typeInfo=nil then
  752. exit(nil);
  753. end;
  754. { skip kind and name }
  755. {$ifdef VER3_0}
  756. typeInfo:=aligntoptr(Pointer(typeInfo)+2+PByte(typeInfo)[1]);
  757. {$else VER3_0}
  758. typeInfo:=aligntoqword(Pointer(typeInfo)+2+PByte(typeInfo)[1]);
  759. {$endif VER3_0}
  760. result:=@(PByte(a)[indices[h]*elsize]);
  761. end;
  762. { obsolete but needed for bootstrapping }
  763. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_DECR_REF']; compilerproc;
  764. begin
  765. fpc_dynarray_clear(p,ti);
  766. end;