dynarr.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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(p,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;ti : 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. i : tdynarrayindex;
  96. size : t_size;
  97. { contains the "fixed" pointers where the refcount }
  98. { and high are at positive offsets }
  99. realp,newp : pdynarray;
  100. ti : pdynarraytypeinfo;
  101. begin
  102. ti:=pdynarraytypeinfo(pti);
  103. { skip kind and name }
  104. inc(pointer(ti),ord(ti^.namelen));
  105. { determine new memory size }
  106. size:=ti^.elesize*dims[0]+sizeof(tdynarray);
  107. { not assigned yet? }
  108. if not(assigned(p)) then
  109. begin
  110. getmem(newp,size);
  111. fillchar(newp^,size,0);
  112. end
  113. else
  114. begin
  115. realp:=pdynarray(p-sizeof(tdynarray));
  116. if dims[0]<0 then
  117. HandleErrorFrame(201,get_frame);
  118. { if the new dimension is 0, we've to release all data }
  119. if dims[0]=0 then
  120. begin
  121. dynarray_clear(realp,pdynarraytypeinfo(pti));
  122. p:=nil;
  123. exit;
  124. end;
  125. if realp^.refcount<>1 then
  126. begin
  127. { make an unique copy }
  128. getmem(newp,size);
  129. move(p^,(pointer(newp)+sizeof(tdynarray))^,ti^.elesize*dims[0]);
  130. { increment ref. count of members }
  131. for i:=0 to dims[0]-1 do
  132. int_addref(pointer(newp)+sizeof(tdynarray)+ti^.elesize*i,ti^.eletype);
  133. { a declock(ref. count) isn't enough here }
  134. { it could be that the in MT enviroments }
  135. { in the mean time the refcount was }
  136. { decremented }
  137. { it is, because it doesn't really matter }
  138. { if the array is now removed }
  139. fpc_dynarray_decr_ref(p,ti);
  140. end
  141. else if dims[0]<>realp^.high+1 then
  142. begin
  143. { range checking is quite difficult ... }
  144. { if size overflows then it is less than }
  145. { the values it was calculated from }
  146. if (size<sizeof(tdynarray)) or
  147. ((ti^.elesize>0) and (size<ti^.elesize)) then
  148. HandleErrorFrame(201,get_frame);
  149. { resize? }
  150. if realp^.refcount=1 then
  151. begin
  152. { shrink the array? }
  153. if dims[0]<realp^.high+1 then
  154. begin
  155. int_finalizearray(pointer(realp)+sizeof(tdynarray)+ti^.elesize*dims[0],
  156. ti^.eletype,realp^.high-dims[0]+1,ti^.elesize);
  157. reallocmem(realp,size);
  158. end
  159. else if dims[0]>realp^.high+1 then
  160. begin
  161. reallocmem(realp,size);
  162. fillchar((pointer(realp)+sizeof(tdynarray)+ti^.elesize*(realp^.high+1))^,
  163. (dims[0]-realp^.high-1)*ti^.elesize,0);
  164. end;
  165. end;
  166. end
  167. else
  168. newp:=realp;
  169. { handle nested arrays }
  170. if dimcount>1 then
  171. begin
  172. for i:=0 to dims[0]-1 do
  173. int_dynarray_setlength(pointer(plongint(pointer(newp)+sizeof(tdynarray))[i*ti^.elesize]),
  174. ti^.eletype,dimcount-1,@dims[1]);
  175. end;
  176. end;
  177. p:=pointer(newp)+sizeof(tdynarray);
  178. newp^.refcount:=1;
  179. newp^.high:=dims[0]-1;
  180. end;
  181. function fpc_dynarray_copy(var p : pointer;ti : pointer;
  182. dimcount : dword;dims : pdynarrayindex) : pointer;[Public,Alias:'FPC_DYNARRAY_COPY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  183. begin
  184. { note: ti is of type pdynarrayinfo, but it can't be declared }
  185. { that way because this procedure is also declared in the interface }
  186. { (as compilerproc) and the pdynarraytypeinfo isn't available there }
  187. {!!!!!!!!!!}
  188. end;
  189. {
  190. $Log$
  191. Revision 1.9 2001-08-19 21:02:01 florian
  192. * fixed and added a lot of stuff to get the Jedi DX( headers
  193. compiled
  194. Revision 1.8 2001/08/01 15:00:10 jonas
  195. + "compproc" helpers
  196. * renamed several helpers so that their name is the same as their
  197. "public alias", which should facilitate the conversion of processor
  198. specific code in the code generator to processor independent code
  199. * some small fixes to the val_ansistring and val_widestring helpers
  200. (always immediately exit if the source string is longer than 255
  201. chars)
  202. * fixed fpc_dynarray_high and fpc_dynarray_length if the dynarray is
  203. still nil (used to crash, now return resp -1 and 0)
  204. Revision 1.7 2001/05/27 14:28:44 florian
  205. + made the ref. couting MT safe
  206. Revision 1.6 2001/04/13 23:49:48 peter
  207. * fixes for the stricter compiler
  208. Revision 1.5 2000/12/01 23:30:00 florian
  209. * fixed some bugs in setlength
  210. Revision 1.4 2000/11/12 23:23:34 florian
  211. * interfaces basically running
  212. Revision 1.3 2000/11/07 23:42:21 florian
  213. + AfterConstruction and BeforeDestruction implemented
  214. + TInterfacedObject implemented
  215. Revision 1.2 2000/11/06 21:35:59 peter
  216. * removed some warnings
  217. Revision 1.1 2000/11/04 17:52:46 florian
  218. * fixed linker errors
  219. }