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

Mark Morgan Lloyd markMLl.fpc-pascal at telemetry.co.uk
Wed Jul 4 15:29:01 CEST 2012


Tomas Hajny wrote:
> On Wed, July 4, 2012 11:32, Mark Morgan Lloyd wrote:
>> When building a shared library, I'm putting a (function that returns a)
>> magic number into both the library and the calling program. This returns
>> an integer, and allows the program and library to check their exposed
>> APIs without risk.
>>
>> 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?
> 
> First of all, you can easily check whether the default memory manager is
> used by calling IsMemoryManagerSet. Obviously, this doesn't tell whether
> the custom memory manager is cmem or something else (e.g. TraceManager
> from HeapTrace). However, you could possibly try to check the distance
> between the individual MemoryManager methods retrieved using
> GetMemoryManager - while that is not bulletproof by any means, it provides
> at least some indication.
> 
> Finally, if there are certain major changes to features provided by
> individual memory managers (like some operation being safe or not), adding
> some TMemoryManager method allowing to provide this information may be the
> best solution for the future.

Thanks Tomas (and Ludo). Under the circumstances I think 
IsMemoryManagerSet is probably an adequate solution, since the real risk 
is that somebody messes about with the start of the main unit (or with 
the project settings) without appreciating that the cmem import is 
crucial to operation elsewhere in the program (or, analogously, that a 
shared library will crash when some functions are called).

Having something- e.g. an exported string- in cmem would be a partial 
solution, but would have to be checked in the main unit. With any 
solution, I think that testing that cmem was the first import could be a 
challenge.

-- 
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]



More information about the fpc-pascal mailing list