dynarr.inc 11 KB

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