dynarr.inc 28 KB

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