dynarr.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 : sizeint;
  28. eletype : pdynarraytypeinfo;
  29. end;
  30. procedure fpc_dynarray_rangecheck(p : pointer;i : tdynarrayindex);[Public,Alias:'FPC_DYNARRAY_RANGECHECK']; {$ifdef hascompilerproc} compilerproc; {$endif}
  31. begin
  32. if not(assigned(p)) or (i<0) or (i>pdynarray(p-sizeof(tdynarray))^.high) then
  33. HandleErrorFrame(201,get_frame);
  34. end;
  35. function fpc_dynarray_length(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  36. begin
  37. if assigned(p) then
  38. fpc_dynarray_length:=pdynarray(p-sizeof(tdynarray))^.high+1
  39. else
  40. fpc_dynarray_length:=0;
  41. end;
  42. function fpc_dynarray_high(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_HIGH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  43. begin
  44. if assigned(p) then
  45. fpc_dynarray_high:=pdynarray(p-sizeof(tdynarray))^.high
  46. else
  47. fpc_dynarray_high:=-1;
  48. end;
  49. { releases and finalizes the data of a dyn. array and sets p to nil }
  50. procedure fpc_dynarray_clear_internal(p : pointer;ti : pointer);
  51. var
  52. elesize : sizeint;
  53. eletype : pdynarraytypeinfo;
  54. begin
  55. if p=nil then
  56. exit;
  57. { skip kind and name }
  58. inc(pointer(ti),ord(pdynarraytypeinfo(ti)^.namelen));
  59. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  60. move(pdynarraytypeinfo(ti)^.elesize,elesize,sizeof(elesize));
  61. move(pdynarraytypeinfo(ti)^.eletype,eletype,sizeof(eletype));
  62. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  63. elesize:=pdynarraytypeinfo(ti)^.elesize;
  64. eletype:=pdynarraytypeinfo(ti)^.eletype;
  65. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  66. { finalize all data }
  67. int_finalizearray(p+sizeof(tdynarray),eletype,pdynarray(p)^.high+1,
  68. elesize);
  69. { release the data }
  70. freemem(p);
  71. end;
  72. procedure fpc_dynarray_clear(var p : pointer;ti : pointer); [Public,Alias:'FPC_DYNARRAY_CLEAR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  73. begin
  74. if (P=Nil) then
  75. exit;
  76. fpc_dynarray_clear_internal(p-sizeof(tdynarray),ti);
  77. p:=nil;
  78. end;
  79. {$ifdef hascompilerproc}
  80. { alias for internal use }
  81. Procedure fpc_dynarray_clear (var p : pointer;ti : pointer);[external name 'FPC_DYNARRAY_CLEAR'];
  82. {$endif hascompilerproc}
  83. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer);saveregisters;[Public,Alias:'FPC_DYNARRAY_DECR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  84. var
  85. realp : pdynarray;
  86. begin
  87. if p=nil then
  88. exit;
  89. realp:=pdynarray(p-sizeof(tdynarray));
  90. if realp^.refcount=0 then
  91. HandleErrorFrame(204,get_frame);
  92. { decr. ref. count }
  93. { should we remove the array? }
  94. if declocked(realp^.refcount) then
  95. fpc_dynarray_clear_internal(realp,pdynarraytypeinfo(ti));
  96. p := nil;
  97. end;
  98. {$ifdef hascompilerproc}
  99. { provide local access to dynarr_decr_ref for dynarr_setlength }
  100. procedure fpc_dynarray_decr_ref(var p : pointer;ti : pointer);saveregisters; [external name 'FPC_DYNARRAY_DECR_REF'];
  101. {$endif}
  102. procedure fpc_dynarray_incr_ref(p : pointer);saveregisters;[Public,Alias:'FPC_DYNARRAY_INCR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  103. var
  104. realp : pdynarray;
  105. begin
  106. if p=nil then
  107. exit;
  108. realp:=pdynarray(p-sizeof(tdynarray));
  109. if realp^.refcount=0 then
  110. HandleErrorFrame(204,get_frame);
  111. inclocked(realp^.refcount);
  112. end;
  113. {$ifdef hascompilerproc}
  114. { provide local access to dynarr_decr_ref for dynarr_setlength }
  115. procedure fpc_dynarray_incr_ref(p : pointer);saveregisters; [external name 'FPC_DYNARRAY_INCR_REF'];
  116. {$endif}
  117. { provide local access to dynarr_setlength }
  118. procedure int_dynarray_setlength(var p : pointer;pti : pointer;
  119. dimcount : dword;dims : pdynarrayindex);[external name 'FPC_DYNARR_SETLENGTH'];
  120. procedure fpc_dynarray_setlength(var p : pointer;pti : pointer;
  121. dimcount : dword;dims : pdynarrayindex);[Public,Alias:'FPC_DYNARR_SETLENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  122. var
  123. i : tdynarrayindex;
  124. movelen,
  125. size : sizeint;
  126. { contains the "fixed" pointers where the refcount }
  127. { and high are at positive offsets }
  128. realp,newp : pdynarray;
  129. ti : pdynarraytypeinfo;
  130. updatep: boolean;
  131. elesize : sizeint;
  132. eletype : pdynarraytypeinfo;
  133. begin
  134. ti:=pdynarraytypeinfo(pti);
  135. { skip kind and name }
  136. inc(pointer(ti),ord(ti^.namelen));
  137. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  138. move(pdynarraytypeinfo(ti)^.elesize,elesize,sizeof(elesize));
  139. move(pdynarraytypeinfo(ti)^.eletype,eletype,sizeof(eletype));
  140. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  141. elesize:=pdynarraytypeinfo(ti)^.elesize;
  142. eletype:=pdynarraytypeinfo(ti)^.eletype;
  143. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  144. { determine new memory size }
  145. { dims[dimcount-1] because the dimensions are in reverse order! (JM) }
  146. size:=elesize*dims[dimcount-1]+sizeof(tdynarray);
  147. updatep := false;
  148. { not assigned yet? }
  149. if not(assigned(p)) then
  150. begin
  151. { do we have to allocate memory? }
  152. if dims[dimcount-1] = 0 then
  153. exit;
  154. getmem(newp,size);
  155. fillchar(newp^,size,0);
  156. updatep := true;
  157. end
  158. else
  159. begin
  160. realp:=pdynarray(p-sizeof(tdynarray));
  161. if dims[dimcount-1]<0 then
  162. HandleErrorFrame(201,get_frame);
  163. { if the new dimension is 0, we've to release all data }
  164. if dims[dimcount-1]=0 then
  165. begin
  166. fpc_dynarray_clear_internal(realp,pdynarraytypeinfo(pti));
  167. p:=nil;
  168. exit;
  169. end;
  170. if realp^.refcount<>1 then
  171. begin
  172. updatep := true;
  173. { make an unique copy }
  174. getmem(newp,size);
  175. fillchar(newp^,size,0);
  176. if realp^.high < dims[dimcount-1] then
  177. movelen := realp^.high+1
  178. else
  179. movelen := dims[dimcount-1];
  180. move(p^,(pointer(newp)+sizeof(tdynarray))^,elesize*movelen);
  181. { increment ref. count of members }
  182. for i:= 0 to movelen-1 do
  183. int_addref(pointer(newp)+sizeof(tdynarray)+elesize*i,eletype);
  184. { a declock(ref. count) isn't enough here }
  185. { it could be that the in MT enviroments }
  186. { in the mean time the refcount was }
  187. { decremented }
  188. { it is, because it doesn't really matter }
  189. { if the array is now removed }
  190. { fpc_dynarray_decr_ref(p,ti); }
  191. if declocked(realp^.refcount) then
  192. fpc_dynarray_clear_internal(realp,pdynarraytypeinfo(ti));
  193. end
  194. else if dims[dimcount-1]<>realp^.high+1 then
  195. begin
  196. { range checking is quite difficult ... }
  197. { if size overflows then it is less than }
  198. { the values it was calculated from }
  199. if (size<sizeof(tdynarray)) or
  200. ((elesize>0) and (size<elesize)) then
  201. HandleErrorFrame(201,get_frame);
  202. { resize? }
  203. { here, realp^.refcount has to be one, otherwise the previous }
  204. { if-statement would have been taken. Or is this also for MT }
  205. { code? (JM) }
  206. if realp^.refcount=1 then
  207. begin
  208. { shrink the array? }
  209. if dims[dimcount-1]<realp^.high+1 then
  210. begin
  211. int_finalizearray(pointer(realp)+sizeof(tdynarray)+
  212. elesize*dims[dimcount-1],
  213. eletype,realp^.high-dims[dimcount-1]+1,elesize);
  214. reallocmem(realp,size);
  215. end
  216. else if dims[dimcount-1]>realp^.high+1 then
  217. begin
  218. reallocmem(realp,size);
  219. fillchar((pointer(realp)+sizeof(tdynarray)+elesize*(realp^.high+1))^,
  220. (dims[dimcount-1]-realp^.high-1)*elesize,0);
  221. end;
  222. newp := realp;
  223. updatep := true;
  224. end;
  225. end;
  226. end;
  227. { handle nested arrays }
  228. if dimcount>1 then
  229. begin
  230. for i:=0 to dims[dimcount-1]-1 do
  231. int_dynarray_setlength(pointer((pointer(newp)+sizeof(tdynarray)+i*elesize)^),
  232. eletype,dimcount-1,dims);
  233. end;
  234. if updatep then
  235. begin
  236. p:=pointer(newp)+sizeof(tdynarray);
  237. newp^.refcount:=1;
  238. newp^.high:=dims[dimcount-1]-1;
  239. end;
  240. end;
  241. { provide local access to dynarr_copy }
  242. procedure int_dynarray_copy(var pdest : pointer;psrc : pointer;ti : pointer;
  243. lowidx,count:tdynarrayindex);[external name 'FPC_DYNARR_COPY'];
  244. procedure fpc_dynarray_copy(var pdest : pointer;psrc : pointer;ti : pointer;
  245. lowidx,count:tdynarrayindex);[Public,Alias:'FPC_DYNARR_COPY'];{$ifdef hascompilerproc} compilerproc; {$endif}
  246. var
  247. realpdest,
  248. realpsrc : pdynarray;
  249. cnt,
  250. i,size : longint;
  251. highidx : tdynarrayindex;
  252. elesize : sizeint;
  253. eletype : pdynarraytypeinfo;
  254. begin
  255. highidx:=lowidx+count-1;
  256. pdest:=nil;
  257. if psrc=nil then
  258. exit;
  259. realpsrc:=pdynarray(psrc-sizeof(tdynarray));
  260. { skip kind and name }
  261. inc(pointer(ti),ord(pdynarraytypeinfo(ti)^.namelen));
  262. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  263. move(pdynarraytypeinfo(ti)^.elesize,elesize,sizeof(elesize));
  264. move(pdynarraytypeinfo(ti)^.eletype,eletype,sizeof(eletype));
  265. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  266. elesize:=pdynarraytypeinfo(ti)^.elesize;
  267. eletype:=pdynarraytypeinfo(ti)^.eletype;
  268. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  269. { -1, -1 (highidx=lowidx-1-1=-3) is used to copy the whole array like a:=copy(b);, so
  270. update the lowidx and highidx with the values from psrc }
  271. if (lowidx=-1) and (highidx=-3) then
  272. begin
  273. lowidx:=0;
  274. highidx:=realpsrc^.high;
  275. end;
  276. { get number of elements and check for invalid values }
  277. if (lowidx<0) or (highidx<0) then
  278. HandleErrorFrame(201,get_frame);
  279. cnt:=highidx-lowidx+1;
  280. { create new array }
  281. size:=elesize*cnt;
  282. getmem(realpdest,size+sizeof(tdynarray));
  283. pdest:=pointer(realpdest)+sizeof(tdynarray);
  284. { copy data }
  285. move(pointer(psrc+elesize*lowidx)^,pdest^,size);
  286. { fill new refcount }
  287. realpdest^.refcount:=1;
  288. realpdest^.high:=cnt-1;
  289. { increment ref. count of members }
  290. for i:= 0 to cnt-1 do
  291. int_addref(pointer(pdest+sizeof(tdynarray)+elesize*i),eletype);
  292. end;
  293. {
  294. $Log$
  295. Revision 1.28 2004-05-31 20:25:04 peter
  296. * removed warnings
  297. Revision 1.27 2004/05/31 14:31:57 peter
  298. * remove comment warnings
  299. Revision 1.26 2004/05/24 07:18:17 michael
  300. + Patch from peter to fix crash
  301. Revision 1.25 2004/05/20 15:56:32 florian
  302. * fixed <dyn. array>:=nil;
  303. Revision 1.24 2004/05/02 15:15:58 peter
  304. * use freemem() without size
  305. Revision 1.23 2003/10/29 21:00:34 peter
  306. * fixed a:=copy(b)
  307. Revision 1.22 2003/10/25 22:52:07 florian
  308. * fixed copy(<dynarray>, ...)
  309. Revision 1.21 2002/11/26 23:02:07 peter
  310. * fixed dynarray copy
  311. Revision 1.20 2002/10/09 20:24:30 florian
  312. + range checking for dyn. arrays
  313. Revision 1.19 2002/10/02 18:21:51 peter
  314. * Copy() changed to internal function calling compilerprocs
  315. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  316. new copy functions
  317. Revision 1.18 2002/09/07 15:07:45 peter
  318. * old logs removed and tabs fixed
  319. Revision 1.17 2002/04/26 15:19:05 peter
  320. * use saveregisters for incr routines, saves also problems with
  321. the optimizer
  322. Revision 1.16 2002/04/25 20:14:56 peter
  323. * updated compilerprocs
  324. * incr ref count has now a value argument instead of var
  325. Revision 1.15 2002/01/21 20:16:08 peter
  326. * updated for dynarr:=nil
  327. }