dynarr.inc 13 KB

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