dynarr.inc 26 KB

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