ppheap.pas 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. {
  2. Copyright (c) 1998-2002 by Pierre Muller
  3. Simple unit to add source line and column to each
  4. memory allocation made with heaptrc unit
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  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. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************}
  17. unit ppheap;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses heaptrc;
  21. { call this function before any memory allocation
  22. in a unit initialization code (PM) }
  23. procedure pp_heap_init;
  24. procedure ppheap_register_file(name : string;index : longint);
  25. implementation
  26. uses
  27. cutils,globtype,globals,fmodule;
  28. {*****************************************************************************
  29. Filename registration
  30. *****************************************************************************}
  31. { We need to avoid using memory allocation here
  32. and thus use a fixed size static array }
  33. const
  34. MaxFiles = 1024;
  35. MaxNameLength = 39;
  36. type
  37. theapfileinfo = record
  38. name : string[MaxNameLength];
  39. index : longint;
  40. end;
  41. tfileinfoarray = array [1..MaxFiles] of theapfileinfo;
  42. var
  43. fileinfoarray : tfileinfoarray;
  44. last_index : longint;
  45. procedure ppheap_register_file(name : string;index : longint);
  46. var
  47. len,pos : longint;
  48. begin
  49. inc(last_index);
  50. if last_index <= MaxFiles then
  51. begin
  52. { Keep last part of name if too long }
  53. len:=length(name);
  54. if len>MaxNameLength then
  55. begin
  56. pos:=len-MaxNameLength+4;
  57. fileinfoarray[last_index].name:='...'+copy(name,pos,MaxNameLength);
  58. end
  59. else
  60. begin
  61. pos:=1;
  62. fileinfoarray[last_index].name:=copy(name,pos,MaxNameLength);
  63. end;
  64. fileinfoarray[last_index].index:=index;
  65. end
  66. else
  67. writeln(stderr,'file',name,' has index ',index);
  68. end;
  69. function getfilename(index : longint) : string;
  70. var
  71. i : longint;
  72. begin
  73. for i:=1 to last_index do
  74. begin
  75. if fileinfoarray[i].index=index then
  76. begin
  77. getfilename:=fileinfoarray[i].name;
  78. exit;
  79. end;
  80. end;
  81. getfilename:=tostr(index);
  82. end;
  83. {*****************************************************************************
  84. Heaptrc callbacks
  85. *****************************************************************************}
  86. type
  87. pextra_info = ^textra_info;
  88. textra_info = record
  89. line,
  90. col,
  91. fileindex : longint;
  92. end;
  93. procedure set_extra_info(p : pointer);
  94. begin
  95. with pextra_info(p)^ do
  96. begin
  97. line:=current_filepos.line;
  98. col:=current_filepos.column;
  99. if assigned(current_module) then
  100. fileindex:=current_module.unit_index*100000+current_filepos.fileindex
  101. else
  102. fileindex:=current_filepos.fileindex;
  103. end;
  104. end;
  105. procedure show_extra_info(var t : text;p : pointer);
  106. begin
  107. with pextra_info(p)^ do
  108. begin
  109. writeln(t,'Memory allocated at '+getfilename(fileindex)+'('+tostr(line)+','+tostr(col)+') ');
  110. end;
  111. end;
  112. const
  113. pp_heap_inited : boolean = false;
  114. procedure pp_heap_init;
  115. begin
  116. if not pp_heap_inited then
  117. begin
  118. {$ifdef extheaptrc}
  119. keepreleased:=true;
  120. {$endif extheaptrc}
  121. SetHeapTraceOutput('heap.log');
  122. SetHeapExtraInfo(sizeof(textra_info),
  123. @set_extra_info,
  124. @show_extra_info);
  125. end;
  126. pp_heap_inited:=true;
  127. end;
  128. begin
  129. pp_heap_init;
  130. end.