dynarr.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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_length(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  31. begin
  32. if assigned(p) then
  33. fpc_dynarray_length:=pdynarray(p-sizeof(tdynarray))^.high+1
  34. else
  35. fpc_dynarray_length:=0;
  36. end;
  37. function fpc_dynarray_high(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_HIGH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  38. begin
  39. if assigned(p) then
  40. fpc_dynarray_high:=pdynarray(p-sizeof(tdynarray))^.high
  41. else
  42. fpc_dynarray_high:=-1;
  43. end;
  44. { releases and finalizes the data of a dyn. array and sets p to nil }
  45. procedure dynarray_clear(var p : pdynarray;ti : pdynarraytypeinfo);
  46. begin
  47. { skip kind and name }
  48. inc(pointer(ti),ord(ti^.namelen));
  49. { finalize all data }
  50. int_finalizearray(pointer(p)+sizeof(tdynarray),ti^.eletype,p^.high+1,ti^.elesize);
  51. { release the data }
  52. freemem(p,sizeof(tdynarray)+(p^.high+1)*ti^.elesize);
  53. p:=nil;
  54. end;
  55. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer);[Public,Alias:'FPC_DYNARRAY_DECR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  56. var
  57. realp : pdynarray;
  58. begin
  59. if p=nil then
  60. exit;
  61. realp:=pdynarray(p-sizeof(tdynarray));
  62. if realp^.refcount=0 then
  63. HandleErrorFrame(204,get_frame);
  64. { decr. ref. count }
  65. { should we remove the array? }
  66. if declocked(realp^.refcount) then
  67. dynarray_clear(realp,pdynarraytypeinfo(ti));
  68. p := nil;
  69. end;
  70. {$ifdef hascompilerproc}
  71. { provide local access to dynarr_decr_ref for dynarr_setlength }
  72. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer); [external name 'FPC_DYNARRAY_DECR_REF'];
  73. {$endif}
  74. procedure fpc_dynarray_incr_ref(var p : pointer);[Public,Alias:'FPC_DYNARRAY_INCR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  75. var
  76. realp : pdynarray;
  77. begin
  78. if p=nil then
  79. exit;
  80. realp:=pdynarray(p-sizeof(tdynarray));
  81. if realp^.refcount=0 then
  82. HandleErrorFrame(204,get_frame);
  83. inclocked(realp^.refcount);
  84. end;
  85. {$ifdef hascompilerproc}
  86. { provide local access to dynarr_decr_ref for dynarr_setlength }
  87. procedure fpc_dynarray_incr_ref(var p : pointer); [external name 'FPC_DYNARRAY_INCR_REF'];
  88. {$endif}
  89. { provide local access to dynarr_setlength }
  90. procedure int_dynarray_setlength(var p : pointer;pti : pointer;
  91. dimcount : dword;dims : pdynarrayindex);[external name 'FPC_DYNARR_SETLENGTH'];
  92. procedure fpc_dynarray_setlength(var p : pointer;pti : pointer;
  93. dimcount : dword;dims : pdynarrayindex);[Public,Alias:'FPC_DYNARR_SETLENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  94. var
  95. movelen: cardinal;
  96. i : tdynarrayindex;
  97. size : t_size;
  98. { contains the "fixed" pointers where the refcount }
  99. { and high are at positive offsets }
  100. realp,newp : pdynarray;
  101. ti : pdynarraytypeinfo;
  102. updatep: boolean;
  103. begin
  104. ti:=pdynarraytypeinfo(pti);
  105. { skip kind and name }
  106. inc(pointer(ti),ord(ti^.namelen));
  107. { determine new memory size }
  108. { dims[dimcount-1] because the dimensions are in reverse order! (JM) }
  109. size:=ti^.elesize*dims[dimcount-1]+sizeof(tdynarray);
  110. updatep := false;
  111. { not assigned yet? }
  112. if not(assigned(p)) then
  113. begin
  114. { do we have to allocate memory? }
  115. if dims[dimcount-1] = 0 then
  116. exit;
  117. getmem(newp,size);
  118. fillchar(newp^,size,0);
  119. updatep := true;
  120. end
  121. else
  122. begin
  123. realp:=pdynarray(p-sizeof(tdynarray));
  124. if dims[dimcount-1]<0 then
  125. HandleErrorFrame(201,get_frame);
  126. { if the new dimension is 0, we've to release all data }
  127. if dims[dimcount-1]=0 then
  128. begin
  129. dynarray_clear(realp,pdynarraytypeinfo(pti));
  130. p:=nil;
  131. exit;
  132. end;
  133. if realp^.refcount<>1 then
  134. begin
  135. updatep := true;
  136. { make an unique copy }
  137. getmem(newp,size);
  138. fillchar(newp^,size,0);
  139. if realp^.high < dims[dimcount-1] then
  140. movelen := realp^.high+1
  141. else
  142. movelen := dims[dimcount-1];
  143. move(p^,(pointer(newp)+sizeof(tdynarray))^,ti^.elesize*movelen);
  144. { increment ref. count of members }
  145. for i:= 0 to movelen-1 do
  146. int_addref(pointer(newp)+sizeof(tdynarray)+ti^.elesize*i,ti^.eletype);
  147. { a declock(ref. count) isn't enough here }
  148. { it could be that the in MT enviroments }
  149. { in the mean time the refcount was }
  150. { decremented }
  151. { it is, because it doesn't really matter }
  152. { if the array is now removed }
  153. { fpc_dynarray_decr_ref(p,ti); }
  154. if declocked(realp^.refcount) then
  155. dynarray_clear(realp,pdynarraytypeinfo(ti));
  156. end
  157. else if dims[dimcount-1]<>realp^.high+1 then
  158. begin
  159. { range checking is quite difficult ... }
  160. { if size overflows then it is less than }
  161. { the values it was calculated from }
  162. if (size<sizeof(tdynarray)) or
  163. ((ti^.elesize>0) and (size<ti^.elesize)) then
  164. HandleErrorFrame(201,get_frame);
  165. { resize? }
  166. { here, realp^.refcount has to be one, otherwise the previous }
  167. { if-statement would have been taken. Or is this also for MT }
  168. { code? (JM) }
  169. if realp^.refcount=1 then
  170. begin
  171. { shrink the array? }
  172. if dims[dimcount-1]<realp^.high+1 then
  173. begin
  174. int_finalizearray(pointer(realp)+sizeof(tdynarray)+
  175. ti^.elesize*dims[dimcount-1],
  176. ti^.eletype,realp^.high-dims[dimcount-1]+1,ti^.elesize);
  177. reallocmem(realp,size);
  178. end
  179. else if dims[dimcount-1]>realp^.high+1 then
  180. begin
  181. reallocmem(realp,size);
  182. fillchar((pointer(realp)+sizeof(tdynarray)+ti^.elesize*(realp^.high+1))^,
  183. (dims[dimcount-1]-realp^.high-1)*ti^.elesize,0);
  184. end;
  185. newp := realp;
  186. updatep := true;
  187. end;
  188. end;
  189. end;
  190. { handle nested arrays }
  191. if dimcount>1 then
  192. begin
  193. for i:=0 to dims[dimcount-1]-1 do
  194. int_dynarray_setlength(pointer((pointer(newp)+sizeof(tdynarray)+i*ti^.elesize)^),
  195. ti^.eletype,dimcount-1,dims);
  196. end;
  197. if updatep then
  198. begin
  199. p:=pointer(newp)+sizeof(tdynarray);
  200. newp^.refcount:=1;
  201. newp^.high:=dims[dimcount-1]-1;
  202. end;
  203. end;
  204. function fpc_dynarray_copy(var p : pointer;ti : pointer;
  205. dimcount : dword;dims : pdynarrayindex) : pointer;[Public,Alias:'FPC_DYNARRAY_COPY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  206. begin
  207. { note: ti is of type pdynarrayinfo, but it can't be declared }
  208. { that way because this procedure is also declared in the interface }
  209. { (as compilerproc) and the pdynarraytypeinfo isn't available there }
  210. {!!!!!!!!!!}
  211. end;
  212. {
  213. $Log$
  214. Revision 1.14 2001-12-29 15:51:11 jonas
  215. * correctly check for 0-size dynarray in previous patch
  216. Revision 1.13 2001/12/28 14:19:07 jonas
  217. * don't allocate memory when doing a setlength(dynarr,0) when dynarr is
  218. already nil
  219. Revision 1.12 2001/11/17 16:56:08 florian
  220. * init and final code in genrtti.inc updated
  221. Revision 1.11 2001/09/27 08:59:13 jonas
  222. * fixed bug in dynarr_decr_ref I introduced with my previous fixes
  223. Revision 1.10 2001/09/26 14:07:25 jonas
  224. * fixed several bugs, most related to handling multi-dimensional
  225. dynamical arrays
  226. Revision 1.9 2001/08/19 21:02:01 florian
  227. * fixed and added a lot of stuff to get the Jedi DX8 headers
  228. compiled
  229. Revision 1.8 2001/08/01 15:00:10 jonas
  230. + "compproc" helpers
  231. * renamed several helpers so that their name is the same as their
  232. "public alias", which should facilitate the conversion of processor
  233. specific code in the code generator to processor independent code
  234. * some small fixes to the val_ansistring and val_widestring helpers
  235. (always immediately exit if the source string is longer than 255
  236. chars)
  237. * fixed fpc_dynarray_high and fpc_dynarray_length if the dynarray is
  238. still nil (used to crash, now return resp -1 and 0)
  239. Revision 1.7 2001/05/27 14:28:44 florian
  240. + made the ref. couting MT safe
  241. Revision 1.6 2001/04/13 23:49:48 peter
  242. * fixes for the stricter compiler
  243. Revision 1.5 2000/12/01 23:30:00 florian
  244. * fixed some bugs in setlength
  245. Revision 1.4 2000/11/12 23:23:34 florian
  246. * interfaces basically running
  247. Revision 1.3 2000/11/07 23:42:21 florian
  248. + AfterConstruction and BeforeDestruction implemented
  249. + TInterfacedObject implemented
  250. Revision 1.2 2000/11/06 21:35:59 peter
  251. * removed some warnings
  252. Revision 1.1 2000/11/04 17:52:46 florian
  253. * fixed linker errors
  254. }