dynarr.inc 8.6 KB

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