initc.pp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1997-98 by Pierre Muller
  5. Code to generate execution of all c functions
  6. with constructors attributes
  7. Based on .ctor and .dtor sections of DJGPP gcc compiler
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. unit InitC;
  15. interface
  16. implementation
  17. type
  18. simple_proc = procedure;
  19. var
  20. first_ctor : longint;external name 'djgpp_first_ctor';
  21. ctor : array [0..maxlongint] of simple_proc;external name 'djgpp_first_ctor';
  22. last_ctor : longint;external name 'djgpp_last_ctor';
  23. first_dtor : longint;external name 'djgpp_first_dtor';
  24. dtor : array [0..maxlongint] of simple_proc;external name 'djgpp_first_dtor';
  25. last_dtor : longint;external name 'djgpp_last_dtor';
  26. bss_count : longint;external name '___bss_count';
  27. const
  28. save_exit : pointer = nil;
  29. procedure run_c_constructors;
  30. const
  31. already_done : longint = -1;
  32. var
  33. f : simple_proc;
  34. i,nb : longint;
  35. begin
  36. if already_done=bss_count then
  37. exit;
  38. already_done:=bss_count;
  39. f:=ctor[0];
  40. nb:=((cardinal(@last_ctor)-cardinal(@first_ctor)) div sizeof(pointer));
  41. for i:=1 to nb do
  42. begin
  43. f();
  44. f:=ctor[i];
  45. end;
  46. end;
  47. procedure run_c_destructors;
  48. const
  49. already_done : longint = -1;
  50. var
  51. f : simple_proc;
  52. i,nb : longint;
  53. begin
  54. exitproc:=save_exit;
  55. if already_done=bss_count then
  56. exit;
  57. already_done:=bss_count;
  58. f:=dtor[0];
  59. nb:=((cardinal(last_dtor)-cardinal(first_dtor)) div sizeof(pointer));
  60. for i:=1 to nb do
  61. begin
  62. f();
  63. f:=dtor[i];
  64. end;
  65. end;
  66. begin
  67. run_c_constructors;
  68. If cardinal(@first_dtor)<>cardinal(@last_dtor) then
  69. begin
  70. { can exitproc be allready non nil here ?
  71. you have to make really weird things to achieve
  72. that be lets suppose it is possible !! (PM) }
  73. save_exit:=exitproc;
  74. exitproc:=@run_c_destructors;
  75. end;
  76. end.
  77. {
  78. $Log$
  79. Revision 1.2 1998-12-21 14:13:07 pierre
  80. * problems with procvar address solved
  81. Revision 1.1 1998/12/21 13:14:30 peter
  82. * moved
  83. Revision 1.1 1998/12/21 11:56:26 pierre
  84. First implementation of intc unit
  85. }