[En-Nut-Discussion] Understanding NutHeapAlloc()/NutHeapFree()

Ralph Mason ralph.mason at telogis.com
Thu Nov 27 03:38:44 CET 2003


You are correct (from my understanding), however it's not portable like that
b/c it assumes sizeof (void*) == 2 which it doesn't always


    /*
     * 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.
     *
     * And space behind it for the End of mem marker
     */
    if ((size += (sizeof(u_int) + sizeof(u_long)) ) < sizeof(HEAPNODE))
        size = sizeof(HEAPNODE);

Here is my updated version that works on windows and with AVR.

I have ported all the heap stuff so that is *should* work with any sized
pointers if you want it.

Ralph


> -----Original Message-----
> From: en-nut-discussion-bounces at egnite.de
> [mailto:en-nut-discussion-bounces at egnite.de]On Behalf Of Jan Dubiec
> Sent: Thursday, 27 November 2003 3:28
> To: en-nut-discussion at egnite.de
> Subject: [En-Nut-Discussion] Understanding NutHeapAlloc()/NutHeapFree()
>
>
> Hello,
>
> I am porting Nut/OS to Renesas' H8/3068F MCU. Unfortunately, I am not
> sure if I understand NutHeapAlloc()/NutHeapFree() source properly. Let
> assume that a thread wants to allocate N bytes from the heap. IMO in
> such case reserved memory area looks as follows:
>
> +------+------------------------------------------------+----------+
> |header|       available for a thread                   |0xDEADBEEF|
> +------+------------------------------------------------+----------+
>  sizeof(HEAPNODE)           N bytes                        4 bytes
>     bytes
>        ^
>        +-- pointer returned to a thread (application)
> Am I right?
>
> Next, there is following code at the beginnig of NutHeapAlloc():
>     /*
>      * 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 += 6) < sizeof(HEAPNODE))
>         size = sizeof(HEAPNODE);
>
> What does 6 mean? Is this sizeof(HEAPNODE)/2 + sizeof(0xDEADBEEF)?
>
> Regards,
> /J.D.
> --
> Jan Dubiec, jdx at slackware.pl, mobile: +48 602 101787
>
> Głęboka wiara wymaga płytkiego rozumu i nikłej wiedzy.
> _______________________________________________
> En-Nut-Discussion mailing list
> En-Nut-Discussion at egnite.de
> http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion
>




More information about the En-Nut-Discussion mailing list