dynarr.inc 12 KB

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