ppheap.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. const
  32. MaxFiles = 1024;
  33. MaxNameLength = 39;
  34. type
  35. theapfileinfo = record
  36. name : string[MaxNameLength];
  37. index : longint;
  38. end;
  39. tfileinfoarray = array [1..MaxFiles] of theapfileinfo;
  40. var
  41. fileinfoarray : tfileinfoarray;
  42. last_index : longint;
  43. procedure ppheap_register_file(name : string;index : longint);
  44. begin
  45. inc(last_index);
  46. if last_index <= MaxFiles then
  47. begin
  48. fileinfoarray[last_index].name:=copy(name,1,MaxNameLength);
  49. fileinfoarray[last_index].index:=index;
  50. end
  51. else
  52. writeln(stderr,'file',name,' has index ',index);
  53. end;
  54. function getfilename(index : longint) : string;
  55. var
  56. i : longint;
  57. begin
  58. for i:=1 to last_index do
  59. begin
  60. if fileinfoarray[i].index=index then
  61. begin
  62. getfilename:=fileinfoarray[i].name;
  63. exit;
  64. end;
  65. end;
  66. getfilename:=tostr(index);
  67. end;
  68. {*****************************************************************************
  69. Heaptrc callbacks
  70. *****************************************************************************}
  71. type
  72. pextra_info = ^textra_info;
  73. textra_info = record
  74. line,
  75. col,
  76. fileindex : longint;
  77. end;
  78. procedure set_extra_info(p : pointer);
  79. begin
  80. with pextra_info(p)^ do
  81. begin
  82. line:=current_filepos.line;
  83. col:=current_filepos.column;
  84. if assigned(current_module) then
  85. fileindex:=current_module.unit_index*100000+current_filepos.fileindex
  86. else
  87. fileindex:=current_filepos.fileindex;
  88. end;
  89. end;
  90. procedure show_extra_info(var t : text;p : pointer);
  91. begin
  92. with pextra_info(p)^ do
  93. begin
  94. writeln(t,'Memory allocated at '+getfilename(fileindex)+'('+tostr(line)+','+tostr(col)+') ');
  95. end;
  96. end;
  97. const
  98. pp_heap_inited : boolean = false;
  99. procedure pp_heap_init;
  100. begin
  101. if not pp_heap_inited then
  102. begin
  103. {$ifdef extheaptrc}
  104. keepreleased:=true;
  105. {$endif extheaptrc}
  106. SetHeapTraceOutput('heap.log');
  107. SetHeapExtraInfo(sizeof(textra_info),
  108. @set_extra_info,
  109. @show_extra_info);
  110. end;
  111. pp_heap_inited:=true;
  112. end;
  113. begin
  114. pp_heap_init;
  115. end.