dynarr.inc 13 KB

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