dynarr.inc 26 KB

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