dynarr.inc 26 KB

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