[fpc-pascal] How can a project determine if it's using cmem?

Ludo Brands ludo.brands at free.fr
Wed Jul 4 14:57:08 CEST 2012


> How can code in the library test whether it is using cmem, so that it 
> can tell the caller that it's safe to call functions that 
> move strings 
> and objects around? It's obviously trivial to rely on a compile-time 
> conditional, but can this be done in a way that doesn't rely on this?
> 

Following little function detects whether the internal fp memory manager is
used:

function UsesFPCMemManager:boolean;
var
  p:pointer;
  TotalAllocated:cardinal;
const
  TESTSIZE=10000;
begin
  TotalAllocated:=SysGetHeapStatus.TotalAllocated;
  getmem(p,TESTSIZE);
  Result:=SysGetHeapStatus.TotalAllocated-TotalAllocated>TESTSIZE;
  freemem(p);
end;

This is obviously not thread safe. 
Using Heaptrc doesn not affect the result.
In case the internal memory manager is not used, it doesn't tell which one
is used. Detecting that is much more difficult. Detecting that the c library
is loaded is not enough because another unit could have included it. If
there is debug info in the executable one could look up the addresses
returned by GetMemoryManager.  

Ludo




More information about the fpc-pascal mailing list