dynarr.inc 27 KB

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