2
0

dynarr.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. updatep := true;
  126. end
  127. else
  128. begin
  129. { if the new dimension is 0, we've to release all data }
  130. if dims[0]=0 then
  131. begin
  132. fpc_dynarray_clear(p,pti);
  133. exit;
  134. end;
  135. realp:=pdynarray(p-sizeof(tdynarray));
  136. newp := realp;
  137. if realp^.refcount<>1 then
  138. begin
  139. updatep := true;
  140. { make an unique copy }
  141. getmem(newp,size);
  142. fillchar(newp^,sizeof(tdynarray),0);
  143. if realp^.high < dims[0] then
  144. movelen := realp^.high+1
  145. else
  146. movelen := dims[0];
  147. movsize := elesize*movelen;
  148. move(p^,(pointer(newp)+sizeof(tdynarray))^, movsize);
  149. if size-sizeof(tdynarray)>movsize then
  150. fillchar((pointer(newp)+sizeof(tdynarray)+movsize)^,size-sizeof(tdynarray)-movsize,0);
  151. { increment ref. count of members }
  152. for i:= 0 to movelen-1 do
  153. int_addref(pointer(newp)+sizeof(tdynarray)+elesize*i,eletype);
  154. { a declock(ref. count) isn't enough here }
  155. { it could be that the in MT environments }
  156. { in the mean time the refcount was }
  157. { decremented }
  158. { it is, because it doesn't really matter }
  159. { if the array is now removed }
  160. fpc_dynarray_clear(p,pti);
  161. end
  162. else if dims[0]<>realp^.high+1 then
  163. begin
  164. { range checking is quite difficult ... }
  165. { if size overflows then it is less than }
  166. { the values it was calculated from }
  167. if (size<sizeof(tdynarray)) or
  168. ((elesize>0) and (size<elesize)) then
  169. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  170. { resize? }
  171. { here, realp^.refcount has to be one, otherwise the previous }
  172. { if-statement would have been taken. Or is this also for MT }
  173. { code? (JM) }
  174. if realp^.refcount=1 then
  175. begin
  176. { shrink the array? }
  177. if dims[0]<realp^.high+1 then
  178. begin
  179. int_finalizearray(pointer(realp)+sizeof(tdynarray)+
  180. elesize*dims[0],
  181. eletype,realp^.high-dims[0]+1);
  182. reallocmem(realp,size);
  183. end
  184. else if dims[0]>realp^.high+1 then
  185. begin
  186. reallocmem(realp,size);
  187. fillchar((pointer(realp)+sizeof(tdynarray)+elesize*(realp^.high+1))^,
  188. (dims[0]-realp^.high-1)*elesize,0);
  189. end;
  190. newp := realp;
  191. updatep := true;
  192. end;
  193. end;
  194. end;
  195. { handle nested arrays }
  196. if dimcount>1 then
  197. begin
  198. for i:=0 to dims[0]-1 do
  199. int_dynarray_setlength(pointer((pointer(newp)+sizeof(tdynarray)+i*elesize)^),
  200. eletype,dimcount-1,@dims[1]);
  201. end;
  202. if updatep then
  203. begin
  204. p:=pointer(newp)+sizeof(tdynarray);
  205. newp^.refcount:=1;
  206. newp^.high:=dims[0]-1;
  207. end;
  208. end;
  209. { provide local access to dynarr_copy }
  210. function int_dynarray_copy(psrc : pointer;ti : pointer;
  211. lowidx,count:tdynarrayindex) : fpc_stub_dynarray;[external name 'FPC_DYNARR_COPY'];
  212. function fpc_dynarray_copy(psrc : pointer;ti : pointer;
  213. lowidx,count:tdynarrayindex) : fpc_stub_dynarray;[Public,Alias:'FPC_DYNARR_COPY'];compilerproc;
  214. var
  215. realpsrc : pdynarray;
  216. i,size : sizeint;
  217. elesize : sizeint;
  218. eletype : pointer;
  219. begin
  220. fpc_dynarray_clear(pointer(result),ti);
  221. if psrc=nil then
  222. exit;
  223. {$ifndef FPC_DYNARRAYCOPY_FIXED}
  224. if (lowidx=-1) and (count=-1) then
  225. begin
  226. lowidx:=0;
  227. count:=high(tdynarrayindex);
  228. end;
  229. {$endif FPC_DYNARRAYCOPY_FIXED}
  230. realpsrc:=pdynarray(psrc-sizeof(tdynarray));
  231. if (lowidx<0) then
  232. begin
  233. { Decrease count if index is negative, this is different from how copy()
  234. works on strings. Checked against D7. }
  235. if count<=0 then
  236. exit; { may overflow when adding lowidx }
  237. count:=count+lowidx;
  238. lowidx:=0;
  239. end;
  240. if (count>realpsrc^.high-lowidx+1) then
  241. count:=realpsrc^.high-lowidx+1;
  242. if count<=0 then
  243. exit;
  244. { skip kind and name }
  245. ti:=aligntoptr(ti+2+PByte(ti)[1]);
  246. elesize:=pdynarraytypedata(ti)^.elSize;
  247. eletype:=pdynarraytypedata(ti)^.elType2;
  248. { create new array }
  249. size:=elesize*count;
  250. getmem(pointer(result),size+sizeof(tdynarray));
  251. pdynarray(result)^.refcount:=1;
  252. pdynarray(result)^.high:=count-1;
  253. inc(pointer(result),sizeof(tdynarray));
  254. { copy data }
  255. move(pointer(psrc+elesize*lowidx)^,pointer(result)^,size);
  256. { increment ref. count of members? }
  257. if PByte(eletype)^ in tkManagedTypes then
  258. for i:=0 to count-1 do
  259. int_addref(pointer(pointer(result)+elesize*i),eletype);
  260. end;
  261. procedure DynArraySetLength(var a: Pointer; typeInfo: Pointer; dimCnt: SizeInt; lengthVec: PSizeInt);
  262. external name 'FPC_DYNARR_SETLENGTH';
  263. function DynArraySize(a : pointer): tdynarrayindex;
  264. external name 'FPC_DYNARRAY_LENGTH';
  265. procedure DynArrayClear(var a: Pointer; typeInfo: Pointer);
  266. external name 'FPC_DYNARRAY_CLEAR';
  267. function DynArrayDim(typeInfo: Pointer): Integer;
  268. begin
  269. result:=0;
  270. while (typeInfo <> nil) and (pdynarraytypeinfo(typeInfo)^.kind = tkDynArray) do
  271. begin
  272. { skip kind and name }
  273. typeInfo:=aligntoptr(typeInfo+2+PByte(typeInfo)[1]);
  274. { element type info}
  275. typeInfo:=pdynarraytypedata(typeInfo)^.elType2;
  276. Inc(result);
  277. end;
  278. end;
  279. function DynArrayBounds(a: Pointer; typeInfo: Pointer): TBoundArray;
  280. var
  281. i,dim: sizeint;
  282. begin
  283. dim:=DynArrayDim(typeInfo);
  284. SetLength(result, dim);
  285. for i:=0 to pred(dim) do
  286. if a = nil then
  287. exit
  288. else
  289. begin
  290. result[i]:=DynArraySize(a)-1;
  291. a:=PPointerArray(a)^[0];
  292. end;
  293. end;
  294. function IsDynArrayRectangular(a: Pointer; typeInfo: Pointer): Boolean;
  295. var
  296. i,j: sizeint;
  297. dim,count: sizeint;
  298. begin
  299. dim:=DynArrayDim(typeInfo);
  300. for i:=1 to pred(dim) do
  301. begin
  302. count:=DynArraySize(PPointerArray(a)^[0]);
  303. for j:=1 to Pred(DynArraySize(a)) do
  304. if count<>DynArraySize(PPointerArray(a)^[j]) then
  305. exit(false);
  306. a:=PPointerArray(a)^[0];
  307. end;
  308. result:=true;
  309. end;
  310. function DynArrayIndex(a: Pointer; const indices: array of SizeInt; typeInfo: Pointer): Pointer;
  311. var
  312. i,h: sizeint;
  313. begin
  314. h:=High(indices);
  315. for i:=0 to h do
  316. begin
  317. if i<h then
  318. a := PPointerArray(a)^[indices[i]];
  319. { skip kind and name }
  320. typeInfo:=(typeInfo+2+PByte(typeInfo)[1]);
  321. { element type info}
  322. typeInfo:=pdynarraytypedata(typeInfo)^.elType2;
  323. if typeInfo=nil then
  324. exit(nil);
  325. end;
  326. { skip kind and name }
  327. typeInfo:=(typeInfo+2+PByte(typeInfo)[1]);
  328. result:=@(PByte(a)[indices[h]*pdynarraytypedata(typeInfo)^.elSize]);
  329. end;
  330. { obsolete but needed for bootstrapping }
  331. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_DECR_REF']; compilerproc;
  332. begin
  333. fpc_dynarray_clear(p,ti);
  334. end;