9.4 Functions and procedures

DumpHeap

Declaration:
procedure DumpHeap;
Description:
DumpHeap dumps to standard output a summary of memory usage. It is called automatically by the heaptrc unit when your program exits (by instaling an exit procedure), but it can be called at any time
Errors:
None.
See also:
MarkHeap (240)

MarkHeap

Declaration:
procedure MarkHeap;
Description:
MarkHeap marks all memory blocks with a special signature. You can use this if you think that you corruped the memory.
Errors:
None.
See also:
DumpHeap (240)

SetExtraInfo

Declaration:
procedure SetExtraInfo( size : longint;func : FillExtraInfoType);
Description:
You can use SetExtraInfo to store extra info in the blocks that the heaptrc unit reserves when tracing getmem calls. Size indicates the size (in bytes) that the trace mechanism should reserve for your extra information. For each call to getmem, func will be called, and passed a pointer to the memory reserved.

When dumping the memory summary, the extra info is shown as Longint values.

Errors:
You can only call SetExtraInfo if no memroy has been allocated yet. If memory was already allocated prior to the call to SetExtraInfo, then an error will be displayed on standard error output, and a DumpHeap (240) is executed.
See also:
DumpHeap (240),SetHeapTraceOutput (242)

Listing: heapex/setinfo.pp


Program heapex;

{ Program used to demonstrate the usage of heaptrc unit }

Uses heaptrc;

Var P1 : ^Longint;
    P2 : Pointer;
    I : longint;
    Marker : Longint;

Procedure SetMarker (P : pointer);

Type PLongint = ^Longint;

begin
  PLongint(P)^:=Marker;
end;

Procedure  Part1;

begin
  // Blocks allocated here are marked with $FFAAFFAA = -5570646
  Marker := $FFAAFFAA;
  New(P1);
  New(P1);
  Dispose(P1);
  For I:=1 to 10 do
    begin
    GetMem (P2,128);
    If (I mod 2) = 0 Then FreeMem(P2,128);
    end;
  GetMem(P2,128);
end;

Procedure  Part2;

begin
  // Blocks allocated here are marked with $FAFAFAFA = -84215046
  Marker := $FAFAFAFA;
  New(P1);
  New(P1);
  Dispose(P1);
  For I:=1 to 10 do
    begin
    GetMem (P2,128);
    If (I mod 2) = 0 Then FreeMem(P2,128);
    end;
  GetMem(P2,128);
end;

begin
 SetExtraInfo(SizeOf(Marker),@SetMarker);
 Writeln ('Part 1');
 part1;
 Writeln('Part 2');
 part2;
end.

SetHeapTraceOutput

Declaration:
Procedure SetHeapTraceOutput(const name : string);
Description:
SetHeapTraceOutput sets the filename into which heap trace info will be written. By default information is written to standard output, this function allows you to redirect the information to a file with full filename name.
Errors:
If the file cannot be written to, errors will occur when writing the trace.
See also:
SetExtraInfo (240)