dynarr.inc 12 KB

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