dynarr.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2000 by Florian Klaempfl
  5. member of the Free Pascal development team.
  6. This file implements the helper routines for dyn. Arrays in FPC
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************
  13. }
  14. type
  15. { don't add new fields, the size is used }
  16. { to calculate memory requirements }
  17. pdynarray = ^tdynarray;
  18. tdynarray = packed record
  19. refcount : longint;
  20. high : tdynarrayindex;
  21. end;
  22. pdynarraytypeinfo = ^tdynarraytypeinfo;
  23. tdynarraytypeinfo = packed record
  24. kind : byte;
  25. namelen : byte;
  26. { here the chars follow, we've to skip them }
  27. elesize : sizeint;
  28. eletype : pdynarraytypeinfo;
  29. end;
  30. function aligntoptr(p : pointer) : pointer;
  31. begin
  32. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  33. if (ptrint(p) mod sizeof(ptrint))<>0 then
  34. inc(ptrint(p),sizeof(ptrint)-ptrint(p) mod sizeof(ptrint));
  35. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  36. result:=p;
  37. end;
  38. procedure fpc_dynarray_rangecheck(p : pointer;i : tdynarrayindex);[Public,Alias:'FPC_DYNARRAY_RANGECHECK']; {$ifdef hascompilerproc} compilerproc; {$endif}
  39. begin
  40. if not(assigned(p)) or (i<0) or (i>pdynarray(p-sizeof(tdynarray))^.high) then
  41. HandleErrorFrame(201,get_frame);
  42. end;
  43. function fpc_dynarray_length(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  44. begin
  45. if assigned(p) then
  46. fpc_dynarray_length:=pdynarray(p-sizeof(tdynarray))^.high+1
  47. else
  48. fpc_dynarray_length:=0;
  49. end;
  50. function fpc_dynarray_high(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_HIGH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  51. begin
  52. if assigned(p) then
  53. fpc_dynarray_high:=pdynarray(p-sizeof(tdynarray))^.high
  54. else
  55. fpc_dynarray_high:=-1;
  56. end;
  57. { releases and finalizes the data of a dyn. array and sets p to nil }
  58. procedure fpc_dynarray_clear_internal(p : pointer;ti : pointer);
  59. var
  60. elesize : sizeint;
  61. eletype : pdynarraytypeinfo;
  62. begin
  63. if p=nil then
  64. exit;
  65. { skip kind and name }
  66. inc(pointer(ti),ord(pdynarraytypeinfo(ti)^.namelen)+2);
  67. {$ifdef FPC_ALIGNSRTTI}
  68. ti:=aligntoptr(ti);
  69. {$endif FPC_ALIGNSRTTI}
  70. elesize:=psizeint(ti)^;
  71. eletype:=pdynarraytypeinfo(pointer(pdynarraytypeinfo(pointer(ti)+sizeof(sizeint)))^);
  72. { finalize all data }
  73. int_finalizearray(p+sizeof(tdynarray),eletype,pdynarray(p)^.high+1,
  74. elesize);
  75. { release the data }
  76. freemem(p);
  77. end;
  78. procedure fpc_dynarray_clear(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_CLEAR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  79. var
  80. realp : pdynarray;
  81. begin
  82. if (P=Nil) then
  83. exit;
  84. realp:=pdynarray(p-sizeof(tdynarray));
  85. if declocked(realp^.refcount) then
  86. fpc_dynarray_clear_internal(p-sizeof(tdynarray),ti);
  87. p:=nil;
  88. end;
  89. {$ifdef hascompilerproc}
  90. { alias for internal use }
  91. Procedure fpc_dynarray_clear (var p : pointer;ti : pointer);[external name 'FPC_DYNARRAY_CLEAR'];
  92. {$endif hascompilerproc}
  93. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[Public,Alias:'FPC_DYNARRAY_DECR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  94. var
  95. realp : pdynarray;
  96. begin
  97. if p=nil then
  98. exit;
  99. realp:=pdynarray(p-sizeof(tdynarray));
  100. if realp^.refcount=0 then
  101. HandleErrorFrame(204,get_frame);
  102. { decr. ref. count }
  103. { should we remove the array? }
  104. if declocked(realp^.refcount) then
  105. fpc_dynarray_clear_internal(realp,pdynarraytypeinfo(ti));
  106. p := nil;
  107. end;
  108. {$ifdef hascompilerproc}
  109. { provide local access to dynarr_decr_ref for dynarr_setlength }
  110. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif} [external name 'FPC_DYNARRAY_DECR_REF'];
  111. {$endif}
  112. procedure fpc_dynarray_incr_ref(p : pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[Public,Alias:'FPC_DYNARRAY_INCR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  113. var
  114. realp : pdynarray;
  115. begin
  116. if p=nil then
  117. exit;
  118. realp:=pdynarray(p-sizeof(tdynarray));
  119. if realp^.refcount=0 then
  120. HandleErrorFrame(204,get_frame);
  121. inclocked(realp^.refcount);
  122. end;
  123. {$ifdef hascompilerproc}
  124. { provide local access to dynarr_decr_ref for dynarr_setlength }
  125. procedure fpc_dynarray_incr_ref(p : pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[external name 'FPC_DYNARRAY_INCR_REF'];
  126. {$endif}
  127. { provide local access to dynarr_setlength }
  128. procedure int_dynarray_setlength(var p : pointer;pti : pointer;
  129. dimcount : dword;dims : pdynarrayindex);[external name 'FPC_DYNARR_SETLENGTH'];
  130. procedure fpc_dynarray_setlength(var p : pointer;pti : pointer;
  131. dimcount : dword;dims : pdynarrayindex);[Public,Alias:'FPC_DYNARR_SETLENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  132. var
  133. i : tdynarrayindex;
  134. movelen,
  135. size : sizeint;
  136. { contains the "fixed" pointers where the refcount }
  137. { and high are at positive offsets }
  138. realp,newp : pdynarray;
  139. ti : pdynarraytypeinfo;
  140. updatep: boolean;
  141. elesize : sizeint;
  142. eletype : pdynarraytypeinfo;
  143. begin
  144. ti:=pdynarraytypeinfo(pti);
  145. { skip kind and name }
  146. inc(pointer(ti),ord(pdynarraytypeinfo(ti)^.namelen)+2);
  147. {$ifdef FPC_ALIGNSRTTI}
  148. ti:=aligntoptr(ti);
  149. {$endif FPC_ALIGNSRTTI}
  150. elesize:=psizeint(ti)^;
  151. eletype:=pdynarraytypeinfo(pointer(pdynarraytypeinfo(pointer(ti)+sizeof(sizeint)))^);
  152. { determine new memory size }
  153. { dims[dimcount-1] because the dimensions are in reverse order! (JM) }
  154. size:=elesize*dims[dimcount-1]+sizeof(tdynarray);
  155. updatep := false;
  156. { not assigned yet? }
  157. if not(assigned(p)) then
  158. begin
  159. { do we have to allocate memory? }
  160. if dims[dimcount-1] = 0 then
  161. exit;
  162. getmem(newp,size);
  163. fillchar(newp^,size,0);
  164. updatep := true;
  165. end
  166. else
  167. begin
  168. realp:=pdynarray(p-sizeof(tdynarray));
  169. newp := realp;
  170. { if the new dimension is 0, we've to release all data }
  171. if dims[dimcount-1]<=0 then
  172. begin
  173. if dims[dimcount-1]<0 then
  174. HandleErrorFrame(201,get_frame);
  175. if declocked(realp^.refcount) then
  176. fpc_dynarray_clear_internal(realp,pdynarraytypeinfo(pti));
  177. p:=nil;
  178. exit;
  179. end;
  180. if realp^.refcount<>1 then
  181. begin
  182. updatep := true;
  183. { make an unique copy }
  184. getmem(newp,size);
  185. fillchar(newp^,size,0);
  186. if realp^.high < dims[dimcount-1] then
  187. movelen := realp^.high+1
  188. else
  189. movelen := dims[dimcount-1];
  190. move(p^,(pointer(newp)+sizeof(tdynarray))^,elesize*movelen);
  191. { increment ref. count of members }
  192. for i:= 0 to movelen-1 do
  193. int_addref(pointer(newp)+sizeof(tdynarray)+elesize*i,eletype);
  194. { a declock(ref. count) isn't enough here }
  195. { it could be that the in MT enviroments }
  196. { in the mean time the refcount was }
  197. { decremented }
  198. { it is, because it doesn't really matter }
  199. { if the array is now removed }
  200. { fpc_dynarray_decr_ref(p,ti); }
  201. if declocked(realp^.refcount) then
  202. fpc_dynarray_clear_internal(realp,pdynarraytypeinfo(ti));
  203. end
  204. else if dims[dimcount-1]<>realp^.high+1 then
  205. begin
  206. { range checking is quite difficult ... }
  207. { if size overflows then it is less than }
  208. { the values it was calculated from }
  209. if (size<sizeof(tdynarray)) or
  210. ((elesize>0) and (size<elesize)) then
  211. HandleErrorFrame(201,get_frame);
  212. { resize? }
  213. { here, realp^.refcount has to be one, otherwise the previous }
  214. { if-statement would have been taken. Or is this also for MT }
  215. { code? (JM) }
  216. if realp^.refcount=1 then
  217. begin
  218. { shrink the array? }
  219. if dims[dimcount-1]<realp^.high+1 then
  220. begin
  221. int_finalizearray(pointer(realp)+sizeof(tdynarray)+
  222. elesize*dims[dimcount-1],
  223. eletype,realp^.high-dims[dimcount-1]+1,elesize);
  224. reallocmem(realp,size);
  225. end
  226. else if dims[dimcount-1]>realp^.high+1 then
  227. begin
  228. reallocmem(realp,size);
  229. fillchar((pointer(realp)+sizeof(tdynarray)+elesize*(realp^.high+1))^,
  230. (dims[dimcount-1]-realp^.high-1)*elesize,0);
  231. end;
  232. newp := realp;
  233. updatep := true;
  234. end;
  235. end;
  236. end;
  237. { handle nested arrays }
  238. if dimcount>1 then
  239. begin
  240. for i:=0 to dims[dimcount-1]-1 do
  241. int_dynarray_setlength(pointer((pointer(newp)+sizeof(tdynarray)+i*elesize)^),
  242. eletype,dimcount-1,dims);
  243. end;
  244. if updatep then
  245. begin
  246. p:=pointer(newp)+sizeof(tdynarray);
  247. newp^.refcount:=1;
  248. newp^.high:=dims[dimcount-1]-1;
  249. end;
  250. end;
  251. { provide local access to dynarr_copy }
  252. procedure int_dynarray_copy(var pdest : pointer;psrc : pointer;ti : pointer;
  253. lowidx,count:tdynarrayindex);[external name 'FPC_DYNARR_COPY'];
  254. procedure fpc_dynarray_copy(var pdest : pointer;psrc : pointer;ti : pointer;
  255. lowidx,count:tdynarrayindex);[Public,Alias:'FPC_DYNARR_COPY'];{$ifdef hascompilerproc} compilerproc; {$endif}
  256. var
  257. realpdest,
  258. realpsrc : pdynarray;
  259. cnt,
  260. i,size : longint;
  261. highidx : tdynarrayindex;
  262. elesize : sizeint;
  263. eletype : pdynarraytypeinfo;
  264. begin
  265. highidx:=lowidx+count-1;
  266. pdest:=nil;
  267. if psrc=nil then
  268. exit;
  269. realpsrc:=pdynarray(psrc-sizeof(tdynarray));
  270. { skip kind and name }
  271. inc(pointer(ti),ord(pdynarraytypeinfo(ti)^.namelen)+2);
  272. {$ifdef FPC_ALIGNSRTTI}
  273. ti:=aligntoptr(ti);
  274. {$endif FPC_ALIGNSRTTI}
  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) then
  286. HandleErrorFrame(201,get_frame);
  287. cnt:=highidx-lowidx+1;
  288. { create new array }
  289. size:=elesize*cnt;
  290. getmem(realpdest,size+sizeof(tdynarray));
  291. pdest:=pointer(realpdest)+sizeof(tdynarray);
  292. { copy data }
  293. move(pointer(psrc+elesize*lowidx)^,pdest^,size);
  294. { fill new refcount }
  295. realpdest^.refcount:=1;
  296. realpdest^.high:=cnt-1;
  297. { increment ref. count of members }
  298. for i:= 0 to cnt-1 do
  299. int_addref(pointer(pdest+elesize*i),eletype);
  300. end;
  301. {
  302. $Log$
  303. Revision 1.35 2005-01-24 21:32:48 florian
  304. * fixed copy(dyn. array of ansistring)
  305. Revision 1.34 2004/11/07 18:31:50 jonas
  306. * fixed getting pointer to nested dynarray types
  307. Revision 1.33 2004/11/07 18:02:47 jonas
  308. * fixed pointer arithmetic errors
  309. Revision 1.32 2004/11/06 15:29:19 florian
  310. * fixed fpc_dynarry_copy for arm/sparc
  311. Revision 1.31 2004/11/03 10:54:36 florian
  312. * fixed dyn. array handling for 32 bit architectures requiering proper alignment
  313. Revision 1.30 2004/10/24 20:01:41 peter
  314. * saveregisters calling convention is obsolete
  315. Revision 1.29 2004/09/15 07:28:09 michael
  316. + Fix crash when resizing dynamic array
  317. Revision 1.28 2004/05/31 20:25:04 peter
  318. * removed warnings
  319. Revision 1.27 2004/05/31 14:31:57 peter
  320. * remove comment warnings
  321. Revision 1.26 2004/05/24 07:18:17 michael
  322. + Patch from peter to fix crash
  323. Revision 1.25 2004/05/20 15:56:32 florian
  324. * fixed <dyn. array>:=nil;
  325. Revision 1.24 2004/05/02 15:15:58 peter
  326. * use freemem() without size
  327. Revision 1.23 2003/10/29 21:00:34 peter
  328. * fixed a:=copy(b)
  329. Revision 1.22 2003/10/25 22:52:07 florian
  330. * fixed copy(<dynarray>, ...)
  331. Revision 1.21 2002/11/26 23:02:07 peter
  332. * fixed dynarray copy
  333. Revision 1.20 2002/10/09 20:24:30 florian
  334. + range checking for dyn. arrays
  335. Revision 1.19 2002/10/02 18:21:51 peter
  336. * Copy() changed to internal function calling compilerprocs
  337. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  338. new copy functions
  339. Revision 1.18 2002/09/07 15:07:45 peter
  340. * old logs removed and tabs fixed
  341. Revision 1.17 2002/04/26 15:19:05 peter
  342. * use saveregisters for incr routines, saves also problems with
  343. the optimizer
  344. Revision 1.16 2002/04/25 20:14:56 peter
  345. * updated compilerprocs
  346. * incr ref count has now a value argument instead of var
  347. Revision 1.15 2002/01/21 20:16:08 peter
  348. * updated for dynarr:=nil
  349. }