By default, the mpatrol library follows the guidelines for ANSI C and C++
regarding the behaviour of the dynamic memory allocation and memory operation
functions it replaces. This means that calling
malloc() with a size of
zero is allowed, for example. However, warnings can be generated for all of
these types of calls by using the CHECKALL option. The
CHECKALLOCS option warns only about calls to
similar functions with a size of zero, the CHECKREALLOCS option warns
only about calls to
realloc() and similar functions with either a null
pointer or a size of zero, and the CHECKFREES option warns only about
free() and similar functions with a null pointer. The
CHECKMEMORY option gives a warning if a zero-size memory operation is
performed or an error if a memory operation is performed on a `NULL'
pointer — this is normally allowed by default.
All newly-allocated memory can be pre-filled with a specified byte by using the
ALLOCBYTE option. This can be used to catch out code that expects
newly-allocated memory to be zeroed, although this option will have no effect on
memory that was allocated with
calloc(). All free memory can also be
pre-filled with a different specified byte by using the FREEBYTE
option. This will catch out code that expects to be able to use the contents of
freed memory. Note that you may wish to change these options from their default
values on your system so that the contents can be filled with values that are
least likely to be used at run-time. For example, ensuring that the pointer
representation of the value can never be a valid pointer, or that the floating
point representation will always be invalid. These values will vary across
operating systems and processor architectures.
Alternatively, the mpatrol library can be instructed to keep all (or a certain number of recent) freed memory allocations so that its diagnostics can be clearer about which freed allocation a piece of code is erroneously trying to access. This is controlled with the NOFREE option, which accepts an argument specifying the maximum number of recently-freed memory allocations to prevent being reused. If the argument is zero then all freed memory allocations will be immediately reused by the mpatrol library. If the argument is non-zero then the mpatrol library will use up more memory than usual since it has to keep all of the freed memory allocations lying around until their lifetime has expired. Note that this option distinguishes between free memory and freed memory. Free memory is unallocated memory that has been taken from the system heap. Freed memory is a freed memory allocation, with all of the original details of the allocation preserved.
Normally, the NOFREE option will fill the freed allocation with the free byte so that any code that accesses it will hopefully fall over. However, the original contents can be preserved using the PRESERVE option in case you need to see what the contents were just before it was freed. The NOFREE option is also affected by the PAGEALLOC option, since then the freed allocation will have its contents both read and write protected so that nothing can access them. If the PRESERVE option is used in this case then the freed allocation will only be made write-protected so that the original contents can be read from but not written to.
Note that if the argument specified with the NOFREE option is non-zero then the mpatrol library will store all recently-freed memory allocations in a queue. Once the queue has filled to the point specified with the NOFREE option then all subsequent calls to free memory will result in the most recently-freed memory allocation being placed at the end of the queue and the freed memory allocation at the beginning of the queue will be returned to the free memory pool for later reuse. Obviously, the larger the freed queue size, the better chance of detecting attempts to access previously-freed memory, but unfortunately more memory will be used up and the mpatrol library will have to keep track of a larger number of memory allocations.