dynarr.inc 13 KB

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