2
0

ppheap.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Pierre Muller
  4. Simple unit to add source line and column to each
  5. memory allocation made with heaptrc unit
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  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. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************}
  18. unit ppheap;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses heaptrc;
  22. { call this function before any memory allocation
  23. in a unit initialization code (PM) }
  24. procedure pp_heap_init;
  25. procedure ppheap_register_file(name : string;index : longint);
  26. implementation
  27. uses
  28. cutils,globtype,globals,fmodule;
  29. {*****************************************************************************
  30. Filename registration
  31. *****************************************************************************}
  32. const
  33. MaxFiles = 1024;
  34. MaxNameLength = 39;
  35. type
  36. theapfileinfo = record
  37. name : string[MaxNameLength];
  38. index : longint;
  39. end;
  40. tfileinfoarray = array [1..MaxFiles] of theapfileinfo;
  41. var
  42. fileinfoarray : tfileinfoarray;
  43. last_index : longint;
  44. procedure ppheap_register_file(name : string;index : longint);
  45. begin
  46. inc(last_index);
  47. if last_index <= MaxFiles then
  48. begin
  49. fileinfoarray[last_index].name:=copy(name,1,MaxNameLength);
  50. fileinfoarray[last_index].index:=index;
  51. end
  52. else
  53. writeln(stderr,'file',name,' has index ',index);
  54. end;
  55. function getfilename(index : longint) : string;
  56. var
  57. i : longint;
  58. begin
  59. for i:=1 to last_index do
  60. begin
  61. if fileinfoarray[i].index=index then
  62. begin
  63. getfilename:=fileinfoarray[i].name;
  64. exit;
  65. end;
  66. end;
  67. getfilename:=tostr(index);
  68. end;
  69. {*****************************************************************************
  70. Heaptrc callbacks
  71. *****************************************************************************}
  72. type
  73. pextra_info = ^textra_info;
  74. textra_info = record
  75. line,
  76. col,
  77. fileindex : longint;
  78. end;
  79. procedure set_extra_info(p : pointer);
  80. begin
  81. with pextra_info(p)^ do
  82. begin
  83. line:=aktfilepos.line;
  84. col:=aktfilepos.column;
  85. if assigned(current_module) then
  86. fileindex:=current_module.unit_index*100000+aktfilepos.fileindex
  87. else
  88. fileindex:=aktfilepos.fileindex;
  89. end;
  90. end;
  91. {$ifdef VER1_0}
  92. function get_extra_info(p : pointer) : string;
  93. begin
  94. with pextra_info(p)^ do
  95. begin
  96. get_extra_info:=getfilename(fileindex)+'('+tostr(line)+','+tostr(col)+
  97. ') ';
  98. end;
  99. end;
  100. {$else}
  101. procedure show_extra_info(var t : text;p : pointer);
  102. begin
  103. with pextra_info(p)^ do
  104. begin
  105. writeln(t,getfilename(fileindex)+'('+tostr(line)+','+tostr(col)+') ');
  106. end;
  107. end;
  108. {$endif}
  109. const
  110. pp_heap_inited : boolean = false;
  111. procedure pp_heap_init;
  112. begin
  113. if not pp_heap_inited then
  114. begin
  115. keepreleased:=true;
  116. SetHeapTraceOutput('heap.log');
  117. {$ifdef VER1_0}
  118. SetExtraInfoString({$ifdef FPC}@{$endif}get_extra_info);
  119. {$else}
  120. SetHeapExtraInfo(sizeof(textra_info),
  121. {$ifdef FPCPROCVAR}@{$endif}set_extra_info,
  122. {$ifdef FPCPROCVAR}@{$endif}show_extra_info);
  123. {$endif}
  124. end;
  125. pp_heap_inited:=true;
  126. end;
  127. begin
  128. pp_heap_init;
  129. end.
  130. {
  131. $Log$
  132. Revision 1.12 2002-11-19 12:08:24 pierre
  133. * fix compilation failure
  134. Revision 1.11 2002/11/15 01:58:53 peter
  135. * merged changes from 1.0.7 up to 04-11
  136. - -V option for generating bug report tracing
  137. - more tracing for option parsing
  138. - errors for cdecl and high()
  139. - win32 import stabs
  140. - win32 records<=8 are returned in eax:edx (turned off by default)
  141. - heaptrc update
  142. - more info for temp management in .s file with EXTDEBUG
  143. Revision 1.10 2002/05/18 13:34:13 peter
  144. * readded missing revisions
  145. Revision 1.9 2002/05/16 19:46:43 carl
  146. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  147. + try to fix temp allocation (still in ifdef)
  148. + generic constructor calls
  149. + start of tassembler / tmodulebase class cleanup
  150. }