dynarr.inc 11 KB

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