dynarr.inc 28 KB

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