dynarr.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 VER"_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. ti:=aligntoptr(ti+2+PByte(ti)[1]);
  79. if assigned(pdynarraytypedata(ti)^.elType) then
  80. int_finalizearray(p,pdynarraytypedata(ti)^.elType{$ifndef VER3_0}^{$endif},realp^.high+1);
  81. freemem(realp);
  82. end;
  83. p:=nil;
  84. end;
  85. { alias for internal use }
  86. Procedure fpc_dynarray_clear (var p : pointer;ti : pointer);[external name 'FPC_DYNARRAY_CLEAR'];
  87. procedure fpc_dynarray_incr_ref(p : pointer);[Public,Alias:'FPC_DYNARRAY_INCR_REF']; compilerproc;
  88. var
  89. realp : pdynarray;
  90. begin
  91. if p=nil then
  92. exit;
  93. realp:=pdynarray(p-sizeof(tdynarray));
  94. if realp^.refcount=0 then
  95. HandleErrorAddrFrameInd(204,get_pc_addr,get_frame);
  96. inclocked(realp^.refcount);
  97. end;
  98. { provide local access to dynarr_decr_ref for dynarr_setlength }
  99. procedure fpc_dynarray_incr_ref(p : pointer); [external name 'FPC_DYNARRAY_INCR_REF'];
  100. procedure fpc_dynarray_assign(var dest: Pointer; src: Pointer; ti: pointer);[public,alias:'FPC_DYNARRAY_ASSIGN']; compilerproc;
  101. begin
  102. fpc_dynarray_incr_ref(src);
  103. fpc_dynarray_clear(dest,ti);
  104. Dest:=Src;
  105. end;
  106. procedure fpc_dynarray_assign(var dest: Pointer; src: Pointer; ti: pointer);[external name 'FPC_DYNARRAY_ASSIGN'];
  107. { provide local access to dynarr_setlength }
  108. procedure int_dynarray_setlength(var p : pointer;pti : pointer;
  109. dimcount : sizeint;dims : pdynarrayindex);[external name 'FPC_DYNARR_SETLENGTH'];
  110. procedure fpc_dynarray_setlength(var p : pointer;pti : pointer;
  111. dimcount : sizeint;dims : pdynarrayindex);[Public,Alias:'FPC_DYNARR_SETLENGTH']; compilerproc;
  112. var
  113. i : tdynarrayindex;
  114. movelen,
  115. size : sizeint;
  116. { contains the "fixed" pointers where the refcount }
  117. { and high are at positive offsets }
  118. realp,newp : pdynarray;
  119. ti : pointer;
  120. updatep: boolean;
  121. elesize : sizeint;
  122. eletype,eletypemngd : pointer;
  123. movsize : sizeint;
  124. begin
  125. { negative length is not allowed }
  126. if dims[0]<0 then
  127. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  128. { skip kind and name }
  129. ti:=aligntoptr(Pointer(pti)+2+PByte(pti)[1]);
  130. elesize:=pdynarraytypedata(ti)^.elSize;
  131. {$ifdef VER3_0}
  132. eletype:=pdynarraytypedata(ti)^.elType2;
  133. {$else}
  134. eletype:=pdynarraytypedata(ti)^.elType2^;
  135. {$endif}
  136. { only set if type needs finalization }
  137. {$ifdef VER3_0}
  138. eletypemngd:=pdynarraytypedata(ti)^.elType;
  139. {$else}
  140. if assigned(pdynarraytypedata(ti)^.elType) then
  141. eletypemngd:=pdynarraytypedata(ti)^.elType^
  142. else
  143. eletypemngd:=nil;
  144. {$endif}
  145. { determine new memory size }
  146. size:=elesize*dims[0]+sizeof(tdynarray);
  147. updatep := false;
  148. { not assigned yet? }
  149. if not(assigned(p)) then
  150. begin
  151. { do we have to allocate memory? }
  152. if dims[0] = 0 then
  153. exit;
  154. getmem(newp,size);
  155. fillchar(newp^,size,0);
  156. updatep := true;
  157. end
  158. else
  159. begin
  160. { if the new dimension is 0, we've to release all data }
  161. if dims[0]=0 then
  162. begin
  163. fpc_dynarray_clear(p,pti);
  164. exit;
  165. end;
  166. realp:=pdynarray(p-sizeof(tdynarray));
  167. newp := realp;
  168. if realp^.refcount<>1 then
  169. begin
  170. updatep := true;
  171. { make an unique copy }
  172. getmem(newp,size);
  173. fillchar(newp^,sizeof(tdynarray),0);
  174. if realp^.high < dims[0] then
  175. movelen := realp^.high+1
  176. else
  177. movelen := dims[0];
  178. movsize := elesize*movelen;
  179. move(p^,(pointer(newp)+sizeof(tdynarray))^, movsize);
  180. if size-sizeof(tdynarray)>movsize then
  181. fillchar((pointer(newp)+sizeof(tdynarray)+movsize)^,size-sizeof(tdynarray)-movsize,0);
  182. { increment ref. count of managed members }
  183. if assigned(eletypemngd) then
  184. for i:= 0 to movelen-1 do
  185. int_addref(pointer(newp)+sizeof(tdynarray)+elesize*i,eletypemngd);
  186. { a declock(ref. count) isn't enough here }
  187. { it could be that the in MT environments }
  188. { in the mean time the refcount was }
  189. { decremented }
  190. { it is, because it doesn't really matter }
  191. { if the array is now removed }
  192. fpc_dynarray_clear(p,pti);
  193. end
  194. else if dims[0]<>realp^.high+1 then
  195. begin
  196. { range checking is quite difficult ... }
  197. { if size overflows then it is less than }
  198. { the values it was calculated from }
  199. if (size<sizeof(tdynarray)) or
  200. ((elesize>0) and (size<elesize)) then
  201. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  202. { resize? }
  203. { here, realp^.refcount has to be one, otherwise the previous }
  204. { if-statement would have been taken. Or is this also for MT }
  205. { code? (JM) }
  206. if realp^.refcount=1 then
  207. begin
  208. { shrink the array? }
  209. if dims[0]<realp^.high+1 then
  210. begin
  211. if assigned(eletypemngd) then
  212. int_finalizearray(pointer(realp)+sizeof(tdynarray)+
  213. elesize*dims[0],
  214. eletypemngd,realp^.high-dims[0]+1);
  215. reallocmem(realp,size);
  216. end
  217. else if dims[0]>realp^.high+1 then
  218. begin
  219. reallocmem(realp,size);
  220. fillchar((pointer(realp)+sizeof(tdynarray)+elesize*(realp^.high+1))^,
  221. (dims[0]-realp^.high-1)*elesize,0);
  222. end;
  223. newp := realp;
  224. updatep := true;
  225. end;
  226. end;
  227. end;
  228. { handle nested arrays }
  229. if dimcount>1 then
  230. begin
  231. for i:=0 to dims[0]-1 do
  232. int_dynarray_setlength(pointer((pointer(newp)+sizeof(tdynarray)+i*elesize)^),
  233. eletype,dimcount-1,@dims[1]);
  234. end;
  235. if updatep then
  236. begin
  237. p:=pointer(newp)+sizeof(tdynarray);
  238. newp^.refcount:=1;
  239. newp^.high:=dims[0]-1;
  240. end;
  241. end;
  242. { provide local access to dynarr_copy }
  243. function int_dynarray_copy(psrc : pointer;ti : pointer;
  244. lowidx,count:tdynarrayindex) : fpc_stub_dynarray;[external name 'FPC_DYNARR_COPY'];
  245. function fpc_dynarray_copy(psrc : pointer;ti : pointer;
  246. lowidx,count:tdynarrayindex) : fpc_stub_dynarray;[Public,Alias:'FPC_DYNARR_COPY'];compilerproc;
  247. var
  248. realpsrc : pdynarray;
  249. i,size : sizeint;
  250. elesize : sizeint;
  251. eletype : pointer;
  252. begin
  253. fpc_dynarray_clear(pointer(result),ti);
  254. if psrc=nil then
  255. exit;
  256. {$ifndef FPC_DYNARRAYCOPY_FIXED}
  257. if (lowidx=-1) and (count=-1) then
  258. begin
  259. lowidx:=0;
  260. count:=high(tdynarrayindex);
  261. end;
  262. {$endif FPC_DYNARRAYCOPY_FIXED}
  263. realpsrc:=pdynarray(psrc-sizeof(tdynarray));
  264. if (lowidx<0) then
  265. begin
  266. { Decrease count if index is negative, this is different from how copy()
  267. works on strings. Checked against D7. }
  268. if count<=0 then
  269. exit; { may overflow when adding lowidx }
  270. count:=count+lowidx;
  271. lowidx:=0;
  272. end;
  273. if (count>realpsrc^.high-lowidx+1) then
  274. count:=realpsrc^.high-lowidx+1;
  275. if count<=0 then
  276. exit;
  277. { skip kind and name }
  278. ti:=aligntoptr(ti+2+PByte(ti)[1]);
  279. elesize:=pdynarraytypedata(ti)^.elSize;
  280. { only set if type needs finalization }
  281. {$ifdef VER3_0}
  282. eletype:=pdynarraytypedata(ti)^.elType;
  283. {$else}
  284. if assigned(pdynarraytypedata(ti)^.elType) then
  285. eletype:=pdynarraytypedata(ti)^.elType^
  286. else
  287. eletype:=nil;
  288. {$endif}
  289. { create new array }
  290. size:=elesize*count;
  291. getmem(pointer(result),size+sizeof(tdynarray));
  292. pdynarray(result)^.refcount:=1;
  293. pdynarray(result)^.high:=count-1;
  294. inc(pointer(result),sizeof(tdynarray));
  295. { copy data }
  296. move(pointer(psrc+elesize*lowidx)^,pointer(result)^,size);
  297. { increment ref. count of members? }
  298. if assigned(eletype) then
  299. for i:=0 to count-1 do
  300. int_addref(pointer(pointer(result)+elesize*i),eletype);
  301. end;
  302. procedure DynArraySetLength(var a: Pointer; typeInfo: Pointer; dimCnt: SizeInt; lengthVec: PSizeInt);
  303. external name 'FPC_DYNARR_SETLENGTH';
  304. function DynArraySize(a : pointer): tdynarrayindex;
  305. external name 'FPC_DYNARRAY_LENGTH';
  306. procedure DynArrayClear(var a: Pointer; typeInfo: Pointer);
  307. external name 'FPC_DYNARRAY_CLEAR';
  308. function DynArrayDim(typeInfo: Pointer): Integer;
  309. begin
  310. result:=0;
  311. while (typeInfo <> nil) and (pdynarraytypeinfo(typeInfo)^.kind = tkDynArray) do
  312. begin
  313. { skip kind and name }
  314. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  315. { element type info}
  316. {$ifdef VER3_0}
  317. typeInfo:=pdynarraytypedata(typeInfo)^.elType2;
  318. {$else VER3_0}
  319. typeInfo:=pdynarraytypedata(typeInfo)^.elType2^;
  320. {$endif VER3_0}
  321. Inc(result);
  322. end;
  323. end;
  324. function DynArrayBounds(a: Pointer; typeInfo: Pointer): TBoundArray;
  325. var
  326. i,dim: sizeint;
  327. begin
  328. dim:=DynArrayDim(typeInfo);
  329. SetLength(result, dim);
  330. for i:=0 to pred(dim) do
  331. if a = nil then
  332. exit
  333. else
  334. begin
  335. result[i]:=DynArraySize(a)-1;
  336. a:=PPointerArray(a)^[0];
  337. end;
  338. end;
  339. function IsDynArrayRectangular(a: Pointer; typeInfo: Pointer): Boolean;
  340. var
  341. i,j: sizeint;
  342. dim,count: sizeint;
  343. begin
  344. dim:=DynArrayDim(typeInfo);
  345. for i:=1 to pred(dim) do
  346. begin
  347. count:=DynArraySize(PPointerArray(a)^[0]);
  348. for j:=1 to Pred(DynArraySize(a)) do
  349. if count<>DynArraySize(PPointerArray(a)^[j]) then
  350. exit(false);
  351. a:=PPointerArray(a)^[0];
  352. end;
  353. result:=true;
  354. end;
  355. function DynArrayIndex(a: Pointer; const indices: array of SizeInt; typeInfo: Pointer): Pointer;
  356. var
  357. i,h: sizeint;
  358. begin
  359. h:=High(indices);
  360. for i:=0 to h do
  361. begin
  362. if i<h then
  363. a := PPointerArray(a)^[indices[i]];
  364. { skip kind and name }
  365. typeInfo:=(typeInfo+2+PByte(typeInfo)[1]);
  366. { element type info}
  367. {$ifdef VER3_0}
  368. typeInfo:=pdynarraytypedata(typeInfo)^.elType2;
  369. {$else VER3_0}
  370. typeInfo:=pdynarraytypedata(typeInfo)^.elType2^;
  371. {$endif VER3_0}
  372. if typeInfo=nil then
  373. exit(nil);
  374. end;
  375. { skip kind and name }
  376. typeInfo:=(typeInfo+2+PByte(typeInfo)[1]);
  377. result:=@(PByte(a)[indices[h]*pdynarraytypedata(typeInfo)^.elSize]);
  378. end;
  379. { obsolete but needed for bootstrapping }
  380. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_DECR_REF']; compilerproc;
  381. begin
  382. fpc_dynarray_clear(p,ti);
  383. end;