dynarr.inc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. getmem(newp,size);
  115. fillchar(newp^,size,0);
  116. updatep := true;
  117. end
  118. else
  119. begin
  120. realp:=pdynarray(p-sizeof(tdynarray));
  121. if dims[dimcount-1]<0 then
  122. HandleErrorFrame(201,get_frame);
  123. { if the new dimension is 0, we've to release all data }
  124. if dims[dimcount-1]=0 then
  125. begin
  126. dynarray_clear(realp,pdynarraytypeinfo(pti));
  127. p:=nil;
  128. exit;
  129. end;
  130. if realp^.refcount<>1 then
  131. begin
  132. updatep := true;
  133. { make an unique copy }
  134. getmem(newp,size);
  135. fillchar(newp^,size,0);
  136. if realp^.high < dims[dimcount-1] then
  137. movelen := realp^.high+1
  138. else
  139. movelen := dims[dimcount-1];
  140. move(p^,(pointer(newp)+sizeof(tdynarray))^,ti^.elesize*movelen);
  141. { increment ref. count of members }
  142. for i:= 0 to movelen-1 do
  143. int_addref(pointer(newp)+sizeof(tdynarray)+ti^.elesize*i,ti^.eletype);
  144. { a declock(ref. count) isn't enough here }
  145. { it could be that the in MT enviroments }
  146. { in the mean time the refcount was }
  147. { decremented }
  148. { it is, because it doesn't really matter }
  149. { if the array is now removed }
  150. { fpc_dynarray_decr_ref(p,ti); }
  151. if declocked(realp^.refcount) then
  152. dynarray_clear(realp,pdynarraytypeinfo(ti));
  153. end
  154. else if dims[dimcount-1]<>realp^.high+1 then
  155. begin
  156. { range checking is quite difficult ... }
  157. { if size overflows then it is less than }
  158. { the values it was calculated from }
  159. if (size<sizeof(tdynarray)) or
  160. ((ti^.elesize>0) and (size<ti^.elesize)) then
  161. HandleErrorFrame(201,get_frame);
  162. { resize? }
  163. { here, realp^.refcount has to be one, otherwise the previous }
  164. { if-statement would have been taken. Or is this also for MT }
  165. { code? (JM) }
  166. if realp^.refcount=1 then
  167. begin
  168. { shrink the array? }
  169. if dims[dimcount-1]<realp^.high+1 then
  170. begin
  171. int_finalizearray(pointer(realp)+sizeof(tdynarray)+
  172. ti^.elesize*dims[dimcount-1],
  173. ti^.eletype,realp^.high-dims[dimcount-1]+1,ti^.elesize);
  174. reallocmem(realp,size);
  175. end
  176. else if dims[dimcount-1]>realp^.high+1 then
  177. begin
  178. reallocmem(realp,size);
  179. fillchar((pointer(realp)+sizeof(tdynarray)+ti^.elesize*(realp^.high+1))^,
  180. (dims[dimcount-1]-realp^.high-1)*ti^.elesize,0);
  181. end;
  182. newp := realp;
  183. updatep := true;
  184. end;
  185. end;
  186. end;
  187. { handle nested arrays }
  188. if dimcount>1 then
  189. begin
  190. for i:=0 to dims[dimcount-1]-1 do
  191. int_dynarray_setlength(pointer((pointer(newp)+sizeof(tdynarray)+i*ti^.elesize)^),
  192. ti^.eletype,dimcount-1,dims);
  193. end;
  194. if updatep then
  195. begin
  196. p:=pointer(newp)+sizeof(tdynarray);
  197. newp^.refcount:=1;
  198. newp^.high:=dims[dimcount-1]-1;
  199. end;
  200. end;
  201. function fpc_dynarray_copy(var p : pointer;ti : pointer;
  202. dimcount : dword;dims : pdynarrayindex) : pointer;[Public,Alias:'FPC_DYNARRAY_COPY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  203. begin
  204. { note: ti is of type pdynarrayinfo, but it can't be declared }
  205. { that way because this procedure is also declared in the interface }
  206. { (as compilerproc) and the pdynarraytypeinfo isn't available there }
  207. {!!!!!!!!!!}
  208. end;
  209. {
  210. $Log$
  211. Revision 1.12 2001-11-17 16:56:08 florian
  212. * init and final code in genrtti.inc updated
  213. Revision 1.11 2001/09/27 08:59:13 jonas
  214. * fixed bug in dynarr_decr_ref I introduced with my previous fixes
  215. Revision 1.10 2001/09/26 14:07:25 jonas
  216. * fixed several bugs, most related to handling multi-dimensional
  217. dynamical arrays
  218. Revision 1.9 2001/08/19 21:02:01 florian
  219. * fixed and added a lot of stuff to get the Jedi DX8 headers
  220. compiled
  221. Revision 1.8 2001/08/01 15:00:10 jonas
  222. + "compproc" helpers
  223. * renamed several helpers so that their name is the same as their
  224. "public alias", which should facilitate the conversion of processor
  225. specific code in the code generator to processor independent code
  226. * some small fixes to the val_ansistring and val_widestring helpers
  227. (always immediately exit if the source string is longer than 255
  228. chars)
  229. * fixed fpc_dynarray_high and fpc_dynarray_length if the dynarray is
  230. still nil (used to crash, now return resp -1 and 0)
  231. Revision 1.7 2001/05/27 14:28:44 florian
  232. + made the ref. couting MT safe
  233. Revision 1.6 2001/04/13 23:49:48 peter
  234. * fixes for the stricter compiler
  235. Revision 1.5 2000/12/01 23:30:00 florian
  236. * fixed some bugs in setlength
  237. Revision 1.4 2000/11/12 23:23:34 florian
  238. * interfaces basically running
  239. Revision 1.3 2000/11/07 23:42:21 florian
  240. + AfterConstruction and BeforeDestruction implemented
  241. + TInterfacedObject implemented
  242. Revision 1.2 2000/11/06 21:35:59 peter
  243. * removed some warnings
  244. Revision 1.1 2000/11/04 17:52:46 florian
  245. * fixed linker errors
  246. }