dynarr.inc 11 KB

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