dynarr.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. tdynarrayindex = longint;
  16. pdynarrayindex = ^tdynarrayindex;
  17. t_size = dword;
  18. { don't add new fields, the size is used }
  19. { to calculate memory requirements }
  20. tdynarray = record
  21. refcount : dword;
  22. high : tdynarrayindex;
  23. end;
  24. pdynarray = ^tdynarray;
  25. pdynarraytypeinfo = packed record
  26. kind : byte;
  27. namelen : byte;
  28. // here the chars follow, we've to skip them
  29. elesize : t_size;
  30. eletype : pdynarraytypeinfo;
  31. end;
  32. function dynarray_length(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_LENGTH'];
  33. begin
  34. dynarray_length:=pdynarray(p-sizeof(tdynarray))^.high+1;
  35. end;
  36. function dynarray_high(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_HIGH'];
  37. begin
  38. //!!!!!!! dynarray_length:=pdynarray(p-sizeof(tdynarray))^.high;
  39. end;
  40. procedure dynarray_decr_ref(var p : pointer);[Public,Alias:'FPC_DYNARRAY_DECR_REF'];
  41. begin
  42. end;
  43. procedure dynarray_setlength(var p : pointer;ti : pdynarraytypeinfo;
  44. dimcount : dword;dims : pdynarrayindex);[Public,Alias:'FPC_DYNARRAY_SETLENGTH'];
  45. var
  46. i : tdynarrayindex;
  47. size : t_size;
  48. { contains the "fixed" pointers where the refcount }
  49. { and high are at positive offsets }
  50. realp,newp : pdynarray;
  51. begin
  52. (* !!!!!!
  53. realp:=pdynarray(p-sizeof(tdynarray));
  54. if dims[0]<0 then
  55. HandleErrorFrame(201,get_frame);
  56. if dims[0]=0 then
  57. begin
  58. { release all data }
  59. !!!!!
  60. p:=nil;
  61. exit;
  62. end;
  63. if dims[0]<>realp^.high+1 then
  64. begin
  65. { determine new memory size }
  66. size:=ti.elesize*dims[0]+sizeof(tdynarray);
  67. { range checking is quite difficult ... }
  68. if (size<sizeof(tdynarray)) or
  69. ((ti.elesize>0) and (size<ti.elesize)) then
  70. HandleErrorFrame(201,get_frame);
  71. { skip kind and name }
  72. inc(pointer(ti),ord(ti.namelen));
  73. { resize? }
  74. if realp.refcount=1 then
  75. begin
  76. { shrink the array? }
  77. if dims[0]<realp^.high+1 then
  78. begin
  79. for i:=dims[0]-1 to realp^.high do
  80. finalize(,ti^.eletype);
  81. reallocmem(realp,size);
  82. end
  83. else
  84. begin
  85. reallocmem(realp,size);
  86. !!!!!! fillchar
  87. end;
  88. end
  89. else
  90. begin
  91. { no, copy }
  92. !!!!!!!
  93. end;
  94. end;
  95. { handle nested arrays }
  96. if dimcount>1 then
  97. begin
  98. for i:=0 to dims[0]-1 do
  99. dynarray_setlength(newp+sizeof(tdynarray)+i*elesize,
  100. ti.eletype,dimcount-1,@dims[1]);
  101. end;
  102. p:=newp+sizeof(tdynarray);
  103. !!!!!! *)
  104. end;
  105. function dynarray_copy(var p : pointer;ti : pdynarraytypeinfo;
  106. dimcount : dword;dims : pdynarrayindex) : pointer;[Public,Alias:'FPC_DYNARRAY_COPY'];
  107. begin
  108. end;
  109. {
  110. $Log$
  111. Revision 1.3 2000-11-07 23:42:21 florian
  112. + AfterConstruction and BeforeDestruction implemented
  113. + TInterfacedObject implemented
  114. Revision 1.2 2000/11/06 21:35:59 peter
  115. * removed some warnings
  116. Revision 1.1 2000/11/04 17:52:46 florian
  117. * fixed linker errors
  118. }