dynarr.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 fpc_dynarray_clear(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_CLEAR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  46. begin
  47. if p=nil then
  48. exit;
  49. { skip kind and name }
  50. inc(pointer(ti),ord(pdynarraytypeinfo(ti)^.namelen));
  51. { finalize all data }
  52. int_finalizearray(p+sizeof(tdynarray),pdynarraytypeinfo(ti)^.eletype,pdynarray(p)^.high+1,
  53. pdynarraytypeinfo(ti)^.elesize);
  54. { release the data }
  55. freemem(p,sizeof(tdynarray)+(pdynarray(p)^.high+1)*pdynarraytypeinfo(ti)^.elesize);
  56. p:=nil;
  57. end;
  58. {$ifdef hascompilerproc}
  59. { alias for internal use }
  60. Procedure fpc_dynarray_clear (var p : pointer;ti : pointer);[external name 'FPC_DYNARRAY_CLEAR'];
  61. {$endif hascompilerproc}
  62. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer);saveregisters;[Public,Alias:'FPC_DYNARRAY_DECR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  63. var
  64. realp : pdynarray;
  65. begin
  66. if p=nil then
  67. exit;
  68. realp:=pdynarray(p-sizeof(tdynarray));
  69. if realp^.refcount=0 then
  70. HandleErrorFrame(204,get_frame);
  71. { decr. ref. count }
  72. { should we remove the array? }
  73. if declocked(realp^.refcount) then
  74. fpc_dynarray_clear(realp,pdynarraytypeinfo(ti));
  75. p := nil;
  76. end;
  77. {$ifdef hascompilerproc}
  78. { provide local access to dynarr_decr_ref for dynarr_setlength }
  79. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer);saveregisters; [external name 'FPC_DYNARRAY_DECR_REF'];
  80. {$endif}
  81. procedure fpc_dynarray_incr_ref(p : pointer);saveregisters;[Public,Alias:'FPC_DYNARRAY_INCR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  82. var
  83. realp : pdynarray;
  84. begin
  85. if p=nil then
  86. exit;
  87. realp:=pdynarray(p-sizeof(tdynarray));
  88. if realp^.refcount=0 then
  89. HandleErrorFrame(204,get_frame);
  90. inclocked(realp^.refcount);
  91. end;
  92. {$ifdef hascompilerproc}
  93. { provide local access to dynarr_decr_ref for dynarr_setlength }
  94. procedure fpc_dynarray_incr_ref(p : pointer);saveregisters; [external name 'FPC_DYNARRAY_INCR_REF'];
  95. {$endif}
  96. { provide local access to dynarr_setlength }
  97. procedure int_dynarray_setlength(var p : pointer;pti : pointer;
  98. dimcount : dword;dims : pdynarrayindex);[external name 'FPC_DYNARR_SETLENGTH'];
  99. procedure fpc_dynarray_setlength(var p : pointer;pti : pointer;
  100. dimcount : dword;dims : pdynarrayindex);[Public,Alias:'FPC_DYNARR_SETLENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  101. var
  102. movelen: cardinal;
  103. i : tdynarrayindex;
  104. size : t_size;
  105. { contains the "fixed" pointers where the refcount }
  106. { and high are at positive offsets }
  107. realp,newp : pdynarray;
  108. ti : pdynarraytypeinfo;
  109. updatep: boolean;
  110. begin
  111. ti:=pdynarraytypeinfo(pti);
  112. { skip kind and name }
  113. inc(pointer(ti),ord(ti^.namelen));
  114. { determine new memory size }
  115. { dims[dimcount-1] because the dimensions are in reverse order! (JM) }
  116. size:=ti^.elesize*dims[dimcount-1]+sizeof(tdynarray);
  117. updatep := false;
  118. { not assigned yet? }
  119. if not(assigned(p)) then
  120. begin
  121. { do we have to allocate memory? }
  122. if dims[dimcount-1] = 0 then
  123. exit;
  124. getmem(newp,size);
  125. fillchar(newp^,size,0);
  126. updatep := true;
  127. end
  128. else
  129. begin
  130. realp:=pdynarray(p-sizeof(tdynarray));
  131. if dims[dimcount-1]<0 then
  132. HandleErrorFrame(201,get_frame);
  133. { if the new dimension is 0, we've to release all data }
  134. if dims[dimcount-1]=0 then
  135. begin
  136. fpc_dynarray_clear(realp,pdynarraytypeinfo(pti));
  137. p:=nil;
  138. exit;
  139. end;
  140. if realp^.refcount<>1 then
  141. begin
  142. updatep := true;
  143. { make an unique copy }
  144. getmem(newp,size);
  145. fillchar(newp^,size,0);
  146. if realp^.high < dims[dimcount-1] then
  147. movelen := realp^.high+1
  148. else
  149. movelen := dims[dimcount-1];
  150. move(p^,(pointer(newp)+sizeof(tdynarray))^,ti^.elesize*movelen);
  151. { increment ref. count of members }
  152. for i:= 0 to movelen-1 do
  153. int_addref(pointer(newp)+sizeof(tdynarray)+ti^.elesize*i,ti^.eletype);
  154. { a declock(ref. count) isn't enough here }
  155. { it could be that the in MT enviroments }
  156. { in the mean time the refcount was }
  157. { decremented }
  158. { it is, because it doesn't really matter }
  159. { if the array is now removed }
  160. { fpc_dynarray_decr_ref(p,ti); }
  161. if declocked(realp^.refcount) then
  162. fpc_dynarray_clear(realp,pdynarraytypeinfo(ti));
  163. end
  164. else if dims[dimcount-1]<>realp^.high+1 then
  165. begin
  166. { range checking is quite difficult ... }
  167. { if size overflows then it is less than }
  168. { the values it was calculated from }
  169. if (size<sizeof(tdynarray)) or
  170. ((ti^.elesize>0) and (size<ti^.elesize)) then
  171. HandleErrorFrame(201,get_frame);
  172. { resize? }
  173. { here, realp^.refcount has to be one, otherwise the previous }
  174. { if-statement would have been taken. Or is this also for MT }
  175. { code? (JM) }
  176. if realp^.refcount=1 then
  177. begin
  178. { shrink the array? }
  179. if dims[dimcount-1]<realp^.high+1 then
  180. begin
  181. int_finalizearray(pointer(realp)+sizeof(tdynarray)+
  182. ti^.elesize*dims[dimcount-1],
  183. ti^.eletype,realp^.high-dims[dimcount-1]+1,ti^.elesize);
  184. reallocmem(realp,size);
  185. end
  186. else if dims[dimcount-1]>realp^.high+1 then
  187. begin
  188. reallocmem(realp,size);
  189. fillchar((pointer(realp)+sizeof(tdynarray)+ti^.elesize*(realp^.high+1))^,
  190. (dims[dimcount-1]-realp^.high-1)*ti^.elesize,0);
  191. end;
  192. newp := realp;
  193. updatep := true;
  194. end;
  195. end;
  196. end;
  197. { handle nested arrays }
  198. if dimcount>1 then
  199. begin
  200. for i:=0 to dims[dimcount-1]-1 do
  201. int_dynarray_setlength(pointer((pointer(newp)+sizeof(tdynarray)+i*ti^.elesize)^),
  202. ti^.eletype,dimcount-1,dims);
  203. end;
  204. if updatep then
  205. begin
  206. p:=pointer(newp)+sizeof(tdynarray);
  207. newp^.refcount:=1;
  208. newp^.high:=dims[dimcount-1]-1;
  209. end;
  210. end;
  211. function fpc_dynarray_copy(var p : pointer;ti : pointer;
  212. dimcount : dword;dims : pdynarrayindex) : pointer;[Public,Alias:'FPC_DYNARRAY_COPY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  213. begin
  214. { note: ti is of type pdynarrayinfo, but it can't be declared }
  215. { that way because this procedure is also declared in the interface }
  216. { (as compilerproc) and the pdynarraytypeinfo isn't available there }
  217. {!!!!!!!!!!}
  218. end;
  219. {
  220. $Log$
  221. Revision 1.18 2002-09-07 15:07:45 peter
  222. * old logs removed and tabs fixed
  223. Revision 1.17 2002/04/26 15:19:05 peter
  224. * use saveregisters for incr routines, saves also problems with
  225. the optimizer
  226. Revision 1.16 2002/04/25 20:14:56 peter
  227. * updated compilerprocs
  228. * incr ref count has now a value argument instead of var
  229. Revision 1.15 2002/01/21 20:16:08 peter
  230. * updated for dynarr:=nil
  231. }