initc.pp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 : simple_proc;external name 'djgpp_first_ctor';
  21. last_ctor : simple_proc;external name 'djgpp_last_ctor';
  22. first_dtor : simple_proc;external name 'djgpp_first_dtor';
  23. last_dtor : simple_proc;external name 'djgpp_last_dtor';
  24. bss_count : longint;external name '___bss_count';
  25. const
  26. save_exit : pointer = nil;
  27. procedure run_c_constructors;
  28. const
  29. already_done : longint = -1;
  30. var
  31. f : simple_proc;
  32. i : longint;
  33. begin
  34. if already_done=bss_count then
  35. exit;
  36. already_done:=bss_count;
  37. f:=first_ctor;
  38. for i:=1 to ((longint(last_ctor)-longint(first_ctor)) div sizeof(pointer)) do
  39. begin
  40. f();
  41. inc(longint(f),sizeof(pointer));
  42. end;
  43. end;
  44. procedure run_c_destructors;
  45. const
  46. already_done : longint = -1;
  47. var
  48. f : simple_proc;
  49. i : longint;
  50. begin
  51. exitproc:=save_exit;
  52. if already_done=bss_count then
  53. exit;
  54. already_done:=bss_count;
  55. f:=first_dtor;
  56. for i:=1 to ((longint(last_dtor)-longint(first_dtor)) div sizeof(pointer)) do
  57. begin
  58. f();
  59. inc(longint(f),sizeof(pointer));
  60. end;
  61. end;
  62. begin
  63. run_c_constructors;
  64. If first_dtor<>last_dtor then
  65. begin
  66. { can exitproc be allready non nil here ?
  67. you have to make really weird things to achieve
  68. that be lets suppose it is possible !! (PM) }
  69. save_exit:=exitproc;
  70. exitproc:=@run_c_destructors;
  71. end;
  72. end.
  73. {
  74. $Log$
  75. Revision 1.1 1998-12-21 11:56:26 pierre
  76. First implementation of intc unit
  77. }