Using the CoverageScanner library with a memory pool

The CoverageScanner library can use an internal memory pool to handle memory allocations. This is necessary on platforms which do not provide the standard C functions malloc() and free().

In this case, it is necessary to allocate a C array at program start which will serve these functions. Doing this requires an estimate of the memory consumption. The CoverageScanner library also provides a way to monitor the usage and to detect memory overflow.

Estimation of the size of the memory pool

The memory pool is used for dynamically loaded library support and for string manipulations. Its size depends on the number of files instrumented, and on the size of the temporary output buffer.

For code that uses only static libraries and which produces a single executable, use the following approximation:

512 + 100 * number_of_instrumented_sources

number_of_instrumented_sources includes the headers and the source files. The size of the temporary buffer used for the generation of the execution report file will be added automatically to this size.

Monitoring of the size of the memory pool

The following example shows how to monitor the memory usage of the memory pool:

#include <stdio.h>

#ifdef __COVERAGESCANNER__
void memory_failure( int s )
{
    int used = -1;
    int max_used = -1;
    int size = -1;
    __coveragescanner_memory_pool_stat( &size, &used, &max_used );
    printf( "Memory Failure  : Requested:%i Actual Size=%i Actually Used=%i Peak Usage=%i\n", s, size, used, max_used );
    exit( 2 );
}
#endif

int main( int argc, char *argv[] )
{
#ifdef __COVERAGESCANNER__
    int used = -1;
    int max_used = -1;
    int size = -1;
    __coveragescanner_memory_pool_stat( &size, &used, &max_used );
    printf( "Memory Pool Usage: Size=%i Used=%i Peak=%i\n", size, used, max_used );
#endif

    return 0 ;
}

To compile the example on Microsoft® Windows:

cscl memory-pool.c --cs-memory-pool=1000 --cs-memory-alloc-failure-function=memory_failure

To compile the example on Linux™ or macOS:

csgcc memory-pool.c -o customiofile --cs-memory-pool=1000 --cs-memory-alloc-failure-function=memory_failure

The size of the memory pool is 1000 bytes plus the size of the temporary buffer that is used for serializing the execution report. To monitor the actual usage, use the function CoverageScanner.__coveragescanner_memory_pool_stat().

If a memory overflow occurs, the C function memory_failure() is called. To simulate an overflow, set the size of the memory pool to 5 bytes.