[En-Nut-Discussion] NutHeapAlloc Error?

Nathan Moore nategoose at gmail.com
Mon Oct 13 22:19:18 CEST 2008


On Mon, Oct 13, 2008 at 3:06 PM, Timothy M. De Baillie <
debaillie at ciholas.com> wrote:

> I'm trying to trace down a bug on a system that has REALLY low memory
> and has lots and lots of mallocs.  So this may not really be a problem,
> but I thought I would see what others thought.
>
> in the OS, os/heap.c,
>
> ~~~ OS CUT ~~~
> void *NutHeapAlloc(size_t size)
> {
>
> ......
>
>    if (size >= available) {
> #ifdef NUTDEBUG
>        if (__heap_trf)
>            fputs("MEMOVR\n", __heap_trs);
> #endif
>        return 0;
>    }
>
>    /*
>     * We need additional space in front of the allocated memory
>     * block to store its size. If this is still less than the
>     * space required by a free node, increase it.
>     */
>    if ((size += MEMOVHD) < sizeof(HEAPNODE))
>        size = sizeof(HEAPNODE);
> ~~~ OS CUT ~~~
>
> Should the check "if(size >= available)" not happen after the last two
> lines?
>
>  From what I read, if you request 11 bytes and had exactly 11 bytes
> available, then you would cause a memory wrap of some sort.
>

I think it would result in similar behavior as if you had 100 bytes free in
10 byte chunks and requested 20 bytes.
You'd check each of the chunks, none of them would be big enough, and in the
end (when (0== (node = node->next))
you'd break out of the loop.
It would behave correctly, as far as I can tell, but since the available
test is done anyway it might as well be done after
the size adjustments.

Nathan



More information about the En-Nut-Discussion mailing list