dynarr.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2000 by Florian Klaempfl
  4. member of the Free Pascal development team.
  5. This file implements the helper routines for dyn. Arrays in FPC
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************
  12. }
  13. type
  14. { don't add new fields, the size is used }
  15. { to calculate memory requirements }
  16. pdynarray = ^tdynarray;
  17. tdynarray = packed record
  18. refcount : ptrint;
  19. high : tdynarrayindex;
  20. end;
  21. pdynarraytypedata = ^tdynarraytypedata;
  22. tdynarraytypedata =
  23. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  24. packed
  25. {$else}
  26. {$ifdef powerpc64}
  27. { 3.0.0 does not align elType field on a 8-byte boundary,
  28. thus use packed also in this case }
  29. {$ifdef VER3_0_0}
  30. packed
  31. {$endif VER3_0_0}
  32. {$endif powerpc64}
  33. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  34. record
  35. elSize : SizeUInt;
  36. {$ifdef VER3_0}
  37. elType2 : Pointer;
  38. {$else}
  39. elType2 : PPointer;
  40. {$endif}
  41. varType : Longint;
  42. {$ifdef VER3_0}
  43. elType : Pointer;
  44. {$else}
  45. elType : PPointer;
  46. {$endif}
  47. end;
  48. procedure fpc_dynarray_rangecheck(p : pointer;i : tdynarrayindex);[Public,Alias:'FPC_DYNARRAY_RANGECHECK']; compilerproc;
  49. begin
  50. if not(assigned(p)) or (i<0) or (i>pdynarray(p-sizeof(tdynarray))^.high) then
  51. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  52. end;
  53. function fpc_dynarray_length(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_LENGTH']; compilerproc;
  54. begin
  55. if assigned(p) then
  56. fpc_dynarray_length:=pdynarray(p-sizeof(tdynarray))^.high+1
  57. else
  58. fpc_dynarray_length:=0;
  59. end;
  60. function fpc_dynarray_high(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_HIGH']; compilerproc;
  61. begin
  62. if assigned(p) then
  63. fpc_dynarray_high:=pdynarray(p-sizeof(tdynarray))^.high
  64. else
  65. fpc_dynarray_high:=-1;
  66. end;
  67. procedure fpc_dynarray_clear(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_CLEAR']; compilerproc;
  68. var
  69. realp : pdynarray;
  70. begin
  71. if (P=Nil) then
  72. exit;
  73. realp:=pdynarray(p-sizeof(tdynarray));
  74. if realp^.refcount=0 then
  75. HandleErrorAddrFrameInd(204,get_pc_addr,get_frame);
  76. if declocked(realp^.refcount) then
  77. begin
  78. {$ifdef VER3_0}
  79. ti:=aligntoptr(ti+2+PByte(ti)[1]);
  80. {$else VER3_0}
  81. ti:=aligntoqword(ti+2+PByte(ti)[1]);
  82. {$endif VER3_0}
  83. if assigned(pdynarraytypedata(ti)^.elType) then
  84. int_finalizearray(p,pdynarraytypedata(ti)^.elType{$ifndef VER3_0}^{$endif},realp^.high+1);
  85. freemem(realp);
  86. end;
  87. p:=nil;
  88. end;
  89. { alias for internal use }
  90. Procedure fpc_dynarray_clear (var p : pointer;ti : pointer);[external name 'FPC_DYNARRAY_CLEAR'];
  91. procedure fpc_dynarray_incr_ref(p : pointer);[Public,Alias:'FPC_DYNARRAY_INCR_REF']; compilerproc;
  92. var
  93. realp : pdynarray;
  94. begin
  95. if p=nil then
  96. exit;
  97. realp:=pdynarray(p-sizeof(tdynarray));
  98. if realp^.refcount=0 then
  99. HandleErrorAddrFrameInd(204,get_pc_addr,get_frame);
  100. inclocked(realp^.refcount);
  101. end;
  102. { provide local access to dynarr_decr_ref for dynarr_setlength }
  103. procedure fpc_dynarray_incr_ref(p : pointer); [external name 'FPC_DYNARRAY_INCR_REF'];
  104. procedure fpc_dynarray_assign(var dest: Pointer; src: Pointer; ti: pointer);[public,alias:'FPC_DYNARRAY_ASSIGN']; compilerproc;
  105. begin
  106. fpc_dynarray_incr_ref(src);
  107. fpc_dynarray_clear(dest,ti);
  108. Dest:=Src;
  109. end;
  110. procedure fpc_dynarray_assign(var dest: Pointer; src: Pointer; ti: pointer);[external name 'FPC_DYNARRAY_ASSIGN'];
  111. { provide local access to dynarr_setlength }
  112. procedure int_dynarray_setlength(var p : pointer;pti : pointer;
  113. dimcount : sizeint;dims : pdynarrayindex);[external name 'FPC_DYNARR_SETLENGTH'];
  114. procedure fpc_dynarray_setlength(var p : pointer;pti : pointer;
  115. dimcount : sizeint;dims : pdynarrayindex);[Public,Alias:'FPC_DYNARR_SETLENGTH']; compilerproc;
  116. var
  117. i : tdynarrayindex;
  118. movelen,
  119. size : sizeint;
  120. { contains the "fixed" pointers where the refcount }
  121. { and high are at positive offsets }
  122. realp,newp : pdynarray;
  123. ti : pointer;
  124. updatep: boolean;
  125. elesize : sizeint;
  126. eletype,eletypemngd : pointer;
  127. movsize : sizeint;
  128. begin
  129. { negative length is not allowed }
  130. if dims[0]<0 then
  131. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  132. { skip kind and name }
  133. {$ifdef VER3_0}
  134. ti:=aligntoptr(Pointer(pti)+2+PByte(pti)[1]);
  135. {$else VER3_0}
  136. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  137. {$endif VER3_0}
  138. elesize:=pdynarraytypedata(ti)^.elSize;
  139. {$ifdef VER3_0}
  140. eletype:=pdynarraytypedata(ti)^.elType2;
  141. {$else}
  142. eletype:=pdynarraytypedata(ti)^.elType2^;
  143. {$endif}
  144. { only set if type needs finalization }
  145. {$ifdef VER3_0}
  146. eletypemngd:=pdynarraytypedata(ti)^.elType;
  147. {$else}
  148. if assigned(pdynarraytypedata(ti)^.elType) then
  149. eletypemngd:=pdynarraytypedata(ti)^.elType^
  150. else
  151. eletypemngd:=nil;
  152. {$endif}
  153. { determine new memory size }
  154. size:=elesize*dims[0]+sizeof(tdynarray);
  155. updatep := false;
  156. { not assigned yet? }
  157. if not(assigned(p)) then
  158. begin
  159. { do we have to allocate memory? }
  160. if dims[0] = 0 then
  161. exit;
  162. getmem(newp,size);
  163. fillchar(newp^,size,0);
  164. {$ifdef FPC_HAS_MANAGEMENT_OPERATORS}
  165. { call int_InitializeArray for management operators }
  166. if assigned(eletypemngd) and (PByte(eletype)^ in [tkRecord, tkObject]) then
  167. int_InitializeArray(pointer(newp)+sizeof(tdynarray), eletype, dims[0]);
  168. {$endif FPC_HAS_MANAGEMENT_OPERATORS}
  169. updatep := true;
  170. end
  171. else
  172. begin
  173. { if the new dimension is 0, we've to release all data }
  174. if dims[0]=0 then
  175. begin
  176. fpc_dynarray_clear(p,pti);
  177. exit;
  178. end;
  179. realp:=pdynarray(p-sizeof(tdynarray));
  180. newp := realp;
  181. if realp^.refcount<>1 then
  182. begin
  183. updatep := true;
  184. { make an unique copy }
  185. getmem(newp,size);
  186. fillchar(newp^,sizeof(tdynarray),0);
  187. if realp^.high < dims[0] then
  188. movelen := realp^.high+1
  189. else
  190. movelen := dims[0];
  191. movsize := elesize*movelen;
  192. move(p^,(pointer(newp)+sizeof(tdynarray))^, movsize);
  193. if size-sizeof(tdynarray)>movsize then
  194. fillchar((pointer(newp)+sizeof(tdynarray)+movsize)^,size-sizeof(tdynarray)-movsize,0);
  195. { increment ref. count of managed members }
  196. if assigned(eletypemngd) then
  197. for i:= 0 to movelen-1 do
  198. int_addref(pointer(newp)+sizeof(tdynarray)+elesize*i,eletypemngd);
  199. { a declock(ref. count) isn't enough here }
  200. { it could be that the in MT environments }
  201. { in the mean time the refcount was }
  202. { decremented }
  203. { it is, because it doesn't really matter }
  204. { if the array is now removed }
  205. fpc_dynarray_clear(p,pti);
  206. end
  207. else if dims[0]<>realp^.high+1 then
  208. begin
  209. { range checking is quite difficult ... }
  210. { if size overflows then it is less than }
  211. { the values it was calculated from }
  212. if (size<sizeof(tdynarray)) or
  213. ((elesize>0) and (size<elesize)) then
  214. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  215. { resize? }
  216. { here, realp^.refcount has to be one, otherwise the previous }
  217. { if-statement would have been taken. Or is this also for MT }
  218. { code? (JM) }
  219. if realp^.refcount=1 then
  220. begin
  221. { shrink the array? }
  222. if dims[0]<realp^.high+1 then
  223. begin
  224. if assigned(eletypemngd) then
  225. int_finalizearray(pointer(realp)+sizeof(tdynarray)+
  226. elesize*dims[0],
  227. eletypemngd,realp^.high-dims[0]+1);
  228. reallocmem(realp,size);
  229. end
  230. else if dims[0]>realp^.high+1 then
  231. begin
  232. reallocmem(realp,size);
  233. fillchar((pointer(realp)+sizeof(tdynarray)+elesize*(realp^.high+1))^,
  234. (dims[0]-realp^.high-1)*elesize,0);
  235. {$ifdef FPC_HAS_MANAGEMENT_OPERATORS}
  236. { call int_InitializeArray for management operators }
  237. if assigned(eletypemngd) and (PByte(eletype)^ in [tkRecord, tkObject]) then
  238. int_InitializeArray(pointer(realp)+sizeof(tdynarray)+elesize*(realp^.high+1),
  239. eletype, dims[0]-realp^.high-1);
  240. {$endif FPC_HAS_MANAGEMENT_OPERATORS}
  241. end;
  242. newp := realp;
  243. updatep := true;
  244. end;
  245. end;
  246. end;
  247. { handle nested arrays }
  248. if dimcount>1 then
  249. begin
  250. for i:=0 to dims[0]-1 do
  251. int_dynarray_setlength(pointer((pointer(newp)+sizeof(tdynarray)+i*elesize)^),
  252. eletype,dimcount-1,@dims[1]);
  253. end;
  254. if updatep then
  255. begin
  256. p:=pointer(newp)+sizeof(tdynarray);
  257. newp^.refcount:=1;
  258. newp^.high:=dims[0]-1;
  259. end;
  260. end;
  261. { provide local access to dynarr_copy }
  262. function int_dynarray_copy(psrc : pointer;ti : pointer;
  263. lowidx,count:tdynarrayindex) : fpc_stub_dynarray;[external name 'FPC_DYNARR_COPY'];
  264. function fpc_dynarray_copy(psrc : pointer;ti : pointer;
  265. lowidx,count:tdynarrayindex) : fpc_stub_dynarray;[Public,Alias:'FPC_DYNARR_COPY'];compilerproc;
  266. var
  267. realpsrc : pdynarray;
  268. i,size : sizeint;
  269. elesize : sizeint;
  270. eletype : pointer;
  271. begin
  272. fpc_dynarray_clear(pointer(result),ti);
  273. if psrc=nil then
  274. exit;
  275. {$ifndef FPC_DYNARRAYCOPY_FIXED}
  276. if (lowidx=-1) and (count=-1) then
  277. begin
  278. lowidx:=0;
  279. count:=high(tdynarrayindex);
  280. end;
  281. {$endif FPC_DYNARRAYCOPY_FIXED}
  282. realpsrc:=pdynarray(psrc-sizeof(tdynarray));
  283. if (lowidx<0) then
  284. begin
  285. { Decrease count if index is negative, this is different from how copy()
  286. works on strings. Checked against D7. }
  287. if count<=0 then
  288. exit; { may overflow when adding lowidx }
  289. count:=count+lowidx;
  290. lowidx:=0;
  291. end;
  292. if (count>realpsrc^.high-lowidx+1) then
  293. count:=realpsrc^.high-lowidx+1;
  294. if count<=0 then
  295. exit;
  296. { skip kind and name }
  297. {$ifdef VER3_0}
  298. ti:=aligntoptr(ti+2+PByte(ti)[1]);
  299. {$else VER3_0}
  300. ti:=aligntoqword(ti+2+PByte(ti)[1]);
  301. {$endif VER3_0}
  302. elesize:=pdynarraytypedata(ti)^.elSize;
  303. { only set if type needs finalization }
  304. {$ifdef VER3_0}
  305. eletype:=pdynarraytypedata(ti)^.elType;
  306. {$else}
  307. if assigned(pdynarraytypedata(ti)^.elType) then
  308. eletype:=pdynarraytypedata(ti)^.elType^
  309. else
  310. eletype:=nil;
  311. {$endif}
  312. { create new array }
  313. size:=elesize*count;
  314. getmem(pointer(result),size+sizeof(tdynarray));
  315. pdynarray(result)^.refcount:=1;
  316. pdynarray(result)^.high:=count-1;
  317. inc(pointer(result),sizeof(tdynarray));
  318. { copy data }
  319. move(pointer(psrc+elesize*lowidx)^,pointer(result)^,size);
  320. { increment ref. count of members? }
  321. if assigned(eletype) then
  322. for i:=0 to count-1 do
  323. int_addref(pointer(pointer(result)+elesize*i),eletype);
  324. end;
  325. {$ifndef VER3_0}
  326. procedure fpc_dynarray_delete(var p : pointer;source,count : SizeInt;pti : pointer);
  327. var
  328. newhigh,
  329. i : tdynarrayindex;
  330. size : sizeint;
  331. { contains the "fixed" pointers where the refcount }
  332. { and high are at positive offsets }
  333. realp,newp : pdynarray;
  334. ti : pointer;
  335. elesize : sizeint;
  336. eletype,eletypemngd : pointer;
  337. begin
  338. { if source > high then nothing to do }
  339. if not assigned(p) or
  340. (source>pdynarray(p-sizeof(tdynarray))^.high) or
  341. (count<=0) or
  342. (source<0) then
  343. exit;
  344. { cap count }
  345. if source+count-1>pdynarray(p-sizeof(tdynarray))^.high then
  346. count:=pdynarray(p-sizeof(tdynarray))^.high-source+1;
  347. { fast path: delete whole array }
  348. if (source=0) and (count=pdynarray(p-sizeof(tdynarray))^.high+1) then
  349. begin
  350. fpc_dynarray_clear(p,pti);
  351. exit;
  352. end;
  353. { skip kind and name }
  354. {$ifdef VER3_0}
  355. ti:=aligntoptr(Pointer(pti)+2+PByte(pti)[1]);
  356. {$else VER3_0}
  357. ti:=aligntoqword(Pointer(pti)+2+PByte(pti)[1]);
  358. {$endif VER3_0}
  359. elesize:=pdynarraytypedata(ti)^.elSize;
  360. eletype:=pdynarraytypedata(ti)^.elType2^;
  361. { only set if type needs finalization }
  362. if assigned(pdynarraytypedata(ti)^.elType) then
  363. eletypemngd:=pdynarraytypedata(ti)^.elType^
  364. else
  365. eletypemngd:=nil;
  366. realp:=pdynarray(p-sizeof(tdynarray));
  367. newp:=realp;
  368. { determine new memory size }
  369. newhigh:=realp^.high-count;
  370. size:=elesize*(newhigh+1)+sizeof(tdynarray);
  371. if realp^.refcount<>1 then
  372. begin
  373. { make an unique copy }
  374. getmem(newp,size);
  375. fillchar(newp^,sizeof(tdynarray),0);
  376. { copy the elements that we still need }
  377. if source>0 then
  378. move(p^,(pointer(newp)+sizeof(tdynarray))^,source*elesize);
  379. if source+count-1<realp^.high then
  380. move((p+(source+count)*elesize)^,(pointer(newp)+sizeof(tdynarray)+source*elesize)^,(realp^.high-(source+count)+1)*elesize);
  381. { increment ref. count of managed members }
  382. if assigned(eletypemngd) then
  383. for i:=0 to newhigh do
  384. int_addref(pointer(newp)+sizeof(tdynarray)+elesize*i,eletypemngd);
  385. { a declock(ref. count) isn't enough here }
  386. { it could be that the in MT environments }
  387. { in the mean time the refcount was }
  388. { decremented }
  389. { it is, because it doesn't really matter }
  390. { if the array is now removed }
  391. fpc_dynarray_clear(p,pti);
  392. end
  393. else
  394. begin
  395. { finalize the elements that will be removed }
  396. if assigned(eletypemngd) then
  397. begin
  398. for i:=source to source+count-1 do
  399. int_finalize(p+i*elesize,eletype);
  400. end;
  401. { close the gap by moving the trailing elements to the front }
  402. move((p+(source+count)*elesize)^,(p+source*elesize)^,(realp^.high-(source+count)+1)*elesize);
  403. { resize the array }
  404. reallocmem(realp,size);
  405. newp:=realp;
  406. end;
  407. p:=pointer(newp)+sizeof(tdynarray);
  408. newp^.refcount:=1;
  409. newp^.high:=newhigh;
  410. end;
  411. {$endif VER3_0}
  412. procedure DynArraySetLength(var a: Pointer; typeInfo: Pointer; dimCnt: SizeInt; lengthVec: PSizeInt);
  413. external name 'FPC_DYNARR_SETLENGTH';
  414. function DynArraySize(a : pointer): tdynarrayindex;
  415. external name 'FPC_DYNARRAY_LENGTH';
  416. procedure DynArrayClear(var a: Pointer; typeInfo: Pointer);
  417. external name 'FPC_DYNARRAY_CLEAR';
  418. function DynArrayDim(typeInfo: Pointer): Integer;
  419. begin
  420. result:=0;
  421. while (typeInfo <> nil) and (pdynarraytypeinfo(typeInfo)^.kind = tkDynArray) do
  422. begin
  423. { skip kind and name }
  424. {$ifdef VER3_0}
  425. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  426. {$else VER3_0}
  427. typeInfo:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
  428. {$endif VER3_0}
  429. { element type info}
  430. {$ifdef VER3_0}
  431. typeInfo:=pdynarraytypedata(typeInfo)^.elType2;
  432. {$else VER3_0}
  433. typeInfo:=pdynarraytypedata(typeInfo)^.elType2^;
  434. {$endif VER3_0}
  435. Inc(result);
  436. end;
  437. end;
  438. function DynArrayBounds(a: Pointer; typeInfo: Pointer): TBoundArray;
  439. var
  440. i,dim: sizeint;
  441. begin
  442. dim:=DynArrayDim(typeInfo);
  443. SetLength(result, dim);
  444. for i:=0 to pred(dim) do
  445. if a = nil then
  446. exit
  447. else
  448. begin
  449. result[i]:=DynArraySize(a)-1;
  450. a:=PPointerArray(a)^[0];
  451. end;
  452. end;
  453. function IsDynArrayRectangular(a: Pointer; typeInfo: Pointer): Boolean;
  454. var
  455. i,j: sizeint;
  456. dim,count: sizeint;
  457. begin
  458. dim:=DynArrayDim(typeInfo);
  459. for i:=1 to pred(dim) do
  460. begin
  461. count:=DynArraySize(PPointerArray(a)^[0]);
  462. for j:=1 to Pred(DynArraySize(a)) do
  463. if count<>DynArraySize(PPointerArray(a)^[j]) then
  464. exit(false);
  465. a:=PPointerArray(a)^[0];
  466. end;
  467. result:=true;
  468. end;
  469. function DynArrayIndex(a: Pointer; const indices: array of SizeInt; typeInfo: Pointer): Pointer;
  470. var
  471. i,h: sizeint;
  472. begin
  473. h:=High(indices);
  474. for i:=0 to h do
  475. begin
  476. if i<h then
  477. a := PPointerArray(a)^[indices[i]];
  478. { skip kind and name }
  479. typeInfo:=(typeInfo+2+PByte(typeInfo)[1]);
  480. { element type info}
  481. {$ifdef VER3_0}
  482. typeInfo:=pdynarraytypedata(typeInfo)^.elType2;
  483. {$else VER3_0}
  484. typeInfo:=pdynarraytypedata(typeInfo)^.elType2^;
  485. {$endif VER3_0}
  486. if typeInfo=nil then
  487. exit(nil);
  488. end;
  489. { skip kind and name }
  490. typeInfo:=(typeInfo+2+PByte(typeInfo)[1]);
  491. result:=@(PByte(a)[indices[h]*pdynarraytypedata(typeInfo)^.elSize]);
  492. end;
  493. { obsolete but needed for bootstrapping }
  494. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_DECR_REF']; compilerproc;
  495. begin
  496. fpc_dynarray_clear(p,ti);
  497. end;