initc.pp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 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. { we need to include dpmiexcp unit
  18. to avoid getting troubles with _exit found both
  19. in libc and in v2prt0.as PM }
  20. uses
  21. dpmiexcp;
  22. type
  23. simple_proc = procedure;
  24. var
  25. first_ctor : longint;external name 'djgpp_first_ctor';
  26. ctor : array [0..maxlongint] of simple_proc;external name 'djgpp_first_ctor';
  27. last_ctor : longint;external name 'djgpp_last_ctor';
  28. first_dtor : longint;external name 'djgpp_first_dtor';
  29. dtor : array [0..maxlongint] of simple_proc;external name 'djgpp_first_dtor';
  30. last_dtor : longint;external name 'djgpp_last_dtor';
  31. bss_count : longint;external name '___bss_count';
  32. const
  33. save_exit : pointer = nil;
  34. procedure run_c_constructors;
  35. const
  36. already_done : longint = -1;
  37. var
  38. f : simple_proc;
  39. i,nb : longint;
  40. begin
  41. if already_done=bss_count then
  42. exit;
  43. already_done:=bss_count;
  44. f:=ctor[0];
  45. nb:=((cardinal(@last_ctor)-cardinal(@first_ctor)) div sizeof(pointer));
  46. for i:=1 to nb do
  47. begin
  48. f();
  49. f:=ctor[i];
  50. end;
  51. end;
  52. procedure run_c_destructors;
  53. const
  54. already_done : longint = -1;
  55. var
  56. f : simple_proc;
  57. i,nb : longint;
  58. begin
  59. exitproc:=save_exit;
  60. if already_done=bss_count then
  61. exit;
  62. already_done:=bss_count;
  63. f:=dtor[0];
  64. nb:=((cardinal(last_dtor)-cardinal(first_dtor)) div sizeof(pointer));
  65. for i:=1 to nb do
  66. begin
  67. f();
  68. f:=dtor[i];
  69. end;
  70. end;
  71. begin
  72. run_c_constructors;
  73. If cardinal(@first_dtor)<>cardinal(@last_dtor) then
  74. begin
  75. { can exitproc be allready non nil here ?
  76. you have to make really weird things to achieve
  77. that be lets suppose it is possible !! (PM) }
  78. save_exit:=exitproc;
  79. exitproc:=@run_c_destructors;
  80. end;
  81. end.
  82. {
  83. $Log$
  84. Revision 1.6 2000-02-09 16:59:29 peter
  85. * truncated log
  86. Revision 1.5 2000/01/09 00:35:17 pierre
  87. * initc now loads dpmiexcp unit to avoid linker problems
  88. Revision 1.4 2000/01/07 16:41:32 daniel
  89. * copyright 2000
  90. Revision 1.3 2000/01/07 16:32:23 daniel
  91. * copyright 2000 added
  92. }