dynarr.inc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. pdynarraytypedata = ^tdynarraytypedata;
  22. tdynarraytypedata =
  23. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  24. packed
  25. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  26. record
  27. elSize : PtrUInt;
  28. elType2 : Pointer;
  29. varType : Longint;
  30. end;
  31. procedure fpc_dynarray_rangecheck(p : pointer;i : tdynarrayindex);[Public,Alias:'FPC_DYNARRAY_RANGECHECK']; compilerproc;
  32. begin
  33. if not(assigned(p)) or (i<0) or (i>pdynarray(p-sizeof(tdynarray))^.high) then
  34. HandleErrorFrame(201,get_frame);
  35. end;
  36. function fpc_dynarray_length(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_LENGTH']; compilerproc;
  37. begin
  38. if assigned(p) then
  39. fpc_dynarray_length:=pdynarray(p-sizeof(tdynarray))^.high+1
  40. else
  41. fpc_dynarray_length:=0;
  42. end;
  43. function fpc_dynarray_high(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_HIGH']; compilerproc;
  44. begin
  45. if assigned(p) then
  46. fpc_dynarray_high:=pdynarray(p-sizeof(tdynarray))^.high
  47. else
  48. fpc_dynarray_high:=-1;
  49. end;
  50. procedure fpc_dynarray_clear(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_CLEAR']; compilerproc;
  51. var
  52. realp : pdynarray;
  53. begin
  54. if (P=Nil) then
  55. exit;
  56. realp:=pdynarray(p-sizeof(tdynarray));
  57. if realp^.refcount=0 then
  58. HandleErrorFrame(204,get_frame);
  59. if declocked(realp^.refcount) then
  60. begin
  61. ti:=aligntoptr(ti+2+PByte(ti)[1]);
  62. int_finalizearray(p,pdynarraytypedata(ti)^.elType2,realp^.high+1);
  63. freemem(realp);
  64. end;
  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_incr_ref(p : pointer);[Public,Alias:'FPC_DYNARRAY_INCR_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. inclocked(realp^.refcount);
  79. end;
  80. { provide local access to dynarr_decr_ref for dynarr_setlength }
  81. procedure fpc_dynarray_incr_ref(p : pointer); [external name 'FPC_DYNARRAY_INCR_REF'];
  82. procedure fpc_dynarray_assign(var dest: Pointer; src: Pointer; ti: pointer);[public,alias:'FPC_DYNARRAY_ASSIGN']; compilerproc;
  83. begin
  84. fpc_dynarray_incr_ref(src);
  85. fpc_dynarray_clear(dest,ti);
  86. Dest:=Src;
  87. end;
  88. procedure fpc_dynarray_assign(var dest: Pointer; src: Pointer; ti: pointer);[external name 'FPC_DYNARRAY_ASSIGN'];
  89. { provide local access to dynarr_setlength }
  90. procedure int_dynarray_setlength(var p : pointer;pti : pointer;
  91. dimcount : sizeint;dims : pdynarrayindex);[external name 'FPC_DYNARR_SETLENGTH'];
  92. procedure fpc_dynarray_setlength(var p : pointer;pti : pointer;
  93. dimcount : sizeint;dims : pdynarrayindex);[Public,Alias:'FPC_DYNARR_SETLENGTH']; compilerproc;
  94. var
  95. i : tdynarrayindex;
  96. movelen,
  97. size : sizeint;
  98. { contains the "fixed" pointers where the refcount }
  99. { and high are at positive offsets }
  100. realp,newp : pdynarray;
  101. ti : pointer;
  102. updatep: boolean;
  103. elesize : sizeint;
  104. eletype : pointer;
  105. begin
  106. { negative length is not allowed }
  107. if dims[0]<0 then
  108. HandleErrorFrame(201,get_frame);
  109. { skip kind and name }
  110. ti:=aligntoptr(Pointer(pti)+2+PByte(pti)[1]);
  111. elesize:=pdynarraytypedata(ti)^.elSize;
  112. eletype:=pdynarraytypedata(ti)^.elType2;
  113. { determine new memory size }
  114. size:=elesize*dims[0]+sizeof(tdynarray);
  115. updatep := false;
  116. { not assigned yet? }
  117. if not(assigned(p)) then
  118. begin
  119. { do we have to allocate memory? }
  120. if dims[0] = 0 then
  121. exit;
  122. getmem(newp,size);
  123. fillchar(newp^,size,0);
  124. updatep := true;
  125. end
  126. else
  127. begin
  128. { if the new dimension is 0, we've to release all data }
  129. if dims[0]=0 then
  130. begin
  131. fpc_dynarray_clear(p,pti);
  132. exit;
  133. end;
  134. realp:=pdynarray(p-sizeof(tdynarray));
  135. newp := realp;
  136. if realp^.refcount<>1 then
  137. begin
  138. updatep := true;
  139. { make an unique copy }
  140. getmem(newp,size);
  141. fillchar(newp^,size,0);
  142. if realp^.high < dims[0] then
  143. movelen := realp^.high+1
  144. else
  145. movelen := dims[0];
  146. move(p^,(pointer(newp)+sizeof(tdynarray))^,elesize*movelen);
  147. { increment ref. count of members }
  148. for i:= 0 to movelen-1 do
  149. int_addref(pointer(newp)+sizeof(tdynarray)+elesize*i,eletype);
  150. { a declock(ref. count) isn't enough here }
  151. { it could be that the in MT environments }
  152. { in the mean time the refcount was }
  153. { decremented }
  154. { it is, because it doesn't really matter }
  155. { if the array is now removed }
  156. fpc_dynarray_clear(p,pti);
  157. end
  158. else if dims[0]<>realp^.high+1 then
  159. begin
  160. { range checking is quite difficult ... }
  161. { if size overflows then it is less than }
  162. { the values it was calculated from }
  163. if (size<sizeof(tdynarray)) or
  164. ((elesize>0) and (size<elesize)) then
  165. HandleErrorFrame(201,get_frame);
  166. { resize? }
  167. { here, realp^.refcount has to be one, otherwise the previous }
  168. { if-statement would have been taken. Or is this also for MT }
  169. { code? (JM) }
  170. if realp^.refcount=1 then
  171. begin
  172. { shrink the array? }
  173. if dims[0]<realp^.high+1 then
  174. begin
  175. int_finalizearray(pointer(realp)+sizeof(tdynarray)+
  176. elesize*dims[0],
  177. eletype,realp^.high-dims[0]+1);
  178. reallocmem(realp,size);
  179. end
  180. else if dims[0]>realp^.high+1 then
  181. begin
  182. reallocmem(realp,size);
  183. fillchar((pointer(realp)+sizeof(tdynarray)+elesize*(realp^.high+1))^,
  184. (dims[0]-realp^.high-1)*elesize,0);
  185. end;
  186. newp := realp;
  187. updatep := true;
  188. end;
  189. end;
  190. end;
  191. { handle nested arrays }
  192. if dimcount>1 then
  193. begin
  194. for i:=0 to dims[0]-1 do
  195. int_dynarray_setlength(pointer((pointer(newp)+sizeof(tdynarray)+i*elesize)^),
  196. eletype,dimcount-1,@dims[1]);
  197. end;
  198. if updatep then
  199. begin
  200. p:=pointer(newp)+sizeof(tdynarray);
  201. newp^.refcount:=1;
  202. newp^.high:=dims[0]-1;
  203. end;
  204. end;
  205. { provide local access to dynarr_copy }
  206. function int_dynarray_copy(psrc : pointer;ti : pointer;
  207. lowidx,count:tdynarrayindex) : pointer;[external name 'FPC_DYNARR_COPY'];
  208. function fpc_dynarray_copy(psrc : pointer;ti : pointer;
  209. lowidx,count:tdynarrayindex) : pointer;[Public,Alias:'FPC_DYNARR_COPY'];compilerproc;
  210. var
  211. realpsrc : pdynarray;
  212. i,size : sizeint;
  213. elesize : sizeint;
  214. eletype : pointer;
  215. begin
  216. result:=nil;
  217. if psrc=nil then
  218. exit;
  219. {$ifndef FPC_DYNARRAYCOPY_FIXED}
  220. if (lowidx=-1) and (count=-1) then
  221. begin
  222. lowidx:=0;
  223. count:=high(tdynarrayindex);
  224. end;
  225. {$endif FPC_DYNARRAYCOPY_FIXED}
  226. realpsrc:=pdynarray(psrc-sizeof(tdynarray));
  227. if (lowidx<0) then
  228. begin
  229. { Decrease count if index is negative, this is different from how copy()
  230. works on strings. Checked against D7. }
  231. if count<=0 then
  232. exit; { may overflow when adding lowidx }
  233. count:=count+lowidx;
  234. lowidx:=0;
  235. end;
  236. if (count>realpsrc^.high-lowidx+1) then
  237. count:=realpsrc^.high-lowidx+1;
  238. if count<=0 then
  239. exit;
  240. { skip kind and name }
  241. ti:=aligntoptr(ti+2+PByte(ti)[1]);
  242. elesize:=pdynarraytypedata(ti)^.elSize;
  243. eletype:=pdynarraytypedata(ti)^.elType2;
  244. { create new array }
  245. size:=elesize*count;
  246. getmem(result,size+sizeof(tdynarray));
  247. pdynarray(result)^.refcount:=1;
  248. pdynarray(result)^.high:=count-1;
  249. inc(result,sizeof(tdynarray));
  250. { copy data }
  251. move(pointer(psrc+elesize*lowidx)^,result^,size);
  252. { increment ref. count of members? }
  253. if PByte(eletype)^ in tkManagedTypes then
  254. for i:=0 to count-1 do
  255. int_addref(pointer(result+elesize*i),eletype);
  256. end;
  257. procedure DynArraySetLength(var a: Pointer; typeInfo: Pointer; dimCnt: SizeInt; lengthVec: PSizeInt);
  258. external name 'FPC_DYNARR_SETLENGTH';
  259. { obsolete but needed for bootstrapping }
  260. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_DECR_REF']; compilerproc;
  261. begin
  262. fpc_dynarray_clear(p,ti);
  263. end;