[En-Nut-Discussion] Suggested small changes for heap.c
Malte Marwedel
m.marwedel at onlinehome.de
Sun Feb 7 18:28:15 CET 2010
Hello,
Ulrich Prinz wrote:
> Hi Malte,
>
> As not everyone is deep inside the code at any place, it would be fine
> to make some more explanations, why and what you try to do with this
> change. I think it might be a bug and someone forgot to include the
> debug features in the function. But it's always difficult to rate a
> report without further information.
Ok, I will try. All changes only make a difference if someone has the
Memory
debugging features enabled (allows checking for memory corruption and
prints out the
location in the source file where the call came from - really helpful for
debugging).
> Thanks in advance and best regards,
> Ulrich
>
> Malte Marwedel wrote:
>> Hello,
>> In NutHeapRootFree(), I think
>> NUTPANIC("Trying to release free heap memory at %p in %s:%d\n", file, line);
>> should be
>> NUTPANIC("Trying to release free heap memory at %p in %s:%d\n", block,
>> file, line);
Here, someone simply forgot one parameter for NUTPANIC. file is a
pointer to the name of
the source file where NutHeapRootFree() was called. Line the line number
in that file.
Currently just the pointer is printed and the line number is handled as
pointer to a string -> bad. %p should be the address of the failed to be
released memory location.
>> And in NutHeapRootAvailable() and NutHeapRootRegionAvailable():
>> rc += node->hn_size - NUT_HEAP_OVERHEAD;
>> and
>> return rc - NUT_HEAP_OVERHEAD;
>> I think the following would make more sense:
>> rc += node->hn_size - (NUT_HEAP_OVERHEAD + 2 * NUTMEM_GUARD_BYTES);
>> and
>> return rc - (NUT_HEAP_OVERHEAD + 2 * NUTMEM_GUARD_BYTES);
In order to detect memory corruptions, someone can enable NUTMEM_GUARD.
It will place two bytes (or four on a 32 bit platform) (the size is
defined with NUTMEM_GUARD_BYTES) with a special pattern right in-front
and after the allocated memory region. If free is called, it checks if
the pattern is still there. NUT_HEAP_OVERHEAD is the size used by the
struct to manage the allocated memory list, but does not include the
guard bytes. Currently the two functions ignore that the guard bytes
need additional space. So the following could fail:
if (NutHeapRootRegionAvailable() >= 10) {
malloc(10);
Without NUTMEM_GUARD, NUTMEM_GUARD_BYTES is defined as 0, so the
addition and multiplication will (hopefully) optimized away if no memory
corruption checks are enabled.
Regards,
Malte
More information about the En-Nut-Discussion
mailing list