dynarr.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. procedure fpc_dynarray_rangecheck(p : pointer;i : tdynarrayindex);[Public,Alias:'FPC_DYNARRAY_RANGECHECK']; compilerproc;
  22. begin
  23. if not(assigned(p)) or (i<0) or (i>pdynarray(p-sizeof(tdynarray))^.high) then
  24. HandleErrorFrame(201,get_frame);
  25. end;
  26. function fpc_dynarray_length(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_LENGTH']; compilerproc;
  27. begin
  28. if assigned(p) then
  29. fpc_dynarray_length:=pdynarray(p-sizeof(tdynarray))^.high+1
  30. else
  31. fpc_dynarray_length:=0;
  32. end;
  33. function fpc_dynarray_high(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_HIGH']; compilerproc;
  34. begin
  35. if assigned(p) then
  36. fpc_dynarray_high:=pdynarray(p-sizeof(tdynarray))^.high
  37. else
  38. fpc_dynarray_high:=-1;
  39. end;
  40. { releases and finalizes the data of a dyn. array and sets p to nil }
  41. procedure fpc_dynarray_clear_internal(p : pointer;ti : pointer);
  42. var
  43. eletype : pdynarraytypeinfo;
  44. begin
  45. if p=nil then
  46. exit;
  47. { skip kind and name }
  48. inc(pointer(ti),ord(pdynarraytypeinfo(ti)^.namelen)+2);
  49. ti:=aligntoptr(ti);
  50. eletype:=pdynarraytypeinfo(pointer(pdynarraytypeinfo(pointer(ti)+sizeof(sizeint)))^);
  51. { finalize all data }
  52. int_finalizearray(p+sizeof(tdynarray),eletype,pdynarray(p)^.high+1);
  53. { release the data }
  54. freemem(p);
  55. end;
  56. procedure fpc_dynarray_clear(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_CLEAR']; compilerproc;
  57. var
  58. realp : pdynarray;
  59. begin
  60. if (P=Nil) then
  61. exit;
  62. realp:=pdynarray(p-sizeof(tdynarray));
  63. if declocked(realp^.refcount) then
  64. fpc_dynarray_clear_internal(p-sizeof(tdynarray),ti);
  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_decr_ref(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_DECR_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. HandleErrorFrame(204,get_frame);
  78. { decr. ref. count }
  79. { should we remove the array? }
  80. if declocked(realp^.refcount) then
  81. begin
  82. fpc_dynarray_clear_internal(realp,pdynarraytypeinfo(ti));
  83. p := nil;
  84. end;
  85. end;
  86. { provide local access to dynarr_decr_ref for dynarr_setlength }
  87. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer); [external name 'FPC_DYNARRAY_DECR_REF'];
  88. procedure fpc_dynarray_incr_ref(p : pointer);[Public,Alias:'FPC_DYNARRAY_INCR_REF']; compilerproc;
  89. var
  90. realp : pdynarray;
  91. begin
  92. if p=nil then
  93. exit;
  94. realp:=pdynarray(p-sizeof(tdynarray));
  95. if realp^.refcount=0 then
  96. HandleErrorFrame(204,get_frame);
  97. inclocked(realp^.refcount);
  98. end;
  99. { provide local access to dynarr_decr_ref for dynarr_setlength }
  100. procedure fpc_dynarray_incr_ref(p : pointer); [external name 'FPC_DYNARRAY_INCR_REF'];
  101. { provide local access to dynarr_setlength }
  102. procedure int_dynarray_setlength(var p : pointer;pti : pointer;
  103. dimcount : dword;dims : pdynarrayindex);[external name 'FPC_DYNARR_SETLENGTH'];
  104. procedure fpc_dynarray_setlength(var p : pointer;pti : pointer;
  105. dimcount : dword;dims : pdynarrayindex);[Public,Alias:'FPC_DYNARR_SETLENGTH']; compilerproc;
  106. var
  107. i : tdynarrayindex;
  108. movelen,
  109. size : sizeint;
  110. { contains the "fixed" pointers where the refcount }
  111. { and high are at positive offsets }
  112. realp,newp : pdynarray;
  113. ti : pdynarraytypeinfo;
  114. updatep: boolean;
  115. elesize : sizeint;
  116. eletype : pdynarraytypeinfo;
  117. begin
  118. ti:=pdynarraytypeinfo(pti);
  119. { skip kind and name }
  120. inc(pointer(ti),ord(pdynarraytypeinfo(ti)^.namelen)+2);
  121. ti:=aligntoptr(ti);
  122. elesize:=psizeint(ti)^;
  123. eletype:=pdynarraytypeinfo(pointer(pdynarraytypeinfo(pointer(ti)+sizeof(sizeint)))^);
  124. { determine new memory size }
  125. { dims[dimcount-1] because the dimensions are in reverse order! (JM) }
  126. size:=elesize*dims[dimcount-1]+sizeof(tdynarray);
  127. updatep := false;
  128. { not assigned yet? }
  129. if not(assigned(p)) then
  130. begin
  131. if dims[dimcount-1]<0 then
  132. HandleErrorFrame(201,get_frame);
  133. { do we have to allocate memory? }
  134. if dims[dimcount-1] = 0 then
  135. exit;
  136. getmem(newp,size);
  137. fillchar(newp^,size,0);
  138. updatep := true;
  139. end
  140. else
  141. begin
  142. realp:=pdynarray(p-sizeof(tdynarray));
  143. newp := realp;
  144. { if the new dimension is 0, we've to release all data }
  145. if dims[dimcount-1]<=0 then
  146. begin
  147. if dims[dimcount-1]<0 then
  148. HandleErrorFrame(201,get_frame);
  149. if declocked(realp^.refcount) then
  150. fpc_dynarray_clear_internal(realp,pdynarraytypeinfo(pti));
  151. p:=nil;
  152. exit;
  153. end;
  154. if realp^.refcount<>1 then
  155. begin
  156. updatep := true;
  157. { make an unique copy }
  158. getmem(newp,size);
  159. fillchar(newp^,size,0);
  160. if realp^.high < dims[dimcount-1] then
  161. movelen := realp^.high+1
  162. else
  163. movelen := dims[dimcount-1];
  164. move(p^,(pointer(newp)+sizeof(tdynarray))^,elesize*movelen);
  165. { increment ref. count of members }
  166. for i:= 0 to movelen-1 do
  167. int_addref(pointer(newp)+sizeof(tdynarray)+elesize*i,eletype);
  168. { a declock(ref. count) isn't enough here }
  169. { it could be that the in MT environments }
  170. { in the mean time the refcount was }
  171. { decremented }
  172. { it is, because it doesn't really matter }
  173. { if the array is now removed }
  174. { fpc_dynarray_decr_ref(p,ti); }
  175. if declocked(realp^.refcount) then
  176. fpc_dynarray_clear_internal(realp,pdynarraytypeinfo(ti));
  177. end
  178. else if dims[dimcount-1]<>realp^.high+1 then
  179. begin
  180. { range checking is quite difficult ... }
  181. { if size overflows then it is less than }
  182. { the values it was calculated from }
  183. if (size<sizeof(tdynarray)) or
  184. ((elesize>0) and (size<elesize)) then
  185. HandleErrorFrame(201,get_frame);
  186. { resize? }
  187. { here, realp^.refcount has to be one, otherwise the previous }
  188. { if-statement would have been taken. Or is this also for MT }
  189. { code? (JM) }
  190. if realp^.refcount=1 then
  191. begin
  192. { shrink the array? }
  193. if dims[dimcount-1]<realp^.high+1 then
  194. begin
  195. int_finalizearray(pointer(realp)+sizeof(tdynarray)+
  196. elesize*dims[dimcount-1],
  197. eletype,realp^.high-dims[dimcount-1]+1);
  198. reallocmem(realp,size);
  199. end
  200. else if dims[dimcount-1]>realp^.high+1 then
  201. begin
  202. reallocmem(realp,size);
  203. fillchar((pointer(realp)+sizeof(tdynarray)+elesize*(realp^.high+1))^,
  204. (dims[dimcount-1]-realp^.high-1)*elesize,0);
  205. end;
  206. newp := realp;
  207. updatep := true;
  208. end;
  209. end;
  210. end;
  211. { handle nested arrays }
  212. if dimcount>1 then
  213. begin
  214. for i:=0 to dims[dimcount-1]-1 do
  215. int_dynarray_setlength(pointer((pointer(newp)+sizeof(tdynarray)+i*elesize)^),
  216. eletype,dimcount-1,dims);
  217. end;
  218. if updatep then
  219. begin
  220. p:=pointer(newp)+sizeof(tdynarray);
  221. newp^.refcount:=1;
  222. newp^.high:=dims[dimcount-1]-1;
  223. end;
  224. end;
  225. { provide local access to dynarr_copy }
  226. function int_dynarray_copy(psrc : pointer;ti : pointer;
  227. lowidx,count:tdynarrayindex) : pointer;[external name 'FPC_DYNARR_COPY'];
  228. function fpc_dynarray_copy(psrc : pointer;ti : pointer;
  229. lowidx,count:tdynarrayindex) : pointer;[Public,Alias:'FPC_DYNARR_COPY'];compilerproc;
  230. var
  231. realpdest,
  232. realpsrc : pdynarray;
  233. cnt,
  234. i,size : longint;
  235. highidx : tdynarrayindex;
  236. elesize : sizeint;
  237. eletype : pdynarraytypeinfo;
  238. pdest : pointer;
  239. begin
  240. highidx:=lowidx+count-1;
  241. pdest:=nil;
  242. result:=pdest;
  243. if psrc=nil then
  244. exit;
  245. realpsrc:=pdynarray(psrc-sizeof(tdynarray));
  246. { skip kind and name }
  247. inc(pointer(ti),ord(pdynarraytypeinfo(ti)^.namelen)+2);
  248. ti:=aligntoptr(ti);
  249. elesize:=psizeint(ti)^;
  250. eletype:=pdynarraytypeinfo(pointer(pdynarraytypeinfo(pointer(ti)+sizeof(sizeint)))^);
  251. { -1, -1 (highidx=lowidx-1-1=-3) is used to copy the whole array like a:=copy(b);, so
  252. update the lowidx and highidx with the values from psrc }
  253. if (lowidx=-1) and (highidx=-3) then
  254. begin
  255. lowidx:=0;
  256. highidx:=realpsrc^.high;
  257. end;
  258. { get number of elements and check for invalid values }
  259. if (lowidx<0) or (highidx<0) or (lowidx > realpsrc^.high) then
  260. HandleErrorFrame(201,get_frame);
  261. cnt:=highidx-lowidx+1;
  262. if (cnt > realpsrc^.high - lowidx + 1) then
  263. cnt := realpsrc^.high - lowidx + 1;
  264. { create new array }
  265. size:=elesize*cnt;
  266. getmem(realpdest,size+sizeof(tdynarray));
  267. pdest:=pointer(realpdest)+sizeof(tdynarray);
  268. { copy data }
  269. move(pointer(psrc+elesize*lowidx)^,pdest^,size);
  270. { fill new refcount }
  271. realpdest^.refcount:=1;
  272. realpdest^.high:=cnt-1;
  273. { increment ref. count of members? }
  274. if PByte(eletype)^ in tkManagedTypes then
  275. for i:= 0 to cnt-1 do
  276. int_addref(pointer(pdest+elesize*i),eletype);
  277. result:=pdest;
  278. end;
  279. procedure DynArraySetLength(var a: Pointer; typeInfo: Pointer; dimCnt: SizeInt; lengthVec: PSizeInt);
  280. var
  281. preallocated : array[0..10] of SizeInt;
  282. i : SizeInt;
  283. p : PSizeInt;
  284. begin
  285. if dimCnt<=high(preallocated)+1 then
  286. p:=@preallocated[0]
  287. else
  288. getmem(p,sizeof(SizeInt)*dimCnt);
  289. for i:=0 to dimCnt-1 do
  290. p[i]:=lengthVec[dimCnt-1-i];
  291. int_dynarray_setlength(a,typeInfo,dimCnt,p);
  292. if p<>@preallocated[0] then
  293. freemem(p);
  294. end;