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