dynarr.inc 13 KB

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