dynarr.inc 11 KB

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