dynarr.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. updatep := true;
  165. end
  166. else
  167. begin
  168. { if the new dimension is 0, we've to release all data }
  169. if dims[0]=0 then
  170. begin
  171. fpc_dynarray_clear(p,pti);
  172. exit;
  173. end;
  174. realp:=pdynarray(p-sizeof(tdynarray));
  175. newp := realp;
  176. if realp^.refcount<>1 then
  177. begin
  178. updatep := true;
  179. { make an unique copy }
  180. getmem(newp,size);
  181. fillchar(newp^,sizeof(tdynarray),0);
  182. if realp^.high < dims[0] then
  183. movelen := realp^.high+1
  184. else
  185. movelen := dims[0];
  186. movsize := elesize*movelen;
  187. move(p^,(pointer(newp)+sizeof(tdynarray))^, movsize);
  188. if size-sizeof(tdynarray)>movsize then
  189. fillchar((pointer(newp)+sizeof(tdynarray)+movsize)^,size-sizeof(tdynarray)-movsize,0);
  190. { increment ref. count of managed members }
  191. if assigned(eletypemngd) then
  192. for i:= 0 to movelen-1 do
  193. int_addref(pointer(newp)+sizeof(tdynarray)+elesize*i,eletypemngd);
  194. { a declock(ref. count) isn't enough here }
  195. { it could be that the in MT environments }
  196. { in the mean time the refcount was }
  197. { decremented }
  198. { it is, because it doesn't really matter }
  199. { if the array is now removed }
  200. fpc_dynarray_clear(p,pti);
  201. end
  202. else if dims[0]<>realp^.high+1 then
  203. begin
  204. { range checking is quite difficult ... }
  205. { if size overflows then it is less than }
  206. { the values it was calculated from }
  207. if (size<sizeof(tdynarray)) or
  208. ((elesize>0) and (size<elesize)) then
  209. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  210. { resize? }
  211. { here, realp^.refcount has to be one, otherwise the previous }
  212. { if-statement would have been taken. Or is this also for MT }
  213. { code? (JM) }
  214. if realp^.refcount=1 then
  215. begin
  216. { shrink the array? }
  217. if dims[0]<realp^.high+1 then
  218. begin
  219. if assigned(eletypemngd) then
  220. int_finalizearray(pointer(realp)+sizeof(tdynarray)+
  221. elesize*dims[0],
  222. eletypemngd,realp^.high-dims[0]+1);
  223. reallocmem(realp,size);
  224. end
  225. else if dims[0]>realp^.high+1 then
  226. begin
  227. reallocmem(realp,size);
  228. fillchar((pointer(realp)+sizeof(tdynarray)+elesize*(realp^.high+1))^,
  229. (dims[0]-realp^.high-1)*elesize,0);
  230. end;
  231. newp := realp;
  232. updatep := true;
  233. end;
  234. end;
  235. end;
  236. { handle nested arrays }
  237. if dimcount>1 then
  238. begin
  239. for i:=0 to dims[0]-1 do
  240. int_dynarray_setlength(pointer((pointer(newp)+sizeof(tdynarray)+i*elesize)^),
  241. eletype,dimcount-1,@dims[1]);
  242. end;
  243. if updatep then
  244. begin
  245. p:=pointer(newp)+sizeof(tdynarray);
  246. newp^.refcount:=1;
  247. newp^.high:=dims[0]-1;
  248. end;
  249. end;
  250. { provide local access to dynarr_copy }
  251. function int_dynarray_copy(psrc : pointer;ti : pointer;
  252. lowidx,count:tdynarrayindex) : fpc_stub_dynarray;[external name 'FPC_DYNARR_COPY'];
  253. function fpc_dynarray_copy(psrc : pointer;ti : pointer;
  254. lowidx,count:tdynarrayindex) : fpc_stub_dynarray;[Public,Alias:'FPC_DYNARR_COPY'];compilerproc;
  255. var
  256. realpsrc : pdynarray;
  257. i,size : sizeint;
  258. elesize : sizeint;
  259. eletype : pointer;
  260. begin
  261. fpc_dynarray_clear(pointer(result),ti);
  262. if psrc=nil then
  263. exit;
  264. {$ifndef FPC_DYNARRAYCOPY_FIXED}
  265. if (lowidx=-1) and (count=-1) then
  266. begin
  267. lowidx:=0;
  268. count:=high(tdynarrayindex);
  269. end;
  270. {$endif FPC_DYNARRAYCOPY_FIXED}
  271. realpsrc:=pdynarray(psrc-sizeof(tdynarray));
  272. if (lowidx<0) then
  273. begin
  274. { Decrease count if index is negative, this is different from how copy()
  275. works on strings. Checked against D7. }
  276. if count<=0 then
  277. exit; { may overflow when adding lowidx }
  278. count:=count+lowidx;
  279. lowidx:=0;
  280. end;
  281. if (count>realpsrc^.high-lowidx+1) then
  282. count:=realpsrc^.high-lowidx+1;
  283. if count<=0 then
  284. exit;
  285. { skip kind and name }
  286. {$ifdef VER3_0}
  287. ti:=aligntoptr(ti+2+PByte(ti)[1]);
  288. {$else VER3_0}
  289. ti:=aligntoqword(ti+2+PByte(ti)[1]);
  290. {$endif VER3_0}
  291. elesize:=pdynarraytypedata(ti)^.elSize;
  292. { only set if type needs finalization }
  293. {$ifdef VER3_0}
  294. eletype:=pdynarraytypedata(ti)^.elType;
  295. {$else}
  296. if assigned(pdynarraytypedata(ti)^.elType) then
  297. eletype:=pdynarraytypedata(ti)^.elType^
  298. else
  299. eletype:=nil;
  300. {$endif}
  301. { create new array }
  302. size:=elesize*count;
  303. getmem(pointer(result),size+sizeof(tdynarray));
  304. pdynarray(result)^.refcount:=1;
  305. pdynarray(result)^.high:=count-1;
  306. inc(pointer(result),sizeof(tdynarray));
  307. { copy data }
  308. move(pointer(psrc+elesize*lowidx)^,pointer(result)^,size);
  309. { increment ref. count of members? }
  310. if assigned(eletype) then
  311. for i:=0 to count-1 do
  312. int_addref(pointer(pointer(result)+elesize*i),eletype);
  313. end;
  314. {$ifndef VER3_0}
  315. procedure fpc_dynarray_delete(var p : pointer;source,count : SizeInt;pti : pointer);
  316. var
  317. newhigh,
  318. i : tdynarrayindex;
  319. size : sizeint;
  320. { contains the "fixed" pointers where the refcount }
  321. { and high are at positive offsets }
  322. realp,newp : pdynarray;
  323. ti : pointer;
  324. elesize : sizeint;
  325. eletype,eletypemngd : pointer;
  326. begin
  327. { if source > high then nothing to do }
  328. if not assigned(p) or
  329. (source>pdynarray(p-sizeof(tdynarray))^.high) or
  330. (count<=0) or
  331. (source<0) then
  332. exit;
  333. { cap count }
  334. if source+count-1>pdynarray(p-sizeof(tdynarray))^.high then
  335. count:=pdynarray(p-sizeof(tdynarray))^.high-source+1;
  336. { fast path: delete whole array }
  337. if (source=0) and (count=pdynarray(p-sizeof(tdynarray))^.high+1) then
  338. begin
  339. fpc_dynarray_clear(p,pti);
  340. exit;
  341. end;
  342. { skip kind and name }
  343. {$ifdef VER3_0}
  344. ti:=aligntoptr(Pointer(pti)+2+PByte(pti)[1]);
  345. {$else VER3_0}
  346. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  347. {$endif VER3_0}
  348. elesize:=pdynarraytypedata(ti)^.elSize;
  349. eletype:=pdynarraytypedata(ti)^.elType2^;
  350. { only set if type needs finalization }
  351. if assigned(pdynarraytypedata(ti)^.elType) then
  352. eletypemngd:=pdynarraytypedata(ti)^.elType^
  353. else
  354. eletypemngd:=nil;
  355. realp:=pdynarray(p-sizeof(tdynarray));
  356. newp:=realp;
  357. { determine new memory size }
  358. newhigh:=realp^.high-count;
  359. size:=elesize*(newhigh+1)+sizeof(tdynarray);
  360. if realp^.refcount<>1 then
  361. begin
  362. { make an unique copy }
  363. getmem(newp,size);
  364. fillchar(newp^,sizeof(tdynarray),0);
  365. { copy the elements that we still need }
  366. if source>0 then
  367. move(p^,(pointer(newp)+sizeof(tdynarray))^,source*elesize);
  368. if source+count-1<realp^.high then
  369. move((p+(source+count)*elesize)^,(pointer(newp)+sizeof(tdynarray)+source*elesize)^,(realp^.high-(source+count)+1)*elesize);
  370. { increment ref. count of managed members }
  371. if assigned(eletypemngd) then
  372. for i:=0 to newhigh do
  373. int_addref(pointer(newp)+sizeof(tdynarray)+elesize*i,eletypemngd);
  374. { a declock(ref. count) isn't enough here }
  375. { it could be that the in MT environments }
  376. { in the mean time the refcount was }
  377. { decremented }
  378. { it is, because it doesn't really matter }
  379. { if the array is now removed }
  380. fpc_dynarray_clear(p,pti);
  381. end
  382. else
  383. begin
  384. { finalize the elements that will be removed }
  385. if assigned(eletypemngd) then
  386. begin
  387. for i:=source to source+count-1 do
  388. int_finalize(p+i*elesize,eletype);
  389. end;
  390. { close the gap by moving the trailing elements to the front }
  391. move((p+(source+count)*elesize)^,(p+source*elesize)^,(realp^.high-(source+count)+1)*elesize);
  392. { resize the array }
  393. reallocmem(realp,size);
  394. newp:=realp;
  395. end;
  396. p:=pointer(newp)+sizeof(tdynarray);
  397. newp^.refcount:=1;
  398. newp^.high:=newhigh;
  399. end;
  400. {$endif VER3_0}
  401. procedure DynArraySetLength(var a: Pointer; typeInfo: Pointer; dimCnt: SizeInt; lengthVec: PSizeInt);
  402. external name 'FPC_DYNARR_SETLENGTH';
  403. function DynArraySize(a : pointer): tdynarrayindex;
  404. external name 'FPC_DYNARRAY_LENGTH';
  405. procedure DynArrayClear(var a: Pointer; typeInfo: Pointer);
  406. external name 'FPC_DYNARRAY_CLEAR';
  407. function DynArrayDim(typeInfo: Pointer): Integer;
  408. begin
  409. result:=0;
  410. while (typeInfo <> nil) and (pdynarraytypeinfo(typeInfo)^.kind = tkDynArray) do
  411. begin
  412. { skip kind and name }
  413. {$ifdef VER3_0}
  414. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  415. {$else VER3_0}
  416. typeInfo:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  417. {$endif VER3_0}
  418. { element type info}
  419. {$ifdef VER3_0}
  420. typeInfo:=pdynarraytypedata(typeInfo)^.elType2;
  421. {$else VER3_0}
  422. typeInfo:=pdynarraytypedata(typeInfo)^.elType2^;
  423. {$endif VER3_0}
  424. Inc(result);
  425. end;
  426. end;
  427. function DynArrayBounds(a: Pointer; typeInfo: Pointer): TBoundArray;
  428. var
  429. i,dim: sizeint;
  430. begin
  431. dim:=DynArrayDim(typeInfo);
  432. SetLength(result, dim);
  433. for i:=0 to pred(dim) do
  434. if a = nil then
  435. exit
  436. else
  437. begin
  438. result[i]:=DynArraySize(a)-1;
  439. a:=PPointerArray(a)^[0];
  440. end;
  441. end;
  442. function IsDynArrayRectangular(a: Pointer; typeInfo: Pointer): Boolean;
  443. var
  444. i,j: sizeint;
  445. dim,count: sizeint;
  446. begin
  447. dim:=DynArrayDim(typeInfo);
  448. for i:=1 to pred(dim) do
  449. begin
  450. count:=DynArraySize(PPointerArray(a)^[0]);
  451. for j:=1 to Pred(DynArraySize(a)) do
  452. if count<>DynArraySize(PPointerArray(a)^[j]) then
  453. exit(false);
  454. a:=PPointerArray(a)^[0];
  455. end;
  456. result:=true;
  457. end;
  458. function DynArrayIndex(a: Pointer; const indices: array of SizeInt; typeInfo: Pointer): Pointer;
  459. var
  460. i,h: sizeint;
  461. begin
  462. h:=High(indices);
  463. for i:=0 to h do
  464. begin
  465. if i<h then
  466. a := PPointerArray(a)^[indices[i]];
  467. { skip kind and name }
  468. typeInfo:=(typeInfo+2+PByte(typeInfo)[1]);
  469. { element type info}
  470. {$ifdef VER3_0}
  471. typeInfo:=pdynarraytypedata(typeInfo)^.elType2;
  472. {$else VER3_0}
  473. typeInfo:=pdynarraytypedata(typeInfo)^.elType2^;
  474. {$endif VER3_0}
  475. if typeInfo=nil then
  476. exit(nil);
  477. end;
  478. { skip kind and name }
  479. typeInfo:=(typeInfo+2+PByte(typeInfo)[1]);
  480. result:=@(PByte(a)[indices[h]*pdynarraytypedata(typeInfo)^.elSize]);
  481. end;
  482. { obsolete but needed for bootstrapping }
  483. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_DECR_REF']; compilerproc;
  484. begin
  485. fpc_dynarray_clear(p,ti);
  486. end;